Class: KOSapiClient::ResponseConverter
- Inherits:
-
Object
- Object
- KOSapiClient::ResponseConverter
- Defined in:
- lib/kosapi_client/response_converter.rb
Overview
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.
Instance Method Summary collapse
- #convert(response) ⇒ Object
-
#convert_paginated(response) ⇒ ResultPage
Returns processed entries converted into domain objects wrapped into ResultPage class instance.
- #convert_single(item) ⇒ Object
-
#initialize(client) ⇒ ResponseConverter
constructor
A new instance of ResponseConverter.
Constructor Details
#initialize(client) ⇒ ResponseConverter
Returns a new instance of ResponseConverter.
12 13 14 |
# File 'lib/kosapi_client/response_converter.rb', line 12 def initialize(client) @client = client end |
Instance Method Details
#convert(response) ⇒ Object
16 17 18 19 20 21 22 |
# 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) ⇒ ResultPage
Returns processed entries converted into domain objects wrapped into ResultPage class instance.
29 30 31 32 33 |
# 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) ⇒ Object
35 36 37 38 |
# File 'lib/kosapi_client/response_converter.rb', line 35 def convert_single(item) type = detect_type(item) convert_type(item, type) end |