class Shippo::API::Transformers::List
List
Transformer: if it finds a key mapped to an array, this transformer coerces each element into an appropriate model class – if available. As a result, return from, eg. Shipment.create
will contain rates
key that will no longer be an array of hashes, but an array of Shippo::Rate
instances.
Constants
- MATCHERS
MATCHERS
contains a list of procs that are used to try, in order, to convert a key mapped to an array of hashes, into a word that represents an existing model.Each matcher receives a key as a parameter, and (if matches) it extracts the candidate word to be attempted to
constantize
. For example,rates
matcher will returnrates
as output.
Attributes
h[RW]
Public Class Methods
new(hash)
click to toggle source
# File lib/shippo/api/transformers/list.rb, line 39 def initialize(hash) self.h = hash end
Public Instance Methods
transform()
click to toggle source
# File lib/shippo/api/transformers/list.rb, line 43 def transform h.keys.each { |k| h[k].is_a?(Array) && !h[k].empty? }.each do |list_key| type, *values = transform_list(list_key, h[list_key]) h[list_key] = values if type end end
Private Instance Methods
detect_type_class(model_name)
click to toggle source
# File lib/shippo/api/transformers/list.rb, line 52 def detect_type_class(model_name) type = model_name.to_s.singularize.camelize "Shippo::#{type}".constantize rescue nil end
detect_type_name(list_key)
click to toggle source
# File lib/shippo/api/transformers/list.rb, line 57 def detect_type_name(list_key) results = MATCHERS.map { |m| m.call(list_key) }.compact results.is_a?(Array) && results.size > 0 ? results.first : nil end
transform_list(list_key, array)
click to toggle source
# File lib/shippo/api/transformers/list.rb, line 62 def transform_list(list_key, array) if (type_name = detect_type_name(list_key)) && (type_class = detect_type_class(type_name)) type_array = array.map { |item| type_class.from(item) } return type_name, *type_array else nil end end