Class: Unit::APIResource

Inherits:
Object
  • Object
show all
Defined in:
lib/unit-ruby/util/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.



5
6
7
8
9
10
11
12
# File 'lib/unit-ruby/util/api_resource.rb', line 5

def initialize(attributes = {})
  clear_attributes!
  mark_as_clean!

  attributes.each do |key, value|
    send("#{key}=", value)
  end
end

Instance Attribute Details

#idObject

Returns the value of attribute id.



3
4
5
# File 'lib/unit-ruby/util/api_resource.rb', line 3

def id
  @id
end

Returns the value of attribute links.



3
4
5
# File 'lib/unit-ruby/util/api_resource.rb', line 3

def links
  @links
end

#raw_dataObject

Returns the value of attribute raw_data.



3
4
5
# File 'lib/unit-ruby/util/api_resource.rb', line 3

def raw_data
  @raw_data
end

#relationshipsObject



51
52
53
# File 'lib/unit-ruby/util/api_resource.rb', line 51

def relationships
  @relationships ||= {}
end

#typeObject

Returns the value of attribute type.



3
4
5
# File 'lib/unit-ruby/util/api_resource.rb', line 3

def type
  @type
end

Class Method Details

.attribute(name, type = nil, readonly: false) ⇒ Object

Declares a new attribute by name and adds it to the schema

Parameters:

  • name (Symbol)

    the name of the attribute

  • type (Class) (defaults to: nil)

    the object type

  • readonly (Boolean) (defaults to: false)

    excludes the attribute from the request when creating a resource



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/unit-ruby/util/api_resource.rb', line 35

def self.attribute(name, type = nil, readonly: false)
  schema.add(name, type, readonly: readonly)

  attr_accessor name

  define_method("#{name}=") do |value|
    previous_value = send(name)
    new_value = type.cast(value)

    instance_variable_set("@#{name}", new_value)

    mark_attribute_as_dirty(name) if new_value != previous_value
    new_value
  end
end

.belongs_to(resource_name, class_name: nil) ⇒ Object

Creates an association to a related resource This will create a helper method to traverse into a resource’s related resource(s)



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/unit-ruby/util/api_resource.rb', line 101

def self.belongs_to(resource_name, class_name: nil)
  class_name ||= resource_name.to_s.camelize

  define_method(resource_name) do
    relationship_id = relationships.dig(resource_name, :data, :id)

    return nil unless relationship_id

    Kernel.const_get(class_name).find(relationship_id)
  end

  define_method("#{resource_name}=") do |resource|
    relationships[resource_name] = {
      data: { type: resource_name.to_s.camelize(:lower).to_sym, id: resource.id }
    }
  end
end

.build_resource_from_json_api(data_item) ⇒ Object

Hyrdates an instance of the resource from data returned from the API



155
156
157
158
159
160
# File 'lib/unit-ruby/util/api_resource.rb', line 155

def self.build_resource_from_json_api(data_item)
  new.tap do |resource|
    resource.mark_as_clean!
    resource.update_resource_from_json_api(data_item)
  end
end

.connectionObject

Creates a base http connection to the API



16
17
18
# File 'lib/unit-ruby/util/api_resource.rb', line 16

def self.connection
  @connection ||= Connection.new(headers)
end

.has_many(resource_name, class_name: nil) ⇒ Object



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/unit-ruby/util/api_resource.rb', line 119

def self.has_many(resource_name, class_name: nil)
  class_name ||= resource_name.to_s.camelize

  define_method(resource_name) do
    # if there is only one of the resources, Unit will return the
    # singular name of that relationship in their response.
    # For example, if a deposit account has one customer, `customer`
    # will be returned from Unit as a resource, whereas if there
    # are more than one customer, then customers will be returned from Unit

    # the below will translate these singular relationship
    # into a plural relationship - ie `customer` -> `customers`

    singular_resource_name = resource_name.to_s.singularize.to_sym

    if relationships.keys.include?(resource_name)
      relationships.dig(resource_name.to_sym, :data).map do |resource|
        Kernel.const_get(class_name).find(resource[:id])
      end
    elsif defined?(singular_resource_name)
      [send(singular_resource_name)].compact
    end
  end

  define_method("#{resource_name}=") do |resources|
    singular_resource_name = resource_name.to_s.singularize.to_sym

    relationships[resource_name] = {
      data: resources.map do |resource|
        { type: singular_resource_name, id: resource.id }
      end
    }
  end
end

.header(header_key_value_pair) ⇒ Object

Sets resource-specific headers

Usage:

class Customer < Unit::Resource
    header 'X-Some-Header' => 'Header Value'
end


75
76
77
78
# File 'lib/unit-ruby/util/api_resource.rb', line 75

def self.header(header_key_value_pair)
  key, value = header_key_value_pair.first
  headers[key] = value
end

.headersObject



80
81
82
# File 'lib/unit-ruby/util/api_resource.rb', line 80

def self.headers
  @headers ||= {}
end

.path(route = nil) ⇒ Object

Sets the base path for this resource

Usage:

class Customer < Unit::Resource
    path '/customers'
end


63
64
65
66
67
# File 'lib/unit-ruby/util/api_resource.rb', line 63

def self.path(route = nil)
  return @path if route.nil?

  @path = route
end

.resource_path(id) ⇒ Object



84
85
86
# File 'lib/unit-ruby/util/api_resource.rb', line 84

def self.resource_path(id)
  "#{path}/#{id}"
end

.resources_path(id = nil) ⇒ Object



88
89
90
91
92
# File 'lib/unit-ruby/util/api_resource.rb', line 88

def self.resources_path(id = nil)
  return "#{path}/#{id}" if id

  path
end

.schemaObject

Defines the schema for a resource’s attributes



22
23
24
# File 'lib/unit-ruby/util/api_resource.rb', line 22

def self.schema
  @schema ||= Schema.new
end

Instance Method Details

#as_json_apiHash

Represents this resource for serialization (create/update)

Returns:

  • (Hash)

    Representation of this object as JSONAPI object



188
189
190
191
192
193
194
195
196
197
198
199
# File 'lib/unit-ruby/util/api_resource.rb', line 188

def as_json_api
  self.class.schema.attributes.each_with_object({}) do |schema_attribute, h|
    next if schema_attribute.readonly

    val = send(schema_attribute.name)

    # serialize the value if it is a complex type
    val = val.as_json_api if val.respond_to? :as_json_api

    h[schema_attribute.name] = val
  end
end

#attributesHash

Represents this resource’s attributes

Returns:

  • (Hash)

    Representation of this resource’s attributes as a hash



179
180
181
182
183
# File 'lib/unit-ruby/util/api_resource.rb', line 179

def attributes
  self.class.schema.attributes.each_with_object({}) do |schema_attribute, h|
    h[schema_attribute.name] = send(schema_attribute.name)
  end
end

#clear_attributes!Object



217
218
219
220
221
# File 'lib/unit-ruby/util/api_resource.rb', line 217

def clear_attributes!
  schema.attributes.each do |attribute|
    update_attribute(attribute.name, nil)
  end
end

#dirty?Boolean

Returns:

  • (Boolean)


201
202
203
# File 'lib/unit-ruby/util/api_resource.rb', line 201

def dirty?
  dirty_attributes.any?
end

#dirty_attributesObject



205
206
207
# File 'lib/unit-ruby/util/api_resource.rb', line 205

def dirty_attributes
  @dirty_attributes ||= []
end

#mark_as_clean!Object



213
214
215
# File 'lib/unit-ruby/util/api_resource.rb', line 213

def mark_as_clean!
  @dirty_attributes = []
end

#mark_attribute_as_dirty(name) ⇒ Object



209
210
211
# File 'lib/unit-ruby/util/api_resource.rb', line 209

def mark_attribute_as_dirty(name)
  dirty_attributes << name
end

#resource_typeObject

The JSON:API type for this resource



95
96
97
# File 'lib/unit-ruby/util/api_resource.rb', line 95

def resource_type
  self.class.name.split('::').last.camelize(:lower)
end

#schemaObject



26
27
28
# File 'lib/unit-ruby/util/api_resource.rb', line 26

def schema
  self.class.schema
end

#update_attribute(name, value) ⇒ Object



223
224
225
226
227
# File 'lib/unit-ruby/util/api_resource.rb', line 223

def update_attribute(name, value)
  return unless schema.contains? name

  send("#{name}=", value)
end

#update_resource_from_json_api(data) ⇒ Object



162
163
164
165
166
167
168
169
170
171
172
173
174
# File 'lib/unit-ruby/util/api_resource.rb', line 162

def update_resource_from_json_api(data)
  self.id = data[:id]
  self.type = data[:type]
  self.raw_data = data
  self.relationships = data[:relationships]
  self.links = data[:links]

  clear_attributes!

  data[:attributes].each { |key, value| update_attribute(key, value) }

  mark_as_clean!
end