Class: HTTPAdapter::TyphoeusRequestAdapter

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(request) ⇒ TyphoeusRequestAdapter

Returns a new instance of TyphoeusRequestAdapter.



23
24
25
26
27
28
# File 'lib/httpadapter/adapters/typhoeus.rb', line 23

def initialize(request)
  unless request.kind_of?(Typhoeus::Request)
    raise TypeError, "Expected Typhoeus::Request, got #{request.class}."
  end
  @request = request
end

Class Method Details

.from_ary(array) ⇒ 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 self.from_ary(array)
  method, uri, headers, body = array
  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

.transmit(request, connection = nil) ⇒ Object



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

def self.transmit(request, connection=nil)
  method, uri, headers, body = request
  uri = Addressable::URI.parse(uri)
  typhoeus_request = self.from_ary([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 TyphoeusResponseAdapter.new(typhoeus_response).to_ary
end

Instance Method Details

#to_aryObject



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

def to_ary
  method = @request.method.to_s.upcase
  uri = @request.url.to_str
  headers = []
  @request.headers.each do |header, value|
    headers << [header, value]
  end
  body = @request.body || ""
  return [method, uri, headers, [body]]
end