Class: SimpleJSONAPIClient::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/simple_jsonapi_client/base.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(meta: nil, id:, attributes: nil, relationships: nil, included: {}, connection:, context: nil) ⇒ Base

Returns a new instance of Base.



249
250
251
252
253
254
255
256
257
# File 'lib/simple_jsonapi_client/base.rb', line 249

def initialize(meta: nil, id:, attributes: nil, relationships: nil, included: {}, connection:, context: nil)
  @meta = Utils.symbolize_keys(meta) if meta
  @id = id
  @included = included
  @connection = connection
  @context = context
  @attributes = Utils.symbolize_keys(attributes) if attributes
  @input_relationships = relationships
end

Instance Attribute Details

#contextObject (readonly)

Returns the value of attribute context.



247
248
249
# File 'lib/simple_jsonapi_client/base.rb', line 247

def context
  @context
end

#idObject (readonly)

Returns the value of attribute id.



247
248
249
# File 'lib/simple_jsonapi_client/base.rb', line 247

def id
  @id
end

Class Method Details

._attributesObject



13
14
15
# File 'lib/simple_jsonapi_client/base.rb', line 13

def _attributes
  @_attributes ||= {}
end

.attributes(*attrs) ⇒ Object



17
18
19
20
21
22
23
# File 'lib/simple_jsonapi_client/base.rb', line 17

def attributes(*attrs)
  attrs.each do |attr|
    define_method(attr) { attributes[attr] }
    define_method("#{attr}=") { |x| attributes[attr] = x }
    _attributes[attr] = true
  end
end

.create(opts) ⇒ Object



56
57
58
# File 'lib/simple_jsonapi_client/base.rb', line 56

def create(opts)
  operation(:create_request, :singular, opts)
end

.delete(opts) ⇒ Object



64
65
66
67
# File 'lib/simple_jsonapi_client/base.rb', line 64

def delete(opts)
  operation(:delete_request, :empty, opts)
  true
end

.fetch(opts) ⇒ Object



46
47
48
# File 'lib/simple_jsonapi_client/base.rb', line 46

def fetch(opts)
  operation(:fetch_request, :singular, opts)
end

.fetch_all(opts) ⇒ Object



50
51
52
53
54
# File 'lib/simple_jsonapi_client/base.rb', line 50

def fetch_all(opts)
  Redirection::FetchAll.new(opts) do |request_opts|
    operation(:fetch_all_request, :plural, request_opts)
  end
end

.has_many(relationship_name, opts) ⇒ Object



32
33
34
35
36
37
# File 'lib/simple_jsonapi_client/base.rb', line 32

def has_many(relationship_name, opts)
  model_class = opts.fetch(:class) { opts.fetch(:class_name) }
  define_relationship_methods!(relationship_name)
  relationships[relationship_name.to_sym] =
    Relationships::HasManyRelationship.new(model_class)
end

.has_one(relationship_name, opts) ⇒ Object



39
40
41
42
43
44
# File 'lib/simple_jsonapi_client/base.rb', line 39

def has_one(relationship_name, opts)
  define_relationship_methods!(relationship_name)
  model_class = opts.fetch(:class) { opts.fetch(:class_name) }
  relationships[relationship_name.to_sym] =
    Relationships::HasOneRelationship.new(model_class)
end

.include_records(included_hash, records) ⇒ Object



89
90
91
92
93
# File 'lib/simple_jsonapi_client/base.rb', line 89

def include_records(included_hash, records)
  records.to_a.each do |record|
    included_hash[{ 'id' => record['id'], 'type' => record['type'] }] = record
  end
end

.interpreted_included(records, included) ⇒ Object



82
83
84
85
86
87
# File 'lib/simple_jsonapi_client/base.rb', line 82

def interpreted_included(records, included)
  {}.tap do |included_hash|
    include_records(included_hash, records)
    include_records(included_hash, included)
  end
end

.meta(*attrs) ⇒ Object



25
26
27
28
29
30
# File 'lib/simple_jsonapi_client/base.rb', line 25

def meta(*attrs)
  attrs.each do |attr|
    define_method(attr) { meta[attr] }
    define_method("#{attr}=") { |x| meta[attr] = x }
  end
end

.model_from(record, included, connection, context = nil) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/simple_jsonapi_client/base.rb', line 69

def model_from(record, included, connection, context = nil)
  return unless record
  new(
    meta: record['meta'],
    id: record['id'],
    attributes: record.fetch('attributes', {}),
    relationships: record.fetch('relationships', {}),
    context: context,
    included: included,
    connection: connection
  )
end

.relationshipsObject



9
10
11
# File 'lib/simple_jsonapi_client/base.rb', line 9

def relationships
  @relationships ||= {}
end

.template(id: nil, attributes:, relationships: {}) ⇒ Object



95
96
97
98
99
100
101
102
103
# File 'lib/simple_jsonapi_client/base.rb', line 95

def template(id: nil, attributes:, relationships: {})
  data = {
    type: self::TYPE,
    attributes: attributes,
    relationships: interpreted_relationships(relationships)
  }
  data[:id] = id if id
  { data: data }
end

.update(opts) ⇒ Object



60
61
62
# File 'lib/simple_jsonapi_client/base.rb', line 60

def update(opts)
  operation(:update_request, :singular, opts)
end

Instance Method Details

#as_jsonObject



302
303
304
305
306
307
308
# File 'lib/simple_jsonapi_client/base.rb', line 302

def as_json
  self.class.template(
    id: id,
    attributes: attributes,
    relationships: relationships
  )
end

#attributesObject



267
268
269
# File 'lib/simple_jsonapi_client/base.rb', line 267

def attributes
  @attributes ||= loaded_record.attributes
end

#deleteObject



295
296
297
298
299
300
# File 'lib/simple_jsonapi_client/base.rb', line 295

def delete
  self.class.delete(
    connection: connection,
    url_opts: { id: id }
  )
end

#inspectObject



314
315
316
317
318
319
320
# File 'lib/simple_jsonapi_client/base.rb', line 314

def inspect
  parsed_attributes = attributes.map { |key, value| "#{key}=#{value.inspect}" }.join(' ')
  parsed_attributes = " #{parsed_attributes}" unless parsed_attributes.empty?
  parsed_relationships = relationships.map { |key, value| "#{key}=#{value.inspect}" }.join(' ')
  parsed_relationships = " #{parsed_relationships}" unless parsed_relationships.empty?
  "#<#{self.class.name} id=#{id}#{parsed_attributes}#{parsed_relationships}>"
end

#metaObject



271
272
273
# File 'lib/simple_jsonapi_client/base.rb', line 271

def meta
  @meta ||= loaded_record.meta
end

#relationshipsObject



275
276
277
278
279
280
281
282
283
284
# File 'lib/simple_jsonapi_client/base.rb', line 275

def relationships
  @relationships ||=
    begin
      if input_relationships
        relationships_to_models(Utils.symbolize_keys(input_relationships))
      else
        loaded_record.relationships
      end
    end
end

#same_record_as?(other) ⇒ Boolean

Returns:

  • (Boolean)


263
264
265
# File 'lib/simple_jsonapi_client/base.rb', line 263

def same_record_as?(other)
  to_relationship == other.to_relationship
end

#to_json(*args) ⇒ Object



310
311
312
# File 'lib/simple_jsonapi_client/base.rb', line 310

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

#to_relationshipObject



259
260
261
# File 'lib/simple_jsonapi_client/base.rb', line 259

def to_relationship
  { type: self.class::TYPE, id: id }
end

#update(**attrs) ⇒ Object



286
287
288
289
290
291
292
293
# File 'lib/simple_jsonapi_client/base.rb', line 286

def update(**attrs)
   self.class.update(
     connection: connection,
     id: id,
     url_opts: { id: id },
     **attrs
   )
end