Class: RoadForest::HTTP::ExconAdapter

Inherits:
Object
  • Object
show all
Defined in:
lib/roadforest/http/adapters/excon.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url) ⇒ ExconAdapter

Returns a new instance of ExconAdapter.



7
8
9
10
11
# File 'lib/roadforest/http/adapters/excon.rb', line 7

def initialize(url)
  @root_url = Addressable::URI.parse(url)
  @connections = Hash.new{|h,k| h[k] = build_site_connection(k)}
  @connection_defaults = {}
end

Instance Attribute Details

#connection_defaultsObject (readonly)

Returns the value of attribute connection_defaults.



12
13
14
# File 'lib/roadforest/http/adapters/excon.rb', line 12

def connection_defaults
  @connection_defaults
end

Instance Method Details

#build_site_connection(site) ⇒ Object



14
15
16
# File 'lib/roadforest/http/adapters/excon.rb', line 14

def build_site_connection(site)
  Excon.new(site.to_s, @connection_defaults)
end

#do_request(request) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/roadforest/http/adapters/excon.rb', line 27

def do_request(request)
  uri = @root_url.join(request.url)

  connection = site_connection(uri)
  excon_response = connection.request(
    :method => request.method,
    :path => uri.path,
    :headers => {"Host" => uri.host}.merge(request.headers),
    :body => request.body
  )

  response = HTTP::Response.new
  if excon_response.body.is_a? String
    response.body_string = excon_response.body
  else
    response.body = excon_response.body
  end
  response.headers = excon_response.headers
  response.status = excon_response.status

  return response
end

#reset_connectionsObject



18
19
20
# File 'lib/roadforest/http/adapters/excon.rb', line 18

def reset_connections
  @connections.clear
end

#site_connection(uri) ⇒ Object



22
23
24
25
# File 'lib/roadforest/http/adapters/excon.rb', line 22

def site_connection(uri)
  uri = Addressable::URI.parse(uri)
  @connections[uri.normalized_site]
end