Class: Airwallex::APIResource

Inherits:
Object
  • Object
show all
Defined in:
lib/airwallex/api_resource.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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

#attributesObject (readonly)

Returns the value of attribute attributes.



5
6
7
# File 'lib/airwallex/api_resource.rb', line 5

def attributes
  @attributes
end

#idObject (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_nameObject

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_pathObject

Override in subclasses to specify custom path

Raises:

  • (NotImplementedError)


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_attributesObject

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

Returns:

  • (Boolean)


65
66
67
# File 'lib/airwallex/api_resource.rb', line 65

def dirty?
  !@previous_attributes.empty?
end

#inspectObject

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

#refreshObject

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

Returns:

  • (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_hashObject 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_sObject



92
93
94
# File 'lib/airwallex/api_resource.rb', line 92

def to_s
  to_json
end