class ApiModel::Response

Constants

FALL_THROUGH_METHODS

Attributes

http_response[RW]
objects[RW]

Public Class Methods

new(http_response, config) click to toggle source
# File lib/api_model/response.rb, line 12
def initialize(http_response, config)
  @http_response = http_response
  @_config = config || Configuration.new
end

Public Instance Methods

build(builder, hash) click to toggle source
# File lib/api_model/response.rb, line 34
def build(builder, hash)
  if builder.respond_to? :build
    if builder.method(:build).arity == 2
      builder.build self, hash
    else
      builder.build hash
    end
  else
    builder.new hash
  end
end
build_objects() click to toggle source
# File lib/api_model/response.rb, line 21
def build_objects
  handle_response_errors
  return self if response_body.nil?

  if response_build_hash.is_a? Array
    self.objects = response_build_hash.collect{ |hash| build http_response.builder, hash }
  else
    self.objects = self.build http_response.builder, response_build_hash
  end

  self
end
fetch_from_body(key_reference) click to toggle source

Uses a string notation split by colons to fetch nested keys from a hash. For example, if you have a hash which looks like:

{ foo: { bar: { baz: "Hello world" } } }

Then calling ++fetch_from_body(“foo.bar.baz”)++ would return “Hello world”

# File lib/api_model/response.rb, line 96
def fetch_from_body(key_reference)
  key_reference.split(".").inject(response_body) do |hash,key|
    begin
      hash.fetch(key, nil)
    rescue NoMethodError
      Log.error "Could not set #{key_reference} on #{hash}"
    end
  end
end
metadata() click to toggle source
# File lib/api_model/response.rb, line 17
def metadata
  @metadata ||= OpenStruct.new
end
method_missing(method_name, *args, &block) click to toggle source

Pass though any method which is not defined to the built objects. This makes the response class quite transparent, and keeps the response acting like the built object, or array of objects.

# File lib/api_model/response.rb, line 86
def method_missing(method_name, *args, &block)
  objects.send method_name, *args, &block
end
response_body() click to toggle source
# File lib/api_model/response.rb, line 46
def response_body
  return @response_body if @response_body.present?

  if @_config.parser.method(:parse).arity == 2
    @response_body = @_config.parser.parse self, http_response.api_call.body
  else
    @response_body = @_config.parser.parse http_response.api_call.body
  end
end
response_build_hash() click to toggle source

If the model config defines a json root, use it on the response_body to dig down in to the hash.

# File lib/api_model/response.rb, line 108
def response_build_hash
  if @_config.json_root.present?
    begin
      fetch_from_body @_config.json_root
    rescue
      raise ResponseBuilderError, "Could not find key #{@_config.json_root} in:\n#{response_body}"
    end
  else
    response_body
  end
end
response_cookies() click to toggle source
# File lib/api_model/response.rb, line 60
def response_cookies
  return @cookies if @cookies.present?
  jar = HTTP::CookieJar.new

  set_cookie = http_response.api_call.headers_hash["Set-Cookie"]
  set_cookie = set_cookie.split(", ") unless set_cookie.is_a?(Array)

  set_cookie.each do |cookie|
    jar.parse cookie, http_response.api_call.request.base_url
  end

  @cookies = jar.cookies
end
successful?() click to toggle source
# File lib/api_model/response.rb, line 56
def successful?
  http_response.api_call.success?
end

Private Instance Methods

handle_response_errors() click to toggle source
# File lib/api_model/response.rb, line 122
def handle_response_errors
  if @_config.raise_on_unauthenticated && http_response.api_call.response_code == 401
    raise UnauthenticatedError, @_config.error_message_formatter.format(http_response)
  end

  if @_config.raise_on_not_found && http_response.api_call.response_code == 404
    raise NotFoundError, @_config.error_message_formatter.format(http_response)
  end

  if @_config.raise_on_server_error && http_response.api_call.response_code.between?(500, 599)
    raise ServerError, @_config.error_message_formatter.format(http_response)
  end
end