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

#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)



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/unit-ruby/util/api_resource.rb', line 86

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



140
141
142
143
144
145
# File 'lib/unit-ruby/util/api_resource.rb', line 140

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
end

.has_many(resource_name, class_name: nil) ⇒ Object



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/unit-ruby/util/api_resource.rb', line 104

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

.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



69
70
71
# File 'lib/unit-ruby/util/api_resource.rb', line 69

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

.resources_path(id = nil) ⇒ Object



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

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



172
173
174
175
176
177
178
179
180
181
182
183
# File 'lib/unit-ruby/util/api_resource.rb', line 172

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



163
164
165
166
167
# File 'lib/unit-ruby/util/api_resource.rb', line 163

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



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

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

#dirty?Boolean

Returns:

  • (Boolean)


185
186
187
# File 'lib/unit-ruby/util/api_resource.rb', line 185

def dirty?
  dirty_attributes.any?
end

#dirty_attributesObject



189
190
191
# File 'lib/unit-ruby/util/api_resource.rb', line 189

def dirty_attributes
  @dirty_attributes ||= []
end

#mark_as_clean!Object



197
198
199
# File 'lib/unit-ruby/util/api_resource.rb', line 197

def mark_as_clean!
  @dirty_attributes = []
end

#mark_attribute_as_dirty(name) ⇒ Object



193
194
195
# File 'lib/unit-ruby/util/api_resource.rb', line 193

def mark_attribute_as_dirty(name)
  dirty_attributes << name
end

#resource_typeObject

The JSON:API type for this resource



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

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



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

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

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

#update_resource_from_json_api(data) ⇒ Object



147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/unit-ruby/util/api_resource.rb', line 147

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

  clear_attributes!

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

  mark_as_clean!
end