<commit_before>require 'sinatra/base'
require 'sinatra/cross_origin'
require 'zendesk_apps_support/package'

module ZendeskAppsTools
  class Server < Sinatra::Base
    set :protection, except: :frame_options
    ZENDESK_DOMAINS_REGEX = %r{^http(?:s)?://[a-z0-9-]+\.(?:zendesk|zopim|zd-(?:dev|master|staging))\.com$}

    get '/app.js' do
      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,
        settings: settings.parameters.merge(title: app_name),
        updated_at: Time.now.iso8601,
        created_at: Time.now.iso8601
      )

      app_js = package.compile_js(
        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_js
    end

    enable :cross_origin

    def send_file(*args)
      access_control_allow_origin
      super(*args)
    end

    def request_from_zendesk?
      request.env['HTTP_ORIGIN'] =~ ZENDESK_DOMAINS_REGEX
    end

    # This is for any preflight request
    # It reads 'Access-Control-Request-Headers' to set 'Access-Control-Allow-Headers'
    # And also sets 'Access-Control-Allow-Origin' header<commit_msg>stop using unfortunately named methods<commit_after>require 'sinatra/base'
require 'sinatra/cross_origin'
require 'zendesk_apps_support/package'

module ZendeskAppsTools
  class Server < Sinatra::Base
    set :protection, except: :frame_options
    ZENDESK_DOMAINS_REGEX = %r{^http(?:s)?://[a-z0-9-]+\.(?:zendesk|zopim|zd-(?:dev|master|staging))\.com$}

    get '/app.js' do
      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,
        settings: settings.parameters.merge(title: app_name),
        updated_at: Time.now.iso8601,
        created_at: Time.now.iso8601
      )

      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

    enable :cross_origin

    def send_file(*args)
      access_control_allow_origin
      super(*args)
    end

    def request_from_zendesk?
      request.env['HTTP_ORIGIN'] =~ ZENDESK_DOMAINS_REGEX
    end

    # This is for any preflight request
    # It reads 'Access-Control-Request-Headers' to set 'Access-Control-Allow-Headers'
    # And also sets 'Access-Control-Allow-Origin' header
{'fraction_non_alphanumeric': 0.10997367651360047, 'fraction_numerical': 0.0058496636443404505, 'mean_word_length': 3.227441285537701, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3723835', 'n_tokens_mistral': 1117, 'n_tokens_neox': 1080, 'n_words': 197}