@@ -10,12 +10,80 @@ class Server < Sinatra::Base
1010 set :protection , except : :frame_options
1111 ZENDESK_DOMAINS_REGEX = %r{^http(?:s)?://[a-z0-9-]+\. (?:zendesk|zopim|futuresimple|local.futuresimple|zendesk-(?:dev|master|staging))\. com$}
1212
13+ get '/app.json' do
14+ server_installed_json
15+ end
16+
1317 get '/app.js' do
1418 serve_installed_js
1519 end
1620
1721 enable :cross_origin
1822
23+ def server_installed_json
24+ access_control_allow_origin
25+ content_type 'application/json'
26+
27+ app_paths = [ settings . root ]
28+ apps = [ ]
29+ installations = [ ]
30+
31+ app_paths . each do |app_path |
32+ absolute_app_path = File . join ( app_path )
33+
34+ manifest_json = read_json ( File . join ( absolute_app_path , 'manifest.json' ) )
35+ requirements_json = read_json ( File . join ( absolute_app_path , 'requirements.json' ) ) || nil
36+
37+ new_settings = settings . settings_helper . refresh!
38+ settings . parameters = new_settings if new_settings
39+
40+ # add title to settings
41+ settings . parameters [ 'title' ] = manifest_json [ 'name' ] || 'Local App'
42+
43+ apps << build_app_object (
44+ settings ,
45+ manifest_json
46+ )
47+
48+ installations << build_installation_object (
49+ settings ,
50+ requirements_json
51+ )
52+ end
53+
54+ {
55+ apps : apps ,
56+ installations : installations ,
57+ installation_orders : [ ]
58+ } . to_json
59+ end
60+
61+ def build_app_object ( settings , manifest )
62+ manifest . merge ( {
63+ asset_url_prefix : "http://localhost:#{ settings . port } /" ,
64+ id : settings . app_id
65+ } ) . reject { |key | [ 'parameters' , 'oauth' ] . include? ( key ) }
66+ end
67+
68+ def build_installation_object ( settings , requirements )
69+ {
70+ app_id : settings . app_id ,
71+ name : settings . parameters [ 'title' ] ,
72+ collapsible : true ,
73+ enabled : true ,
74+ id : settings . app_id ,
75+ plan : { name : settings . plan } ,
76+ requirements : requirements ,
77+ settings : settings . parameters ,
78+ updated_at : Time . now . iso8601
79+ }
80+ end
81+
82+ def read_json ( path , parser_opts = { } )
83+ file = File . read ( path ) if File . exists? ( path )
84+ JSON . parse ( file , parser_opts ) unless file . nil?
85+ end
86+
1987 def serve_installed_js
2088 access_control_allow_origin
2189 content_type 'text/javascript'
0 commit comments