Class: HomeAway::API::Adapters::FaradayAdapter

Inherits:
Object
  • Object
show all
Defined in:
lib/homeaway/api/adapters/faraday.rb

Class Method Summary collapse

Class Method Details

.call(site, opts, headers, method, uri, body, params) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/homeaway/api/adapters/faraday.rb', line 30

def self.call(site, opts, headers, method, uri, body, params)
  agent = Faraday::Connection.new(site, opts) do |conn|
    conn.headers = headers
    conn.adapter Faraday.default_adapter
    conn.options.params_encoder = Faraday::FlatParamsEncoder
    conn.use Faraday::HttpCache
    conn.use FaradayMiddleware::Mashify, :mash_class => HomeAway::API::Response
    conn.use FaradayMiddleware::ParseJson
    conn.use FaradayMiddleware::Instrumentation
    conn.use FaradayMiddleware::FollowRedirects
  end
  response = agent.send(method, uri) do |req|
    req.body = body.to_json unless body.empty?
    req.params = params unless params.empty?
  end
  self.transform response
end

.transform(response) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/homeaway/api/adapters/faraday.rb', line 49

def self.transform(response)
  mash = response.body
  mash = HomeAway::API::Response.new unless mash
  mash..headers = HomeAway::API::Response.new(response.headers.to_hash) if response.respond_to? :headers
  if response.respond_to? :status
    mash..status_code = response.status
    if mash..status_code.to_i >= 400
      raise (HomeAway::API::Errors.for_http_code mash..status_code).new(mash)
    end
  end
  if response.respond_to? :env
    mash..request_headers = HomeAway::API::Response.new(response.env.request_headers.to_hash) if response.env.respond_to? :request_headers
  end
  mash
end