class ZendeskAppsTools::Server

Constants

ZENDESK_DOMAINS_REGEX

Public Instance Methods

access_control_allow_origin() click to toggle source

This sets the 'Access-Control-Allow-Origin' header for requests coming from zendesk

# File lib/zendesk_apps_tools/server.rb, line 78
def access_control_allow_origin
  headers 'Access-Control-Allow-Origin' => request.env['HTTP_ORIGIN'] if request_from_zendesk?
end
request_from_zendesk?() click to toggle source
# File lib/zendesk_apps_tools/server.rb, line 63
def request_from_zendesk?
  request.env['HTTP_ORIGIN'] =~ ZENDESK_DOMAINS_REGEX
end
send_file(*args) click to toggle source
Calls superclass method
# File lib/zendesk_apps_tools/server.rb, line 51
def send_file(*args)
  # does the request look like a request from the host product for the generated
  # installed.js file? If so, send that. Otherwise send the static file in the
  # app's public_folder (./assets).
  if request.env['PATH_INFO'] == '/app.js' && params.key?('locale') && params.key?('subdomain')
    serve_installed_js
  else
    access_control_allow_origin
    super(*args)
  end
end
serve_installed_js() click to toggle source
# File lib/zendesk_apps_tools/server.rb, line 19
def serve_installed_js
  access_control_allow_origin
  content_type 'text/javascript'

  new_settings = settings.settings_helper.refresh!
  settings.parameters = new_settings if new_settings

  package = ZendeskAppsSupport::Package.new(settings.root, false)
  app_name = package.manifest.name || 'Local App'
  installation = ZendeskAppsSupport::Installation.new(
    id: settings.app_id,
    app_id: settings.app_id,
    app_name: app_name,
    enabled: true,
    requirements: package.requirements_json,
    collapsible: true,
    settings: settings.parameters.merge(title: app_name),
    updated_at: Time.now.iso8601,
    created_at: Time.now.iso8601,
    plan: { name: settings.plan }
  )

  app_js = package.compile(
    app_id: settings.app_id,
    app_name: app_name,
    assets_dir: "http://localhost:#{settings.port}/",
    locale: params['locale']
  )

  ZendeskAppsSupport::Installed.new([app_js], [installation]).compile
end