Class: Airwallex::APIResource
- Inherits:
-
Object
- Object
- Airwallex::APIResource
- Defined in:
- lib/airwallex/api_resource.rb
Direct Known Subclasses
Balance, BatchTransfer, Beneficiary, Conversion, Customer, Dispute, PaymentIntent, PaymentMethod, Quote, Rate, Refund, Transfer
Instance Attribute Summary collapse
-
#attributes ⇒ Object
readonly
Returns the value of attribute attributes.
-
#id ⇒ Object
readonly
Returns the value of attribute id.
Class Method Summary collapse
-
.resource_name ⇒ Object
Convert class name to resource name PaymentIntent -> payment_intent.
-
.resource_path ⇒ Object
Override in subclasses to specify custom path.
Instance Method Summary collapse
-
#changed_attributes ⇒ Object
Get changed attributes.
-
#dirty? ⇒ Boolean
Check if any attributes have changed.
-
#initialize(attributes = {}) ⇒ APIResource
constructor
A new instance of APIResource.
-
#inspect ⇒ Object
String representation.
-
#method_missing(method_name, *args) ⇒ Object
Dynamic attribute accessors.
-
#refresh ⇒ Object
Refresh resource from API.
-
#refresh_from(data) ⇒ Object
Update internal state from response.
- #respond_to_missing?(method_name, include_private = false) ⇒ Boolean
-
#to_hash ⇒ Object
(also: #to_h)
Convert to hash.
-
#to_json(*args) ⇒ Object
Convert to JSON.
- #to_s ⇒ Object
Constructor Details
#initialize(attributes = {}) ⇒ APIResource
Returns a new instance of APIResource.
7 8 9 10 11 |
# File 'lib/airwallex/api_resource.rb', line 7 def initialize(attributes = {}) @attributes = Util.deep_symbolize_keys(attributes || {}) @id = @attributes[:id] @previous_attributes = {} end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_name, *args) ⇒ Object
Dynamic attribute accessors
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/airwallex/api_resource.rb', line 28 def method_missing(method_name, *args, &) method_str = method_name.to_s if method_str.end_with?("=") # Setter attr_name = method_str.chop.to_sym @previous_attributes[attr_name] = @attributes[attr_name] unless @previous_attributes.key?(attr_name) @attributes[attr_name] = args[0] elsif @attributes.key?(method_name) # Getter @attributes[method_name] else super end end |
Instance Attribute Details
#attributes ⇒ Object (readonly)
Returns the value of attribute attributes.
5 6 7 |
# File 'lib/airwallex/api_resource.rb', line 5 def attributes @attributes end |
#id ⇒ Object (readonly)
Returns the value of attribute id.
5 6 7 |
# File 'lib/airwallex/api_resource.rb', line 5 def id @id end |
Class Method Details
.resource_name ⇒ Object
Convert class name to resource name PaymentIntent -> payment_intent
15 16 17 18 19 20 |
# File 'lib/airwallex/api_resource.rb', line 15 def self.resource_name name.split("::")[-1] .gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2') .gsub(/([a-z\d])([A-Z])/, '\1_\2') .downcase end |
.resource_path ⇒ Object
Override in subclasses to specify custom path
23 24 25 |
# File 'lib/airwallex/api_resource.rb', line 23 def self.resource_path raise NotImplementedError, "#{self} must implement .resource_path" end |
Instance Method Details
#changed_attributes ⇒ Object
Get changed attributes
70 71 72 |
# File 'lib/airwallex/api_resource.rb', line 70 def changed_attributes @previous_attributes.keys end |
#dirty? ⇒ Boolean
Check if any attributes have changed
65 66 67 |
# File 'lib/airwallex/api_resource.rb', line 65 def dirty? !@previous_attributes.empty? end |
#inspect ⇒ Object
String representation
87 88 89 90 |
# File 'lib/airwallex/api_resource.rb', line 87 def inspect id_str = id ? " id=#{id}" : "" "#<#{self.class}:0x#{object_id.to_s(16)}#{id_str}> JSON: #{to_json}" end |
#refresh ⇒ Object
Refresh resource from API
50 51 52 53 54 |
# File 'lib/airwallex/api_resource.rb', line 50 def refresh response = Airwallex.client.get("#{self.class.resource_path}/#{id}") refresh_from(response) self end |
#refresh_from(data) ⇒ Object
Update internal state from response
57 58 59 60 61 62 |
# File 'lib/airwallex/api_resource.rb', line 57 def refresh_from(data) @attributes = Util.deep_symbolize_keys(data || {}) @id = @attributes[:id] @previous_attributes = {} self end |
#respond_to_missing?(method_name, include_private = false) ⇒ Boolean
44 45 46 47 |
# File 'lib/airwallex/api_resource.rb', line 44 def respond_to_missing?(method_name, include_private = false) method_str = method_name.to_s method_str.end_with?("=") || @attributes.key?(method_name) || super end |
#to_hash ⇒ Object Also known as: to_h
Convert to hash
75 76 77 |
# File 'lib/airwallex/api_resource.rb', line 75 def to_hash @attributes.dup end |
#to_json(*args) ⇒ Object
Convert to JSON
82 83 84 |
# File 'lib/airwallex/api_resource.rb', line 82 def to_json(*args) to_hash.to_json(*args) end |
#to_s ⇒ Object
92 93 94 |
# File 'lib/airwallex/api_resource.rb', line 92 def to_s to_json end |