class RESTinPeace::ResponseConverter

Attributes

body[RW]
existing_instance[RW]
klass[RW]

Public Class Methods

new(response, instance_or_class) click to toggle source
# File lib/rest_in_peace/response_converter.rb, line 11
def initialize(response, instance_or_class)
  self.body = response.body

  if instance_or_class.respond_to?(:new)
    self.klass = instance_or_class
    self.existing_instance = new_instance
  else
    self.klass = instance_or_class.class
    self.existing_instance = instance_or_class
  end
end

Public Instance Methods

convert_from_array() click to toggle source
# File lib/rest_in_peace/response_converter.rb, line 38
def convert_from_array
  body.map do |entity|
    convert_from_hash(entity, new_instance)
  end
end
convert_from_hash(entity = body, instance = existing_instance) click to toggle source
# File lib/rest_in_peace/response_converter.rb, line 44
def convert_from_hash(entity = body, instance = existing_instance)
  instance.force_attributes_from_hash entity
  instance
end
new_instance() click to toggle source
# File lib/rest_in_peace/response_converter.rb, line 49
def new_instance
  klass.new
end
result() click to toggle source
# File lib/rest_in_peace/response_converter.rb, line 23
def result
  case body.class.to_s
  when 'Array'
    convert_from_array
  when 'Hash'
    convert_from_hash
  when 'String'
    body
  when 'NilClass'
    nil
  else
    raise UnknownConvertStrategy, body.class
  end
end