Class: Cronofy::ResponseParser
- Inherits:
-
Object
- Object
- Cronofy::ResponseParser
- Defined in:
- lib/cronofy/response_parser.rb
Overview
Internal: Class for dealing with the parsing of API responses.
Instance Method Summary collapse
-
#initialize(response) ⇒ ResponseParser
constructor
A new instance of ResponseParser.
- #json ⇒ Object
- #parse_collection(type, attribute = nil) ⇒ Object
- #parse_collections(attribute_collection_types) ⇒ Object
- #parse_json(type, attribute = nil) ⇒ Object
Constructor Details
#initialize(response) ⇒ ResponseParser
Returns a new instance of ResponseParser.
6 7 8 |
# File 'lib/cronofy/response_parser.rb', line 6 def initialize(response) @response = response end |
Instance Method Details
#json ⇒ Object
28 29 30 |
# File 'lib/cronofy/response_parser.rb', line 28 def json json_hash.dup end |
#parse_collection(type, attribute = nil) ⇒ Object
18 19 20 21 |
# File 'lib/cronofy/response_parser.rb', line 18 def parse_collection(type, attribute = nil) target = parsing_target(attribute) target.map { |item| type.new(item) } end |
#parse_collections(attribute_collection_types) ⇒ Object
10 11 12 13 14 15 16 |
# File 'lib/cronofy/response_parser.rb', line 10 def parse_collections(attribute_collection_types) attribute_collection_types.each do |attribute, type| return parse_collection(type, attribute.to_s) if json_hash[attribute.to_s] end raise "No mapped attributes for response - #{json_hash.keys}" end |
#parse_json(type, attribute = nil) ⇒ Object
23 24 25 26 |
# File 'lib/cronofy/response_parser.rb', line 23 def parse_json(type, attribute = nil) target = parsing_target(attribute) type.new(target) end |