<commit_before>require 'faraday/localhost_header'

module TicketEvolution
  class Connection < Base
    cattr_reader :default_options, :expected_options, :oldest_version_in_service
    cattr_accessor :protocol, :url_base, :adapter

    @@oldest_version_in_service = 8

    @@default_options = HashWithIndifferentAccess.new({
      :version => @@oldest_version_in_service,
      :mode => :sandbox,
      :ssl_verify => true,
      :logger => nil
    })

    @@expected_options = [
      'version',
      'mode',
      'token',
      'secret',
      'ssl_verify',
      'logger'
    ]

    @@url_base = "ticketevolution.com"
    @@protocol = "https"

    @@adapter = :net_http

    def initialize(opts = {})
      @adapter = self.class.adapter
      @config = self.class.default_options.merge(opts)
      @config.delete_if{|k, v| ! TicketEvolution::Connection.expected_options.include?(k)}

      # Error Notification
      if @config.keys.sort_by{|x|x} == TicketEvolution::Connection.expected_options.sort_by{|x|x}
        raise InvalidConfiguration.new("Invalid Token Format") unless @config[:token] =~ /^[a-zA-Z0-9]{32}$/
        raise InvalidConfiguration.new("Invalid Secret Format") unless @config[:secret] =~ /^\S{40}$/
        raise InvalidConfiguration.new("Please Use API Version #{TicketEvolution::Connection.oldest_version_in_service} or Above") unless @config[:version] >= TicketEvolution::Connection.oldest_version_in_service
      else
        raise InvalidConfiguration.new("Missing: #{(self.class.expected_options - @config.keys).join(', ')}")
      end
    end

    def url
      @url ||= [].tap do |parts|
        parts << TicketEvolution::Connection.protocol
        parts << "://api."
        parts << "#{@config[:mode]}." if @config[:mode].present? && @config[:mode].to_sym != :production
        parts << TicketEvolution::Connection.url_base
      end.join
    end

    def uri(path)
      parts = [].tap do |parts|
        parts << self.url
        parts << "/v#{@config[:version]}" if @config[:version] > 8
        parts << path
      end.join
    end

    def sign(method, path, content = nil)
      d = "#{method} #{process_params(method, path, content).gsub(TicketEvolution::Connection.protocol+'://', '').gsub(/\:\d{2,5}\//, '/')}"
      Base64.encode64(
        OpenSSL::HMAC.digest(
          OpenSSL::Digest::Digest.new('sha256'),
          @config[:secret],
          d
      )).chomp
    end

    def build_request(method, path, params = nil)
      options = {
        :headers => {
          "X-Signature" => sign(method, self.uri(path), params),
          "X-Token" => @config[:token]
        },
        :ssl => {
          :verify => @config[:ssl_verify]
        }
      }
      options[:params] = params if method == :GET
      options[:headers]["Accept"] = "application/vnd.ticketevolution.api+json; version=#{@config[:version]}" unless @config[:version] > 8
      Faraday.new(self.uri(path), options) do |builder|
        builder.use Faraday::Response::VerboseLogger, self.logger if self.logger.present?
        builder.use FaradayLocalhostHeader, [ /(?:^|\.)lvh\.me$/i, /(?:^|\.)((?:\d+\.){4})xip\.io$/i ]
        builder.adapter @adapter
      end
    end

    def logger
      @config[:logger]
    end

    private

    def post_body(params)
      MultiJson.encode(params)
    end

    def process_params(method, uri, params)
      "#{uri}?#{if params.present?
        case method
        when :GET
          Faraday::Utils.build_nested_query(params)
        else
          post_body(params)
        end
      end}"
    end
  end
end
<commit_msg>Up the Faraday timeout to 60 seconds (default was 25)<commit_after>require 'faraday/localhost_header'

module TicketEvolution
  class Connection < Base
    cattr_reader :default_options, :expected_options, :oldest_version_in_service
    cattr_accessor :protocol, :url_base, :adapter

    @@oldest_version_in_service = 8

    @@default_options = HashWithIndifferentAccess.new({
      :version => @@oldest_version_in_service,
      :mode => :sandbox,
      :ssl_verify => true,
      :logger => nil
    })

    @@expected_options = [
      'version',
      'mode',
      'token',
      'secret',
      'ssl_verify',
      'logger'
    ]

    @@url_base = "ticketevolution.com"
    @@protocol = "https"

    @@adapter = :net_http

    def initialize(opts = {})
      @adapter = self.class.adapter
      @config = self.class.default_options.merge(opts)
      @config.delete_if{|k, v| ! TicketEvolution::Connection.expected_options.include?(k)}

      # Error Notification
      if @config.keys.sort_by{|x|x} == TicketEvolution::Connection.expected_options.sort_by{|x|x}
        raise InvalidConfiguration.new("Invalid Token Format") unless @config[:token] =~ /^[<KEY>
        raise InvalidConfiguration.new("Invalid Secret Format") unless @config[:secret] =~ /^\S{40}$/
        raise InvalidConfiguration.new("Please Use API Version #{TicketEvolution::Connection.oldest_version_in_service} or Above") unless @config[:version] >= TicketEvolution::Connection.oldest_version_in_service
      else
        raise InvalidConfiguration.new("Missing: #{(self.class.expected_options - @config.keys).join(', ')}")
      end
    end

    def url
      @url ||= [].tap do |parts|
        parts << TicketEvolution::Connection.protocol
        parts << "://api."
        parts << "#{@config[:mode]}." if @config[:mode].present? && @config[:mode].to_sym != :production
        parts << TicketEvolution::Connection.url_base
      end.join
    end

    def uri(path)
      parts = [].tap do |parts|
        parts << self.url
        parts << "/v#{@config[:version]}" if @config[:version] > 8
        parts << path
      end.join
    end

    def sign(method, path, content = nil)
      d = "#{method} #{process_params(method, path, content).gsub(TicketEvolution::Connection.protocol+'://', '').gsub(/\:\d{2,5}\//, '/')}"
      Base64.encode64(
        OpenSSL::HMAC.digest(
          OpenSSL::Digest::Digest.new('sha256'),
          @config[:secret],
          d
      )).chomp
    end

    def build_request(method, path, params = nil)
      options = {
        :headers => {
          "X-Signature" => sign(method, self.uri(path), params),
          "X-Token" => @config[:token]
        },
        :ssl => {
          :verify => @config[:ssl_verify]
        },
        :timeout => 60
      }
      options[:params] = params if method == :GET
      options[:headers]["Accept"] = "application/vnd.ticketevolution.api+json; version=#{@config[:version]}" unless @config[:version] > 8
      Faraday.new(self.uri(path), options) do |builder|
        builder.use Faraday::Response::VerboseLogger, self.logger if self.logger.present?
        builder.use FaradayLocalhostHeader, [ /(?:^|\.)lvh\.me$/i, /(?:^|\.)((?:\d+\.){4})xip\.io$/i ]
        builder.adapter @adapter
      end
    end

    def logger
      @config[:logger]
    end

    private

    def post_body(params)
      MultiJson.encode(params)
    end

    def process_params(method, uri, params)
      "#{uri}?#{if params.present?
        case method
        when :GET
          Faraday::Utils.build_nested_query(params)
        else
          post_body(params)
        end
      end}"
    end
  end
end

{'fraction_non_alphanumeric': 0.15331396153313961, 'fraction_numerical': 0.005534800055348001, 'mean_word_length': 2.7161953727506427, 'pattern_counts': {'":': 2, '<': 34, '<?xml version=': 0, '>': 29, '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': '5093892', 'n_tokens_mistral': 2318, 'n_tokens_neox': 2216, 'n_words': 435}