<commit_before>require 'idobata/hook'

require 'net/http'

require 'action_dispatch/http/headers'
require 'sinatra/base'
require 'sinatra/multi_route'
require 'sprockets'

raise 'Missing environment variable: `IDOBATA_HOOK_URL` is required.' unless ENV['IDOBATA_HOOK_URL']

module Idobata::Hook
  configure do |config|
    config.image_root = '/assets'
    config.image_host = ENV['IDOBATA_HOOK_HOST']
  end

  class Application < Sinatra::Application
    register Sinatra::MultiRoute

    set :idobta_hook_url, ENV['IDOBATA_HOOK_URL']

    set :sprockets, Sprockets::Environment.new

    configure do
      sprockets.append_path Idobata::Hook.root.join('hooks')
    end

    get '/' do
      'hi from Idobata::Hooks.'
    end

    route :get, :post, '/:identifier' do
      hook = Idobata::Hook.find(params[:identifier])

      raw_body = env['rack.input'].read

      headers = ActionDispatch::Http::Headers.new(env)

      payload = hook.new(raw_body, headers).process_payload

      post_to_idobata payload

      'OK'
    end

    private

    def post_to_idobata(payload)
      form = []

      form << ['source', payload[:source].to_s]
      form << ['format', payload[:format].to_s]

      [*payload[:images]].each do |image|
        form << ['image[]', image.tempfile, filename: image.filename, content_type: image.type]
      end

      url  = URI(settings.idobta_hook_url)
      post = Net::HTTP::Post.new(url)

      post.set_form form, 'multipart/form-data'

      Net::HTTP.start(url.hostname, url.port, use_ssl: url.scheme == 'https') do |http|
        http.request post
      end
    end
  end
end
<commit_msg>Refactoring<commit_after>require 'idobata/hook'

require 'net/http'

require 'action_dispatch/http/headers'
require 'sinatra/base'
require 'sinatra/multi_route'
require 'sprockets'

raise 'Missing environment variable: `IDOBATA_HOOK_URL` is required.' unless ENV['IDOBATA_HOOK_URL']

module Idobata::Hook
  configure do |config|
    config.image_root = '/assets'
    config.image_host = ENV['IDOBATA_HOOK_HOST']
  end

  class Application < Sinatra::Application
    register Sinatra::MultiRoute

    set :idobta_hook_url, ENV['IDOBATA_HOOK_URL']

    set :sprockets, Sprockets::Environment.new

    configure do
      sprockets.append_path Idobata::Hook.root.join('hooks')
    end

    get '/' do
      'hi from Idobata::Hooks.'
    end

    route :get, :post, '/:identifier' do
      hook = Idobata::Hook.find(params[:identifier])

      raw_body = env['rack.input'].read

      headers = ActionDispatch::Http::Headers.new(env)

      payload = hook.new(raw_body, headers).process_payload

      post_to_idobata payload

      'OK'
    end

    private

    def post_to_idobata(payload)
      url  = URI(settings.idobta_hook_url)
      post = Net::HTTP::Post.new(url)

      post.set_form generate_form(payload), 'multipart/form-data'

      Net::HTTP.start(url.hostname, url.port, use_ssl: url.scheme == 'https') do |http|
        http.request post
      end
    end

    def generate_form(payload)
      images = Array(payload[:images]).map {|image|
        ['image[]', image.tempfile, filename: image.filename, content_type: image.type]
      }

      [
        ['source', payload[:source].to_s],
        ['format', payload[:format].to_s],
        *images
      ]
    end
  end
end

{'fraction_non_alphanumeric': 0.12560386473429952, 'fraction_numerical': 0.0, 'mean_word_length': 3.2474358974358974, 'pattern_counts': {'":': 0, '<': 11, '<?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': '5942535', 'n_tokens_mistral': 1130, 'n_tokens_neox': 1097, 'n_words': 223}