<commit_before>require 'json'

require 'yaml'

require 'nanite'
require 'nanite/mapper'

module Nanite
  class RackMapper

    HEADER = {
      'Content-Type' => 'text/plain'
    }.freeze

    BODY = "rack.input".freeze
    PATH = "REQUEST_PATH".freeze
    TYPE = "CONTENT_TYPE".freeze
    METHOD = "REQUEST_METHOD".freeze

    # Initialize the mapper, takes a set of options that get passed to the
    # mapper directly.
    def initialize(config_file)
      # Load the config file and pass it into the mapper
      @config = config_file ? YAML.load_file(config_file) : {}

      # Initialize the mapper when EventMachine has started
      EM.next_tick do
        Nanite.start_mapper(@config)
      end
    end

    # Call has a couple requirements for a proper message
    #   1. Content-Type must be application/json
    #   2. HTTP Method must be put
    #   3. Body must be well formatted JSON
    #   4. Body cannot be empty
    def call(env)
      # Check content-type
      if(env[TYPE] != 'application/json')
        raise 'content-type error'
      end

      # Check method
      if(env[METHOD].downcase != 'put')
        raise 'method error'
      end

      success = Nanite.push(env[PATH], JSON.parse(env[BODY].read))

      [200, HEADER.dup, "#{success}"]
    rescue JSON::ParserError
      [400, HEADER.dup, 'invalid json']
    rescue => e
      [400, HEADER.dup, e.message]
    end
  end
end<commit_msg>Add support for passing options to Nanite.<commit_after>require 'json'

require 'yaml'

require 'nanite'
require 'nanite/mapper'

module Nanite
  class RackMapper

    HEADER = {
      'Content-Type' => 'text/plain'
    }.freeze

    BODY = "rack.input".freeze
    PATH = "REQUEST_PATH".freeze
    TYPE = "CONTENT_TYPE".freeze
    METHOD = "REQUEST_METHOD".freeze

    # Initialize the mapper, takes a set of options that get passed to the
    # mapper directly.
    def initialize(config_file)
      # Load the config file and pass it into the mapper
      @config = config_file ? YAML.load_file(config_file) : {}

      # Initialize the mapper when EventMachine has started
      EM.next_tick do
        Nanite.start_mapper(@config)
      end
    end

    # Call has a couple requirements for a proper message
    #   1. Content-Type must be application/json
    #   2. HTTP Method must be put
    #   3. Body must be well formatted JSON
    #   4. Body cannot be empty
    def call(env)
      # Check content-type
      if(env[TYPE] != 'application/json')
        raise 'content-type error'
      end

      # Check method
      if(env[METHOD].downcase != 'put')
        raise 'method error'
      end

      request = JSON.parse(env[BODY].read)

      options = request['options'] || {}
      payload = request['payload']

      # Convert the options to their symbol equivalents (pull from symbolize_keys in Rails)
      options.replace(
        options.inject({}) do |memo, (key, value)|
          memo[(key.to_sym rescue key) || key] = value
          memo
        end
      )

      success = Nanite.push(env[PATH], payload, options)

      [200, HEADER.dup, "#{success}"]
    rescue JSON::ParserError
      [400, HEADER.dup, 'invalid json']
    rescue => e
      [400, HEADER.dup, e.message]
    end
  end
end
{'fraction_non_alphanumeric': 0.1007105344454742, 'fraction_numerical': 0.008032128514056224, 'mean_word_length': 2.4192185850052796, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, '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': '2455359', 'n_tokens_mistral': 1037, 'n_tokens_neox': 982, 'n_words': 311}