Class: HTTPAdapter::TyphoeusAdapter

Inherits:
Object
  • Object
show all
Includes:
HTTPAdapter
Defined in:
lib/httpadapter/adapters/typhoeus.rb

Instance Method Summary collapse

Methods included from HTTPAdapter

#adapt_request, #adapt_response, #specialize_request, #specialize_response, #transmit, verified_request, verified_response

Instance Method Details

#convert_request_from_a(request_ary) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/httpadapter/adapters/typhoeus.rb', line 41

def convert_request_from_a(request_ary)
  method, uri, headers, body = request_ary
  method = method.to_s.downcase.to_sym
  uri = Addressable::URI.parse(uri)
  headers = Hash[headers]
  merged_body = ""
  body.each do |chunk|
    merged_body += chunk
  end
  if merged_body == ''
    merged_body = nil
  end
  request = Typhoeus::Request.new(
    uri.to_str,
    :method => method,
    :headers => headers,
    :body => merged_body
  )
  return request
end

#convert_request_to_a(request_obj) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/httpadapter/adapters/typhoeus.rb', line 26

def convert_request_to_a(request_obj)
  unless request_obj.kind_of?(Typhoeus::Request)
    raise TypeError,
      "Expected Typhoeus::Request, got #{request_obj.class}."
  end
  method = request_obj.method.to_s.upcase
  uri = request_obj.url.to_str
  headers = []
  request_obj.headers.each do |header, value|
    headers << [header, value]
  end
  body = request_obj.body || ""
  return [method, uri, headers, [body]]
end

#convert_response_from_a(request_ary) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/httpadapter/adapters/typhoeus.rb', line 85

def convert_response_from_a(request_ary)
  status, headers, body = request_ary
  status = status.to_i
  merged_body = ""
  body.each do |chunk|
    merged_body += chunk
  end
  response = Typhoeus::Response.new(
    :code => status,
    :headers => headers.inject('') { |a,(h,v)| a << "#{h}: #{v}\r\n"; a },
    :body => merged_body
  )
  return response
end

#convert_response_to_a(response_obj) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/httpadapter/adapters/typhoeus.rb', line 62

def convert_response_to_a(response_obj)
  unless response_obj.kind_of?(Typhoeus::Response)
    raise TypeError,
      "Expected Typhoeus::Response, got #{response_obj.class}."
  end
  status = response_obj.code.to_i
  headers = []
  response_obj.headers_hash.each do |header, value|
    # Eh? Seriously?  This is NOT a header!
    next if header =~ /^HTTP\/\d\.\d \d{3} .+$/
    if value.kind_of?(Array)
      for repeated_header_value in value
        # Header appears multiple times; common for Set-Cookie
        headers << [header, repeated_header_value]
      end
    else
      headers << [header, value]
    end
  end
  body = response_obj.body || ""
  return [status, headers, [body]]
end

#fetch_resource(request_ary, connection = nil) ⇒ Object



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/httpadapter/adapters/typhoeus.rb', line 100

def fetch_resource(request_ary, connection=nil)
  method, uri, headers, body = request_ary
  uri = Addressable::URI.parse(uri)
  typhoeus_request = self.convert_request_from_a(
    [method, uri, headers, body]
  )
  typhoeus_response = nil
  unless connection
    hydra = Typhoeus::Hydra.new
    connection = HTTPAdapter::Connection.new(
      uri.host, uri.inferred_port, hydra,
      :join => [:run, [], nil]
    )
  else
    http = nil
  end
  typhoeus_request.on_complete do |response|
    typhoeus_response = response
  end
  connection.connection.queue(typhoeus_request)
  connection.join
  return self.convert_response_to_a(typhoeus_response)
end