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



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/airwallex/api_resource.rb', line 50

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

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

.symbolized_get(path, params = {}) ⇒ Hash

Perform a one-off GET that returns a plain, symbolized Hash rather than an APIResource. See .symbolized_post.

Parameters:

  • path (String) —

    the request path

  • params (Hash) (defaults to: {}) —

    query params

Returns:

  • (Hash) —

    symbolized response



45
46
47
# File 'lib/airwallex/api_resource.rb', line 45

def self.symbolized_get(path, params = {})
  Util.deep_symbolize_keys(Airwallex.client.get(path, params))
end

.symbolized_post(path, params = {}) ⇒ Hash

Perform a one-off POST that returns a plain, symbolized Hash rather than an APIResource — for actions with no id/refresh lifecycle of their own (e.g. Beneficiary.verify_account, a bank-detail check that never becomes a resource).

Parameters:

  • path (String) —

    the request path

  • params (Hash) (defaults to: {}) —

    request body

Returns:

  • (Hash) —

    symbolized response



35
36
37
# File 'lib/airwallex/api_resource.rb', line 35

def self.symbolized_post(path, params = {})
  Util.deep_symbolize_keys(Airwallex.client.post(path, params))
end

Instance Method Details

#changed_attributes ⇒ Object

Get changed attributes



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

def changed_attributes
  @previous_attributes.keys
end

#dirty? ⇒ Boolean

Check if any attributes have changed

Returns:

  • (Boolean)


87
88
89
# File 'lib/airwallex/api_resource.rb', line 87

def dirty?
  !@previous_attributes.empty?
end

#inspect ⇒ Object

String representation



109
110
111
112
# File 'lib/airwallex/api_resource.rb', line 109

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



72
73
74
75
76
# File 'lib/airwallex/api_resource.rb', line 72

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



79
80
81
82
83
84
# File 'lib/airwallex/api_resource.rb', line 79

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)


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

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



97
98
99
# File 'lib/airwallex/api_resource.rb', line 97

def to_hash
  @attributes.dup
end

#to_json(*args) ⇒ Object

Convert to JSON



104
105
106
# File 'lib/airwallex/api_resource.rb', line 104

def to_json(*args)
  to_hash.to_json(*args)
end

#to_s ⇒ Object



114
115
116
# File 'lib/airwallex/api_resource.rb', line 114

def to_s
  to_json
end