class KOSapiClient::ResponseConverter

This class converts parsed response in hash format (wrapped in Response) into domain Ruby objects. Root domain object type is determined at runtime based on API response.

Public Class Methods

new(client) click to toggle source
# File lib/kosapi_client/response_converter.rb, line 12
def initialize(client)
  @client = client
end

Public Instance Methods

convert(response) click to toggle source
# File lib/kosapi_client/response_converter.rb, line 16
def convert(response)
  if response.is_paginated?
    convert_paginated(response)
  else
    convert_single(response.item)
  end
end
convert_paginated(response) click to toggle source

Returns processed entries converted into domain objects wrapped into ResultPage class instance. @param response [KOSapiResponse] Response object wrapping array of hashes corresponding to entries @return [ResultPage] ResultPage of domain objects

# File lib/kosapi_client/response_converter.rb, line 29
def convert_paginated(response)
  items = response.items || []
  converted_items = items.map{ |p| convert_single(p) }
  Entity::ResultPage.new(converted_items, create_links(response))
end
convert_single(item) click to toggle source
# File lib/kosapi_client/response_converter.rb, line 35
def convert_single(item)
  type = detect_type(item)
  convert_type(item, type)
end

Private Instance Methods

convert_type(hash, type) click to toggle source
# File lib/kosapi_client/response_converter.rb, line 41
def convert_type(hash, type)
  type.parse(hash)
end
detect_type(hash) click to toggle source
# File lib/kosapi_client/response_converter.rb, line 45
def detect_type(hash)
  type_str = hash[:xsi_type]
  extract_type(type_str)
end
extract_type(type_str) click to toggle source
# File lib/kosapi_client/response_converter.rb, line 50
def extract_type(type_str)
  type_name = type_str.camelcase(:upper)
  begin
    entity_type = Entity.const_get(type_name)
  rescue
    raise "Unknown entity type: #{type_name}"
  end
  entity_type
end