Class: GoCardless::Resource

Inherits:
Object
  • Object
show all
Defined in:
lib/gocardless/resource.rb

Direct Known Subclasses

Bill, Merchant, Payment, PreAuthorization, Subscription, User

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hash = {}) ⇒ Resource

Returns a new instance of Resource.



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/gocardless/resource.rb', line 5

def initialize(hash = {})
  # Handle sub resources
  sub_resource_uris = hash.delete('sub_resource_uris')
  unless sub_resource_uris.nil?
    # Need to define a method for each sub resource
    sub_resource_uris.each do |name,uri|
      uri = URI.parse(uri)

      # Convert the query string to a hash
      default_query = if uri.query.nil? || uri.query == ''
        nil
      else
        Hash[CGI.parse(uri.query).map { |k,v| [k,v.first] }]
      end

      # Strip api prefix from path
      path = uri.path.sub(%r{^/api/v\d+}, '')

      # Modify the instance's metaclass to add the method
      metaclass = class << self; self; end
      metaclass.send(:define_method, name) do |*args|
        # 'name' will be something like 'bills', convert it to Bill and
        # look up the resource class with that name
        class_name = Utils.camelize(Utils.singularize(name.to_s))
        klass = GoCardless.const_get(class_name)
        # Convert the results to instances of the looked-up class
        params = args.first || {}
        query = default_query.nil? ? nil : default_query.merge(params)
        client.api_get(path, query).map do |attrs|
          klass.new(attrs).tap { |m| m.client = client }
        end
      end
    end
  end

  # Set resource attribute values
  hash.each { |key,val| send("#{key}=", val) if respond_to?("#{key}=") }
end

Class Attribute Details

.endpoint ⇒ Object

Returns the value of attribute endpoint.



47
48
49
# File 'lib/gocardless/resource.rb', line 47

def endpoint
  @endpoint
end

Instance Attribute Details

#client=(value) ⇒ Object

Sets the attribute client

Parameters:

  • value —

    the value to set the attribute client to.



44
45
46
# File 'lib/gocardless/resource.rb', line 44

def client=(value)
  @client = value
end

#id ⇒ String

Returns the $1 property of the object.

Returns:

  • (String) —

    the $1 property of the object



143
144
145
# File 'lib/gocardless/resource.rb', line 143

def id
  @id
end

#uri ⇒ Object

Returns the value of attribute uri.



144
145
146
# File 'lib/gocardless/resource.rb', line 144

def uri
  @uri
end

Class Method Details

.creatable(val = true) ⇒ Object



123
124
125
# File 'lib/gocardless/resource.rb', line 123

def creatable(val = true)
  @creatable = val
end

.creatable? ⇒ Boolean

Returns:

  • (Boolean)


131
132
133
# File 'lib/gocardless/resource.rb', line 131

def creatable?
  !!@creatable
end

.date_accessor(*args) ⇒ Object



76
77
78
79
# File 'lib/gocardless/resource.rb', line 76

def date_accessor(*args)
  attr_reader *args
  date_writer *args
end

.date_writer(*args) ⇒ Object



67
68
69
70
71
72
73
74
# File 'lib/gocardless/resource.rb', line 67

def date_writer(*args)
  args.each do |attr|
    define_method("#{attr.to_s}=".to_sym) do |date|
      date = date.is_a?(String) ? DateTime.parse(date) : date
      instance_variable_set("@#{attr}", date)
    end
  end
end

.find(id) ⇒ Object

Raises:



61
62
63
64
65
# File 'lib/gocardless/resource.rb', line 61

def find(id)
  message = "Merchant details not found, set GoCardless.account_details"
  raise Error, message unless GoCardless.client
  self.find_with_client(GoCardless.client, id)
end

.find_with_client(client_obj, id) ⇒ Object



53
54
55
56
57
58
59
# File 'lib/gocardless/resource.rb', line 53

def find_with_client(client_obj, id)
  path = endpoint.gsub(':id', id.to_s)
  data = client_obj.api_get(path)
  obj = self.new(data)
  obj.client = client_obj
  obj
end

.new_with_client(client, attrs = {}) ⇒ Object



49
50
51
# File 'lib/gocardless/resource.rb', line 49

def new_with_client(client, attrs = {})
  self.new(attrs).tap { |obj| obj.client = client }
end

.reference_accessor(*args) ⇒ Object



118
119
120
121
# File 'lib/gocardless/resource.rb', line 118

def reference_accessor(*args)
  reference_reader *args
  reference_writer *args
end

.reference_reader(*args) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/gocardless/resource.rb', line 81

def reference_reader(*args)
  attr_reader *args

  args.each do |attr|
    if !attr.to_s.end_with?('_id')
      raise ArgumentError, 'reference_reader args must end with _id'
    end

    name = attr.to_s.sub(/_id$/, '')
    define_method(name.to_sym) do
      obj_id = instance_variable_get("@#{attr}")
      klass = GoCardless.const_get(Utils.camelize(name))
      klass.find_with_client(client, obj_id)
    end
  end
end

.reference_writer(*args) ⇒ Object



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/gocardless/resource.rb', line 98

def reference_writer(*args)
  attr_writer *args

  args.each do |attr|
    if !attr.to_s.end_with?('_id')
      raise ArgumentError, 'reference_writer args must end with _id'
    end

    name = attr.to_s.sub(/_id$/, '')
    define_method("#{name}=".to_sym) do |obj|
      klass = GoCardless.const_get(Utils.camelize(name))
      if !obj.is_a?(klass)
        raise ArgumentError, "Object must be an instance of #{klass}"
      end

      instance_variable_set("@#{attr}", obj.id)
    end
  end
end

.updatable(val = true) ⇒ Object



127
128
129
# File 'lib/gocardless/resource.rb', line 127

def updatable(val = true)
  @updatable = val
end

.updatable? ⇒ Boolean

Returns:

  • (Boolean)


135
136
137
# File 'lib/gocardless/resource.rb', line 135

def updatable?
  !!@updatable
end

Instance Method Details

#inspect ⇒ Object



156
157
158
# File 'lib/gocardless/resource.rb', line 156

def inspect
  "#<#{self.class} #{to_hash.map { |k,v| "#{k}=#{v.inspect}" }.join(', ')}>"
end

#persisted? ⇒ Boolean

Returns:

  • (Boolean)


160
161
162
# File 'lib/gocardless/resource.rb', line 160

def persisted?
  !id.nil?
end

#save ⇒ Object

Save a resource on the API server. If the resource already exists (has a non-null id), it will be updated with a PUT, otherwise it will be created with a POST.



167
168
169
170
# File 'lib/gocardless/resource.rb', line 167

def save
  save_data self.to_hash
  self
end

#to_hash ⇒ Object



146
147
148
149
150
# File 'lib/gocardless/resource.rb', line 146

def to_hash
  attrs = instance_variables.map { |v| v.to_s.sub(/^@/, '') }
  attrs.delete 'client'
  Hash[attrs.select { |v| respond_to? v }.map { |v| [v.to_sym, send(v)] }]
end

#to_json ⇒ Object



152
153
154
# File 'lib/gocardless/resource.rb', line 152

def to_json
  MultiJson.encode(Utils.stringify_times(to_hash))
end