class Simpal::PayPalObject
Represents an API
resource.
Public Class Methods
new(resource = {})
click to toggle source
Create a new object representing the provided resource.
@param resource [Hash] The resource to represent.
# File lib/simpal/pay_pal_object.rb, line 11 def initialize(resource = {}) transform = proc do |key, value| case value when Array then value.map { |v| transform.call(nil, v) } when Hash then PayPalObject.new(value) else if key && (key.end_with?('date') || key.end_with?('time')) Time.parse(value) else value end end end @values = resource.each_with_object({}) do |(key, value), values| values[key.to_s] = transform.call(key.to_s, value) add_reader(key) end end
Public Instance Methods
inspect()
click to toggle source
@return [String] A JSON string representation of the PayPal object.
# File lib/simpal/pay_pal_object.rb, line 51 def inspect paypal_id = respond_to?(:id) && id ? " id=#{id}" : '' json = JSON.pretty_generate(@values) "#<#{self.class}:0x#{object_id.to_s(16)}#{paypal_id}> JSON: #{json}" end
method_missing(_name, *args)
click to toggle source
@return [NilClass] `nil` instead of raising an exception.
# File lib/simpal/pay_pal_object.rb, line 65 def method_missing(_name, *args); end
respond_to_missing?(_name, _include_all)
click to toggle source
@return [Boolean] `true` to indicate that all methods can be responded to.
# File lib/simpal/pay_pal_object.rb, line 59 def respond_to_missing?(_name, _include_all) true end
to_hash()
click to toggle source
@return [Hash] A hash representation of the PayPal object.
# File lib/simpal/pay_pal_object.rb, line 33 def to_hash transform = proc do |value| case value when Array then value.map { |v| transform.call(v) } when PayPalObject then value.to_hash else value.is_a?(Time) ? value.iso8601 : value end end @values.transform_values(&transform) end
Also aliased as: to_h
Private Instance Methods
add_reader(name)
click to toggle source
Add a read-only attribute for the specified value.
@param name [String, Symbol] The name of the attribute to define.
Calls superclass method
# File lib/simpal/pay_pal_object.rb, line 73 def add_reader(name) metaclass.instance_eval do name = name.to_s if name.to_sym == :method define_method(name.to_sym) { |*args| args.empty? ? @values[name] : super(*args) } else define_method(name.to_sym) { @values[name] } end end end
metaclass()
click to toggle source
@return [Class] The metaclass which can be used to define methods on this instance only.
# File lib/simpal/pay_pal_object.rb, line 86 def metaclass class << self self end end