Class: Simpal::PayPalObject
- Inherits:
-
Object
- Object
- Simpal::PayPalObject
- Defined in:
- lib/simpal/paypal_object.rb
Overview
Represents an API resource.
Constant Summary collapse
- TIME_REGEXP =
Returns A simple regular expression that matches the syntax of an ISO8601 date and time.
/\A[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}(Z|\+[0-9]{2}:[0-9]{2}(\.[0-9]+)?)\z/.freeze
Instance Method Summary collapse
-
#initialize(resource = {}) ⇒ PayPalObject
constructor
Create an object representing a resource from the PayPal API.
-
#inspect ⇒ String
A JSON representation of the PayPal object.
-
#method_missing(_name, *_args) ⇒ NilClass
Returns ‘nil` when the method is missing instead of raising a `NoMethodError` exception.
-
#respond_to_missing?(name, _include_all) ⇒ Boolean
Determines whether the object can respond to the specified method name.
-
#to_hash ⇒ Hash
(also: #to_h)
A hash representation of the PayPal object.
Constructor Details
#initialize(resource = {}) ⇒ PayPalObject
Create an object representing a resource from the PayPal API.
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/simpal/paypal_object.rb', line 15 def initialize(resource = {}) transform = proc do |value| case value when Array then value.map { |v| transform.call(v) } when Hash then PayPalObject.new(value) when TIME_REGEXP then Time.parse(value) else value end end @resource = resource.each_with_object({}) do |(key, value), hash| hash[key.to_s] = transform.call(value) end @resource.each_key do |key| define_singleton_method(key.to_sym) do |*args, &block| return @resource[key] if args.empty? return super(*args, &block) if defined?(super) raise NoMethodError, "method '#{key}' doesn't support arguments in #{self}" end end end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(_name, *_args) ⇒ NilClass
Returns ‘nil` when the method is missing instead of raising a `NoMethodError` exception.
78 79 80 |
# File 'lib/simpal/paypal_object.rb', line 78 def method_missing(_name, *_args) nil end |
Instance Method Details
#inspect ⇒ String
Returns A JSON representation of the PayPal object.
56 57 58 59 60 |
# File 'lib/simpal/paypal_object.rb', line 56 def inspect resource_id = respond_to?(:id) && id ? " id=#{id}" : '' json = JSON.pretty_generate(@resource) "#<#{self.class}:0x#{object_id.to_s(16)}#{resource_id}> JSON: #{json}" end |
#respond_to_missing?(name, _include_all) ⇒ Boolean
Determines whether the object can respond to the specified method name.
68 69 70 |
# File 'lib/simpal/paypal_object.rb', line 68 def respond_to_missing?(name, _include_all) @resource.key?(name.to_s) end |
#to_hash ⇒ Hash Also known as: to_h
Returns A hash representation of the PayPal object.
41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/simpal/paypal_object.rb', line 41 def to_hash transform = proc do |value| case value when Array then value.map { |v| transform.call(v) } when PayPalObject then value.to_hash when Time then value.iso8601 else value end end @resource.transform_values(&transform) end |