<commit_before>module CrowdFlower
  @@key = nil
  @@domain = nil

  class UsageError < StandardError ; end

  class APIError < StandardError
    attr_reader :details

    def initialize(hash)
      @details = hash
      super @details.inspect
    end
  end
  
  # a convenience method for backward compatibility
  def self.connect!(key, domain_base = "https://api.crowdflower.com", version = 1)
    Base.connect!(key, domain_base, version)
  end

  def self.connect_domain!(key, domain_base, version = 1)
    Base.connect_domain!(key, domain_base, version)
  end

  def self.connect_config!(opts)
    Base.connect_config!(opts)
  end
  
  # an object that stores connection details; does actual http talking
  class Connection
    include HTTParty
    headers "accept" => "application/json"
    format :json
    
    attr_reader :key, :domain, :version, :domain_base, :ssl_port, :public_port
    
    def initialize(key, domain_base, version, ssl_port = 443, public_port = 80)
      @domain_base = domain_base
      @version = version
      @key = key
      @ssl_port = ssl_port
      @public_port = public_port
      port = @domain_base =~ /^https/ ? @ssl_port : @public_port
      @domain = "#{@domain_base}:#{port}/v#{version}"
      begin # pass yaml file
        key = YAML.load_file(key)
        @key = key[:key] || key["key"]
      rescue # pass key
        @key = key
      end
    end

    # get, post, put and delete methods
    def method_missing(method_id, *args)
      if [:get, :post, :put, :delete].include?(method_id)
        path, options = *args
        options ||= {}
        options[:query] = (default_params.merge(options[:query] || {}))
        options[:headers] = (self.class.default_options[:headers].merge(options[:headers] || {}))
        self.class.send(method_id, url(path), options)
      else
        super
      end
    end

    # Returns the base crowdflower domain from the api url's domain.
    #
    # @api public
    # @return [String] the base crowdflower domain
    # @example<commit_msg>Revert "changing domain_base to add correct port based on passed in protocol"<commit_after>module CrowdFlower
  @@key = nil
  @@domain = nil

  class UsageError < StandardError ; end

  class APIError < StandardError
    attr_reader :details

    def initialize(hash)
      @details = hash
      super @details.inspect
    end
  end
  
  # a convenience method for backward compatibility
  def self.connect!(key, domain_base = "https://api.crowdflower.com", version = 1)
    Base.connect!(key, domain_base, version)
  end

  def self.connect_domain!(key, domain_base, version = 1)
    Base.connect_domain!(key, domain_base, version)
  end

  def self.connect_config!(opts)
    Base.connect_config!(opts)
  end
  
  # an object that stores connection details; does actual http talking
  class Connection
    include HTTParty
    headers "accept" => "application/json"
    format :json
    
    attr_reader :key, :domain, :version, :domain_base, :ssl_port, :public_port
    
    def initialize(key, domain_base, version, ssl_port = 443, public_port = 80)
      @domain_base = domain_base
      @version = version
      @domain = "#{@domain_base}/v#{version}"
      @key = key
      @ssl_port = ssl_port
      @public_port = public_port
      begin # pass yaml file
        key = YAML.load_file(key)
        @key = key[:key] || key["key"]
      rescue # pass key
        @key = key
      end
    end

    # get, post, put and delete methods
    def method_missing(method_id, *args)
      if [:get, :post, :put, :delete].include?(method_id)
        path, options = *args
        options ||= {}
        options[:query] = (default_params.merge(options[:query] || {}))
        options[:headers] = (self.class.default_options[:headers].merge(options[:headers] || {}))
        self.class.send(method_id, url(path), options)
      else
        super
      end
    end

    # Returns the base crowdflower domain from the api url's domain.
    #
    # @api public
    # @return [String] the base crowdflower domain
    # @example
{'fraction_non_alphanumeric': 0.11155082859262924, 'fraction_numerical': 0.0034627751669552313, 'mean_word_length': 2.847764034253092, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 5, 'https://': 2, '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': '776750', 'n_tokens_mistral': 1271, 'n_tokens_neox': 1233, 'n_words': 339}