Class: Rack::Client::Handler::Excon

Inherits:
Object
  • Object
show all
Includes:
DualBand
Defined in:
lib/rack/client/handler/excon.rb

Instance Method Summary collapse

Methods included from DualBand

#call

Constructor Details

#initialize(url) ⇒ Excon

Returns a new instance of Excon.



9
10
11
# File 'lib/rack/client/handler/excon.rb', line 9

def initialize(url)
  @uri = URI.parse(url)
end

Instance Method Details

#async_call(env) ⇒ Object



13
14
15
# File 'lib/rack/client/handler/excon.rb', line 13

def async_call(env)
  raise("Asynchronous API is not supported for EmHttp Handler") unless block_given?
end

#connectionObject



40
41
42
# File 'lib/rack/client/handler/excon.rb', line 40

def connection
  connection_table[self] ||= ::Excon.new(@uri.to_s)
end

#connection_tableObject



44
45
46
# File 'lib/rack/client/handler/excon.rb', line 44

def connection_table
  Thread.current[:_rack_client_excon_connections] ||= {}
end

#parse(excon_response) ⇒ Object



35
36
37
38
# File 'lib/rack/client/handler/excon.rb', line 35

def parse(excon_response)
  body = excon_response.body.empty? ? [] : StringIO.new(excon_response.body)
  Response.new(excon_response.status, Headers.new(excon_response.headers).to_http, body)
end

#sync_call(env) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/rack/client/handler/excon.rb', line 17

def sync_call(env)
  request = Rack::Request.new(env)

  body = case request.body
         when StringIO then request.body.string
         when IO       then request.body.read
         when Array    then request.body.to_s
         when String   then request.body
         end

  response = parse connection.request(:method   => request.request_method,
                                      :path     => request.path,
                                      :headers  => Headers.from(env).to_http,
                                      :body     => body)

  response.finish
end