module KOSapiClient::Entity::DataMappings::ClassMethods

Public Instance Methods

attr_mappings() click to toggle source
# File lib/kosapi_client/entity/data_mappings.rb, line 35
def attr_mappings
  if self.superclass.respond_to? :attr_mappings
    parent_mappings = self.superclass.attr_mappings
  end
  (parent_mappings || {}).merge(@data_mappings)
end
map_data(name, type=String, opts = {}) click to toggle source
# File lib/kosapi_client/entity/data_mappings.rb, line 28
def map_data(name, type=String, opts = {})
  attr_accessor name
  opts[:type] = type
  @data_mappings ||= {}
  @data_mappings[name] = opts
end
parse(content, context = {}) click to toggle source

Parses composed domain type from hash response structure.

@param [Hash] content hash structure from API response corresponding to single domain object @return [BaseEntity] parsed domain object

# File lib/kosapi_client/entity/data_mappings.rb, line 46
def parse(content, context = {})
  instance = new()
  set_mapped_attributes(instance, content)
  instance
end
set_mapped_attributes(instance, source_hash) click to toggle source

Creates new domain object instance and sets values of mapped domain object attributes from source hash. Attributes are mapped by .map_data method.

# File lib/kosapi_client/entity/data_mappings.rb, line 55
def set_mapped_attributes(instance, source_hash)
  if self.superclass.respond_to? :set_mapped_attributes
    self.superclass.set_mapped_attributes(instance, source_hash)
  end
  raise "Missing data mappings for entity #{self}" unless @data_mappings
  @data_mappings.each do |name, options|
    set_mapped_attribute(instance, name, source_hash, options)
  end
end

Private Instance Methods

convert_array(values, type) click to toggle source

Converts values of array type to proper domain objects. It checks whether the value is really an array, because when API returns a single value it does not get parsed into an array.

# File lib/kosapi_client/entity/data_mappings.rb, line 101
def convert_array(values, type)
  if values.is_a?(Array)
    values.map { |it| convert_type(it, type) }
  else
    [ convert_type(values, type) ]
  end
end
convert_type(value, type) click to toggle source
# File lib/kosapi_client/entity/data_mappings.rb, line 88
def convert_type(value, type)
  return value.to_i if type == Integer
  return value if type == String
  return convert_array(value, type.first) if type.is_a?(Array)

  return type.parse(value) if type.respond_to? :parse
  raise "Unknown type #{type} to convert value #{value} to."
end
retrieve_value(source_hash, key, mapping_options) click to toggle source
# File lib/kosapi_client/entity/data_mappings.rb, line 109
def retrieve_value(source_hash, key, mapping_options)
  if (reader = mapping_options[:reader])
    return reader.call(source_hash, key)
  end

  if (path = mapping_options[:path])
    parent_element = source_hash[path]
  else
    parent_element = source_hash
  end
  parent_element[key] if parent_element
end
set_mapped_attribute(instance, name, source_hash, mapping_options) click to toggle source
# File lib/kosapi_client/entity/data_mappings.rb, line 66
def set_mapped_attribute(instance, name, source_hash, mapping_options)
  namespace = mapping_options[:namespace]
  src_element = mapping_options[:element] || name
  if namespace
    key = "#{namespace}_#{src_element}".to_sym
  else
    key = src_element
  end
  value = retrieve_value(source_hash, key, mapping_options)
  if value.nil?
    raise "Missing value for attribute #{name}" if mapping_options[:required]
    if mapping_options[:type].is_a?(Array)
      value = []
    else
      return
    end
  else
    value = convert_type(value, mapping_options[:type])
  end
    instance.send("#{name}=".to_sym, value)
end