Class: HomeAway::API::Adapters::HurleyAdapter

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

Class Method Summary collapse

Class Method Details

.__update_opts(struct, opts) ⇒ Object



38
39
40
41
42
43
44
45
# File 'lib/homeaway/api/adapters/hurley.rb', line 38

def self.__update_opts(struct, opts)
  opts.each_pair do |k, v|
    method = "#{k}=".to_sym
    if struct.respond_to?(method)
      struct.send(method, v)
    end
  end
end

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



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

def self.call(site, opts, headers, method, uri, body, params)
  begin
    agent = Hurley::Client.new(site)
    agent.header.update(headers)
    self.__update_opts(agent.request_options, opts)
    self.__update_opts(agent.ssl_options, opts)
    request = agent.request(method, uri)
    request.query.update(params) unless params.empty?
    request.body = body.to_json unless (!body.respond_to?(:to_json) || body.empty?)
    response = agent.call(request)
  rescue => e
    raise HomeAway::API::Errors::HomeAwayAPIError.new(e.message.to_s)
  end
  response_to_mash(response)
end

.response_to_mash(response) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/homeaway/api/adapters/hurley.rb', line 65

def self.response_to_mash(response)
  body = response.body ||= {}
  mash = HomeAway::API::Response.new
  mash.update JSON.parse(body) unless body.empty?
  mash. = HomeAway::API::Response.new
  mash..headers = response.header.to_hash if response.respond_to? :header
  if response.respond_to? :status_code
    mash..status_code = response.status_code
    if mash..status_code.to_i >= 400
      raise (HomeAway::API::Errors.for_http_code mash..status_code).new(mash)
    end
  end
  mash.delete(:_metadata) if mash..empty?
  mash
end