Sha256: de6f93dc006dc8a49b92cc87d790c800ca9c001770e5cb92ef4d15e5bae94401

Contents?: true

Size: 1.82 KB

Versions: 2

Compression:

Stored size: 1.82 KB

Contents

# encoding: utf-8
require 'ting_yun/http/abstract_request'

module TingYun
  module Http

  class TyphoeusHTTPResponse
        def initialize(response)
          @response = response
        end

        def [](key)
          unless headers.nil?
            result = headers[key]

            # Typhoeus 0.5.3 has a bug where asking the headers hash for a
            # non-existent header will return the hash itself, not what we want.
            result == headers ? nil : result
          end
        end

        def to_hash
          hash = {}
          headers.each do |(k,v)|
            hash[k] = v
          end
          hash
        end

        private

        def headers
          @response.headers
        end
      end

      class TyphoeusHTTPRequest < AbstractRequest
        def initialize(request)
          @request = request
          @uri = case request.url
            when ::URI then request.url
            else TingYun::Agent::HTTPClients::URIUtil.parse_and_normalize_url(request.url)
            end
        end

        TYPHOEUS = "Typhoeus".freeze

        def type
          TYPHOEUS
        end
        def from
          "typhoeus%2Fhttp"
        end

        LHOST = 'host'.freeze
        UHOST = 'Host'.freeze

        def host_from_header
          self[LHOST] || self[UHOST]
        end

        def host
          host_from_header || @uri.host
        end

        GET = 'GET'.freeze

        def method
          (@request.options[:method] || GET).to_s.upcase
        end

        def [](key)
          return nil unless @request.options && @request.options[:headers]
          @request.options[:headers][key]
        end

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

        def uri
          @uri
        end
      end
    end

end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
tingyun_rpm-1.6.1 lib/ting_yun/http/typhoeus_wrappers.rb
tingyun_rpm-1.5.0 lib/ting_yun/http/typhoeus_wrappers.rb