Class: FunWithJsonApi::SchemaValidators::CheckResourceIsAuthorised

Inherits:
Object
  • Object
show all
Defined in:
lib/fun_with_json_api/schema_validators/check_resource_is_authorized.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(resource, resource_id, deserializer, resource_pointer: '/data') ⇒ CheckResourceIsAuthorised

Returns a new instance of CheckResourceIsAuthorised.



15
16
17
18
19
20
# File 'lib/fun_with_json_api/schema_validators/check_resource_is_authorized.rb', line 15

def initialize(resource, resource_id, deserializer, resource_pointer: '/data')
  @resource = resource
  @resource_id = resource_id
  @deserializer = deserializer
  @resource_pointer = resource_pointer
end

Instance Attribute Details

#deserializerObject (readonly)

Returns the value of attribute deserializer.



12
13
14
# File 'lib/fun_with_json_api/schema_validators/check_resource_is_authorized.rb', line 12

def deserializer
  @deserializer
end

#resourceObject (readonly)

Returns the value of attribute resource.



10
11
12
# File 'lib/fun_with_json_api/schema_validators/check_resource_is_authorized.rb', line 10

def resource
  @resource
end

#resource_idObject (readonly)

Returns the value of attribute resource_id.



11
12
13
# File 'lib/fun_with_json_api/schema_validators/check_resource_is_authorized.rb', line 11

def resource_id
  @resource_id
end

#resource_pointerObject (readonly)

Returns the value of attribute resource_pointer.



13
14
15
# File 'lib/fun_with_json_api/schema_validators/check_resource_is_authorized.rb', line 13

def resource_pointer
  @resource_pointer
end

Class Method Details

.call(*args) ⇒ Object



6
7
8
# File 'lib/fun_with_json_api/schema_validators/check_resource_is_authorized.rb', line 6

def self.call(*args)
  new(*args).call
end

Instance Method Details

#callObject



22
23
24
25
26
27
28
29
30
31
32
# File 'lib/fun_with_json_api/schema_validators/check_resource_is_authorized.rb', line 22

def call
  unless deserializer.resource_authorizer.call(resource)
    raise Exceptions::UnauthorizedResource.new(
      "resource_authorizer method for '#{deserializer.type}' returned a false value",
      ExceptionPayload.new(
        pointer: resource_pointer,
        detail: unauthorized_resource_message
      )
    )
  end
end

#resource_typeObject



34
35
36
# File 'lib/fun_with_json_api/schema_validators/check_resource_is_authorized.rb', line 34

def resource_type
  deserializer.type
end