Sha256: afcb4e7ffc36d8d4b23b3b0ff5ce7374dd907ec0bf7acefcafafc1db50ae67ea
Contents?: true
Size: 1.9 KB
Versions: 4
Compression:
Stored size: 1.9 KB
Contents
require 'json' module Recurly # This is a wrapper class to help parse responses into Recurly objects. class JSONParser # Parses the json body into a recurly object. # # @param client [Client] The Recurly client which made the request. # @param body [String] The JSON string to parse. # @return [Resource] def self.parse(client, body) data = JSON.parse(body) from_json(data).tap do |object| object.client = client if object.requires_client? end end # Converts the parsed JSON into a Recurly object. # # @param data [Hash] The parsed JSON data # @return [Error,Resource] def self.from_json(data) type = if data.has_key?("error") "error" else data.delete('object') end klazz = self.recurly_class(type) unless klazz raise ArgumentError, "Unknown resource for json type #{type}" end data = data["error"] if klazz == Resources::Error klazz.from_json(data) end # Returns the Recurly ruby class responsible for the Recurly json key. # TODO figure out how we should handle nil types # # @example # JSONParser.recurly_class('list') # #=> Recurly::Pager # @example # JSONParser.recurly_class('shipping_address') # #=> Recurly::Resources::ShippingAddress # # @param type [String] The JSON key. # @return [Resource,Pager,nil] def self.recurly_class(type) case type when nil nil when 'list' Pager else type_camelized = type.split('_').map(&:capitalize).join if Resources.const_defined?(type_camelized) klazz = Resources.const_get(type_camelized) if klazz.ancestors.include?(Resource) klazz else # TODO might want to throw an error? nil end end end end end end
Version data entries
4 entries across 4 versions & 1 rubygems