<commit_before>require 'net/https'
require 'uri'
require 'rack/utils'

module Typekit
  class Connection
    METHODS = [ :get, :post, :delete ]

    def initialize options
      @token = options[:token] || ''
      raise ArgumentError, 'The API token is required.' if @token.empty?
    end

    METHODS.each do |method|
      define_method(method) do |uri, parameters = {}|
        request method, uri, parameters
      end
    end

    protected

    def request method, uri, parameters = {}
      unless parameters.empty?
        uri = "#{ uri }?#{ Rack::Utils.build_nested_query parameters }"
      end

      klass = Net::HTTP.const_get method.to_s.capitalize
      http_request = klass.new URI(uri)
      http_request['X-Typekit-Token'] = @token

      http = Net::HTTP.new http_request.uri.host, http_request.uri.port
      http.use_ssl = true

      response = http.request http_request
      Response.new code: response.code.to_i, content: response.body
    rescue SocketError
      raise SocketError, 'Unable to connect to Typekit’s API.'
    end
  end
end<commit_msg>Made Connection satisfy the spec (a workaround for a bug in Rack::Utils.build_nested_query)<commit_after>require 'net/https'
require 'uri'
require 'rack/utils'

module Typekit
  class Connection
    METHODS = [ :get, :post, :delete ]

    def initialize options
      @token = options[:token] || ''
      raise ArgumentError, 'The API token is required.' if @token.empty?
    end

    METHODS.each do |method|
      define_method(method) do |uri, parameters = {}|
        request method, uri, parameters
      end
    end

    protected

    def request method, uri, parameters = {}
      unless parameters.empty?
        parameters = self.class.prepare_parameters parameters
        uri = "#{ uri }?#{ Rack::Utils.build_nested_query parameters }"
      end

      klass = Net::HTTP.const_get method.to_s.capitalize
      http_request = klass.new URI(uri)
      http_request['X-Typekit-Token'] = @token

      http = Net::HTTP.new http_request.uri.host, http_request.uri.port
      http.use_ssl = true

      response = http.request http_request
      Response.new code: response.code.to_i, content: response.body
    rescue SocketError
      raise SocketError, 'Unable to connect to Typekit’s API.'
    end

    def self.prepare_parameters parameters
      #
      # FIXME: https://github.com/rack/rack/issues/557
      #
      Hash[
        parameters.map do |key, value|
          case value
          when Integer, TrueClass, FalseClass
            [ key, value.to_s ]
          when Hash
            [ key, prepare_parameters(value) ]
          else
            [ key, value ]
          end
        end
      ]
    end
  end
end
{'fraction_non_alphanumeric': 0.0970479704797048, 'fraction_numerical': 0.001107011070110701, 'mean_word_length': 2.4756410256410257, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 1, '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': '2851696', 'n_tokens_mistral': 815, 'n_tokens_neox': 789, 'n_words': 221}