Class: RailsSpotlight::Middlewares::Handlers::BaseActionHandler

Inherits:
Object
  • Object
show all
Defined in:
lib/rails_spotlight/middlewares/handlers/base_action_handler.rb

Constant Summary collapse

BaseError =
Class.new(StandardError) do
  attr_reader :code, :status

  def initialize(message = nil, code: nil)
    super(message)
    @code = code
    @status = case self.class.name.demodulize.underscore
              when 'unsupported_media_type' then 415
              when 'not_found' then 404
              when 'unprocessable_entity' then 422
              when 'forbidden' then 403
              else 500
              end
  end
end
UnsupportedMediaType =
Class.new(BaseError)
NotFound =
Class.new(BaseError)
UnprocessableEntity =
Class.new(BaseError)
Forbidden =
Class.new(BaseError)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(request_id, request, content_type) ⇒ BaseActionHandler

Returns a new instance of BaseActionHandler.



27
28
29
30
31
# File 'lib/rails_spotlight/middlewares/handlers/base_action_handler.rb', line 27

def initialize(request_id, request, content_type)
  @request_id = request_id
  @request = request
  @content_type = content_type
end

Instance Attribute Details

#content_typeObject (readonly)

Returns the value of attribute content_type.



33
34
35
# File 'lib/rails_spotlight/middlewares/handlers/base_action_handler.rb', line 33

def content_type
  @content_type
end

#requestObject (readonly)

Returns the value of attribute request.



33
34
35
# File 'lib/rails_spotlight/middlewares/handlers/base_action_handler.rb', line 33

def request
  @request
end

#request_idObject (readonly)

Returns the value of attribute request_id.



33
34
35
# File 'lib/rails_spotlight/middlewares/handlers/base_action_handler.rb', line 33

def request_id
  @request_id
end

Instance Method Details

#callObject



35
36
37
38
39
40
41
42
43
44
# File 'lib/rails_spotlight/middlewares/handlers/base_action_handler.rb', line 35

def call
  validate_data_access! if data_access_token
  validate_project! unless skip_project_validation?
  execute
  response
rescue BaseError => e
  error_response(e)
rescue => e # rubocop:disable Style/RescueStandardError
  error_response(BaseError.new(e.message))
end