class Cronofy::ResponseParser

Internal: Class for dealing with the parsing of API responses.

Public Class Methods

new(response) click to toggle source
# File lib/cronofy/response_parser.rb, line 6
def initialize(response)
  @response = response
end

Public Instance Methods

json() click to toggle source
# File lib/cronofy/response_parser.rb, line 28
def json
  json_hash.dup
end
parse_collection(type, attribute = nil) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# File lib/cronofy/response_parser.rb, line 23
def parse_json(type, attribute = nil)
  target = parsing_target(attribute)
  type.new(target)
end

Private Instance Methods

json_hash() click to toggle source
# File lib/cronofy/response_parser.rb, line 34
def json_hash
  @json_hash ||= JSON.parse(@response.body)
end
parsing_target(attribute) click to toggle source
# File lib/cronofy/response_parser.rb, line 38
def parsing_target(attribute)
  if attribute
    json_hash[attribute]
  else
    json_hash
  end
end