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.



240
241
242
243
244
245
246
247
248
# File 'lib/simple_jsonapi_client/base.rb', line 240

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

Instance Attribute Details

#contextObject (readonly)

Returns the value of attribute context.



238
239
240
# File 'lib/simple_jsonapi_client/base.rb', line 238

def context
  @context
end

#idObject (readonly)

Returns the value of attribute id.



238
239
240
# File 'lib/simple_jsonapi_client/base.rb', line 238

def id
  @id
end

Class Method Details

._attributesObject



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

def _attributes
  @_attributes ||= {}
end

.attributes(*attrs) ⇒ Object



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

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



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

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

.delete(opts) ⇒ Object



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

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

.fetch(opts) ⇒ Object



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

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

.fetch_all(opts) ⇒ Object



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

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



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

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



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

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



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

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



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

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

.meta(*attrs) ⇒ Object



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

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



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

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



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

def relationships
  @relationships ||= {}
end

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



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

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



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

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

Instance Method Details

#as_jsonObject



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

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

#attributesObject



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

def attributes
  @attributes ||= loaded_record.attributes
end

#deleteObject



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

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

#inspectObject



305
306
307
308
309
310
311
# File 'lib/simple_jsonapi_client/base.rb', line 305

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



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

def meta
  @meta ||= loaded_record.meta
end

#relationshipsObject



266
267
268
269
270
271
272
273
274
275
# File 'lib/simple_jsonapi_client/base.rb', line 266

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

#same_record_as?(other) ⇒ Boolean

Returns:

  • (Boolean)


254
255
256
# File 'lib/simple_jsonapi_client/base.rb', line 254

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

#to_json(*args) ⇒ Object



301
302
303
# File 'lib/simple_jsonapi_client/base.rb', line 301

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

#to_relationshipObject



250
251
252
# File 'lib/simple_jsonapi_client/base.rb', line 250

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

#update(**attrs) ⇒ Object



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

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