Skip to content

Commit 79de03a

Browse files
author
Olaf Kwant
committed
installed.json output
1 parent 631d3d2 commit 79de03a

File tree

1 file changed

+65
-0
lines changed

1 file changed

+65
-0
lines changed

lib/zendesk_apps_tools/server.rb

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,77 @@ 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+
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+
1984
def serve_installed_js
2085
access_control_allow_origin
2186
content_type 'text/javascript'

0 commit comments

Comments
 (0)