@@ -10,12 +10,77 @@ 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
+ apps = [ ]
28
+ installations = [ ]
29
+
30
+ absolute_app_path = File . join ( settings . root )
31
+
32
+ manifest_json = read_json ( File . join ( absolute_app_path , 'manifest.json' ) )
33
+ requirements_json = read_json ( File . join ( absolute_app_path , 'requirements.json' ) ) || nil
34
+
35
+ new_settings = settings . settings_helper . refresh!
36
+ settings . parameters = new_settings if new_settings
37
+
38
+ # add title to settings
39
+ settings . parameters [ 'title' ] = manifest_json [ 'name' ] || 'Local App'
40
+
41
+ apps << build_app_object (
42
+ settings ,
43
+ manifest_json
44
+ )
45
+
46
+ installations << build_installation_object (
47
+ settings ,
48
+ requirements_json
49
+ )
50
+
51
+ {
52
+ apps : apps ,
53
+ installations : installations ,
54
+ installation_orders : [ ]
55
+ } . to_json
56
+ end
57
+
58
+ def build_app_object ( settings , manifest )
59
+ manifest . merge ( {
60
+ asset_url_prefix : "http://localhost:#{ settings . port } /" ,
61
+ id : settings . app_id
62
+ } ) . reject { |key | [ 'parameters' , 'oauth' ] . include? ( key ) }
63
+ end
64
+
65
+ def build_installation_object ( settings , requirements )
66
+ {
67
+ app_id : settings . app_id ,
68
+ name : settings . parameters [ 'title' ] ,
69
+ collapsible : true ,
70
+ enabled : true ,
71
+ id : settings . app_id ,
72
+ plan : { name : settings . plan } ,
73
+ requirements : requirements ,
74
+ settings : settings . parameters ,
75
+ updated_at : Time . now . iso8601
76
+ }
77
+ end
78
+
79
+ def read_json ( path , parser_opts = { } )
80
+ file = File . read ( path ) if File . exists? ( path )
81
+ JSON . parse ( file , parser_opts ) unless file . nil?
82
+ end
83
+
19
84
def serve_installed_js
20
85
access_control_allow_origin
21
86
content_type 'text/javascript'
0 commit comments