Sha256: 23dc8763bd0c0f9506c8219d8ae9506ceb771ae2b560af90a74f67894689273a

Contents?: true

Size: 1.86 KB

Versions: 3

Compression:

Stored size: 1.86 KB

Contents

# encoding: utf-8

module OneApm
  module Support
    module HTTPClients
      class ExconHTTPResponse
        def initialize(response)
          @response = response
          # Since HTTP headers are case-insensitive, we normalize all of them to
          # upper case here, and then also in our [](key) implementation.
          @normalized_headers = {}
          headers = response.respond_to?(:headers) ? response.headers : response[:headers]
          (headers || {}).each do |key, val|
            @normalized_headers[key.upcase] = val
          end
        end

        def [](key)
          @normalized_headers[key.upcase]
        end

        def to_hash
          @normalized_headers.dup
        end
      end

      class ExconHTTPRequest
        def initialize(datum)
          @datum = datum
        end

        def type
          "Excon"
        end

        def host
          if hostname = (self['host'] || self['Host'])
            hostname.split(':').first
          else
            @datum[:host]
          end
        end

        def port
          if hostname = (self['host'] || self['Host'])
            hostname.split(':').last
          else
            @datum[:port]
          end
        end


        def method
          @datum[:method].to_s.upcase
        end

        def [](key)
          @datum[:headers][key]
        end

        def []=(key, value)
          @datum[:headers] ||= {}
          @datum[:headers][key] = value
        end

        def uri
          URI.parse("#{@datum[:scheme]}://#{@datum[:host]}:#{@datum[:port]}#{@datum[:path]}")
        end

        def query
          uri.query.nil?? {} : CGI.parse(uri.query)
        end

        def body
          @datum[:body]
        end

        def post_params
          body.nil?? {} :  CGI.parse(body)
        end

        def params
          post_params.merge(query)
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
oneapm_rpm-1.4.2 lib/one_apm/support/http_clients/excon_wrappers.rb
oneapm_rpm-1.4.1 lib/one_apm/support/http_clients/excon_wrappers.rb
oneapm_rpm-1.4.0 lib/one_apm/support/http_clients/excon_wrappers.rb