class Dialers::Transformable

This class provide some ways to transform a reponse into an object or a set of objects based on what objects the user of this class wants to provide.

Attributes

raw_data[RW]
response[RW]

Public Class Methods

new(response) click to toggle source
# File lib/dialers/transformable.rb, line 7
def initialize(response)
  self.response = response
  self.raw_data = response.body
end

Public Instance Methods

as_received() click to toggle source

Returns the raw body of the request. @return [Hash, Array] the raw body of the request.

# File lib/dialers/transformable.rb, line 50
def as_received
  raw_data
end
transform_to_many(entity_class_or_decider, root: nil) click to toggle source

Transforms a response into many objects based on the response’s body.

@param entity_class_or_decider [Hash, Class] A class or a hash of Fixnum => Class

to decide with the status.

@param root [String, nil] The root to use to get an array from the body.

@return [Array<Object>] The result of the transformation.

# File lib/dialers/transformable.rb, line 40
def transform_to_many(entity_class_or_decider, root: nil)
  items = get_rooted_items(root)
  unless items.is_a?(Array)
    fail Dialers::ImpossibleTranformationError.new(response)
  end
  items.map { |item| transform_attributes_to_object(entity_class_or_decider, item) }
end
transform_to_one(entity_class_or_decider, root: nil) click to toggle source

Transforms a response into one object based on the response’s body.

If you pass a hash like this:

{ 200 => ProductCreated, 202 => ProductAccepted }

the transformation will decide which object to create based on the status.

It’s important to note that the class must provide attribute writers for this method to be able to fill an object with the responses’s body data.

@param entity_class_or_decider [Hash, Class] A class or a hash of Fixnum => Class

to decide with the status.

@param root [String, nil] The root to use to get the object from the body.

@return [Object] The result of the transformation.

# File lib/dialers/transformable.rb, line 28
def transform_to_one(entity_class_or_decider, root: nil)
  object = root ? raw_data[root] : raw_data
  transform_attributes_to_object(entity_class_or_decider, object)
end

Private Instance Methods

decide_entity_class(entity_class_or_decider) click to toggle source
# File lib/dialers/transformable.rb, line 68
def decide_entity_class(entity_class_or_decider)
  case entity_class_or_decider
  when Class
    entity_class_or_decider
  when Hash
    entity_class_or_decider[response.status]
  end
end
get_rooted_items(root) click to toggle source
# File lib/dialers/transformable.rb, line 77
def get_rooted_items(root)
  if root.nil?
    raw_data
  elsif raw_data.is_a?(Array)
    raw_data
  else
    raw_data[root]
  end
end
transform_attributes_to_object(entity_class_or_decider, attributes) click to toggle source
# File lib/dialers/transformable.rb, line 59
def transform_attributes_to_object(entity_class_or_decider, attributes)
  entity_class = decide_entity_class(entity_class_or_decider)
  if entity_class.nil?
    fail Dialers::ResponseError.new(response)
  else
    Dialers::AssignAttributes.call(entity_class.new, attributes)
  end
end