Class: LemonSqueezy::Object
- Inherits:
-
OpenStruct
- Object
- OpenStruct
- LemonSqueezy::Object
- Defined in:
- lib/lemon_squeezy/object.rb
Direct Known Subclasses
Checkout, Customer, Discount, DiscountRedemption, File, License, LicenseKey, LicenseKeyInstance, Order, Price, Product, Store, Subscription, SubscriptionInvoice, SubscriptionItem, SubscriptionUsage, User, Variant, Webhook
Instance Method Summary collapse
-
#initialize(attributes) ⇒ Object
constructor
A new instance of Object.
- #to_ostruct(obj) ⇒ Object
Constructor Details
#initialize(attributes) ⇒ Object
Returns a new instance of Object.
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/lemon_squeezy/object.rb', line 5 def initialize(attributes) # Remove relationships and links from responses as they are not required attributes.delete "relationships" attributes.delete "links" attrs = {} if attributes["attributes"] # Add ID from response to attributes attrs[:id] = attributes["id"] if attributes["id"] attrs.merge!(attributes["attributes"]) else attrs.merge!(attributes) end super to_ostruct(attrs) end |
Instance Method Details
#to_ostruct(obj) ⇒ Object
23 24 25 26 27 28 29 30 31 |
# File 'lib/lemon_squeezy/object.rb', line 23 def to_ostruct(obj) if obj.is_a?(Hash) OpenStruct.new(obj.map { |key, val| [key, to_ostruct(val)] }.to_h) elsif obj.is_a?(Array) obj.map { |o| to_ostruct(o) } else # Assumed to be a primitive value obj end end |