class Fixer::Response

Attributes

raw[RW]
request[RW]

Public Class Methods

new(response, request={}) click to toggle source
# File lib/fixer/response.rb, line 6
def initialize(response, request={})
  @raw     = response
  @request = request

  check_for_error(response)
end

Public Instance Methods

[](key) click to toggle source
# File lib/fixer/response.rb, line 33
def [](key)
  if self.object.is_a?(Array) || self.object.is_a?(Hash)
    self.object[key]
  else
    self.object.send(:"#{key}")
  end
end
body() click to toggle source
# File lib/fixer/response.rb, line 25
def body
  self.raw.body
end
check_for_error(response) click to toggle source
# File lib/fixer/response.rb, line 13
def check_for_error(response)
  status_code_type = response.status.to_s[0]
  case status_code_type
  when "2"
    # puts "all is well, status: #{response.status}"
  when "4", "5"
    raise "Whoops, error back from Fixer: #{response.status}"
  else
    raise "Unrecognized status code: #{response.status}"
  end
end
has_key?(key) click to toggle source
# File lib/fixer/response.rb, line 41
def has_key?(key)
  self.object.is_a?(Hash) && self.object.has_key?(key)
end
method_missing(method_name, *args, &block) click to toggle source

Coerce any method calls for body attributes

Calls superclass method
# File lib/fixer/response.rb, line 47
def method_missing(method_name, *args, &block)
  if self.has_key?(method_name.to_s)
    self.[](method_name, &block)
  elsif self.body.respond_to?(method_name)
    self.body.send(method_name, *args, &block)
  elsif self.request[:api].respond_to?(method_name)
    self.request[:api].send(method_name, *args, &block)
  else
    super
  end
end
object() click to toggle source
# File lib/fixer/response.rb, line 29
def object
  body
end