Class: GoCardless::Resource

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

Direct Known Subclasses

Bill, Merchant, Payout, 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.


6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/gocardless/resource.rb', line 6

def initialize(hash = {})
  # Define an object singleton method for each sub resource
  hash.delete('sub_resource_uris') { [] }.each do |name, uri|
    uri = URI.parse(uri)

    # Convert the query string to a params hash
    default_query = if uri.query.nil? || uri.query.empty?
      {}
    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
    define_paginated_resource_method(name, path, default_query)
  end

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

Class Attribute Details

.endpointObject

Returns the value of attribute endpoint


32
33
34
# File 'lib/gocardless/resource.rb', line 32

def endpoint
  @endpoint
end

Instance Attribute Details

#client=(value) ⇒ Object

Sets the attribute client

Parameters:

  • value

    the value to set the attribute client to.


29
30
31
# File 'lib/gocardless/resource.rb', line 29

def client=(value)
  @client = value
end

#idString

Returns the $1 property of the object.

Returns:

  • (String)

    the $1 property of the object


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

def id
  @id
end

#uriObject

Returns the value of attribute uri


129
130
131
# File 'lib/gocardless/resource.rb', line 129

def uri
  @uri
end

Class Method Details

.creatable(val = true) ⇒ Object


108
109
110
# File 'lib/gocardless/resource.rb', line 108

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

.creatable?Boolean

Returns:

  • (Boolean)

116
117
118
# File 'lib/gocardless/resource.rb', line 116

def creatable?
  !!@creatable
end

.date_accessor(*args) ⇒ Object


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

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

.date_writer(*args) ⇒ Object


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

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:


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

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


38
39
40
41
42
43
44
# File 'lib/gocardless/resource.rb', line 38

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


34
35
36
# File 'lib/gocardless/resource.rb', line 34

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

.reference_accessor(*args) ⇒ Object


103
104
105
106
# File 'lib/gocardless/resource.rb', line 103

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

.reference_reader(*args) ⇒ Object


66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/gocardless/resource.rb', line 66

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


83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/gocardless/resource.rb', line 83

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


112
113
114
# File 'lib/gocardless/resource.rb', line 112

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

.updatable?Boolean

Returns:

  • (Boolean)

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

def updatable?
  !!@updatable
end

Instance Method Details

#inspectObject


141
142
143
# File 'lib/gocardless/resource.rb', line 141

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

#persisted?Boolean

Returns:

  • (Boolean)

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

def persisted?
  !id.nil?
end

#saveObject

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.


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

def save
  save_data self.to_hash
  self
end

#to_hashObject


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

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_jsonObject


137
138
139
# File 'lib/gocardless/resource.rb', line 137

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