@@ -10,12 +10,80 @@ class Server < Sinatra::Base
10
10
set :protection , except : :frame_options
11
11
ZENDESK_DOMAINS_REGEX = %r{^http(?:s)?://[a-z0-9-]+\. (?:zendesk|zopim|futuresimple|local.futuresimple|zendesk-(?:dev|master|staging))\. com$}
12
12
13
+ get '/app.json' do
14
+ server_installed_json
15
+ end
16
+
13
17
get '/app.js' do
14
18
serve_installed_js
15
19
end
16
20
17
21
enable :cross_origin
18
22
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
+
19
87
def serve_installed_js
20
88
access_control_allow_origin
21
89
content_type 'text/javascript'
0 commit comments