Class: TokenAuth::Payload

Inherits:
Object
  • Object
show all
Defined in:
app/models/token_auth/payload.rb

Overview

An inbound resource. Inbound resource requests are validated and an attempt is made to upsert them.

Defined Under Namespace

Classes: MalformedPayloadError

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(entity_id:) ⇒ Payload

Returns a new instance of Payload.



18
19
20
21
22
# File 'app/models/token_auth/payload.rb', line 18

def initialize(entity_id:)
  @entity_id = entity_id
  @valid_resources = []
  @errors = []
end

Instance Attribute Details

#errorsObject (readonly)

Returns the value of attribute errors.



6
7
8
# File 'app/models/token_auth/payload.rb', line 6

def errors
  @errors
end

#valid_resourcesObject (readonly)

Returns the value of attribute valid_resources.



6
7
8
# File 'app/models/token_auth/payload.rb', line 6

def valid_resources
  @valid_resources
end

Class Method Details

.resource_type(params) ⇒ Object



11
12
13
14
15
16
# File 'app/models/token_auth/payload.rb', line 11

def self.resource_type(params)
  params.extract!(:type)[:type]

rescue NoMethodError
  nil
end

Instance Method Details

#save(entities_params) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'app/models/token_auth/payload.rb', line 24

def save(entities_params)
  raise MalformedPayloadError unless entities_params.respond_to?(:each)

  entities_params.each do |entity_params|
    type = self.class.resource_type(entity_params)
    resource = find_pushable_resource(type)

    if resource
      upsert_resource(
        klass: resource.klass,
        params: entity_params.merge(
          "#{ resource.entity_id_attribute_name }": @entity_id
        )
      )
    else
      @errors << "invalid resource '#{ type }'"
    end
  end
end