Class: Api::V1::BaseController

Inherits:
ActionController::API
  • Object
show all
Defined in:
app/controllers/api/v1/base_controller.rb

Overview

Base controller for all API endpoints.

Extends ActionController::API (not ApplicationController) to keep API requests
lightweight and isolated from web session handling, guest user creation,
and HTML rendering.

All API controllers should inherit from this class.

Instance Method Summary collapse

Instance Method Details

#catalog_for_requestCatalog (protected)

Returns the catalog for the request's locale.

Returns:

  • (Catalog)

    the catalog for the request's locale



98
99
100
# File 'app/controllers/api/v1/base_controller.rb', line 98

def catalog_for_request
  Catalog.locale_to_catalog(locale_for_request.to_sym)
end

#error!(error: "An unknown error occurred", status: 500) ⇒ void (protected)

This method returns an undefined value.

Renders a JSON { error: } body with the given HTTP status. Called
directly by subclasses for ad hoc error responses outside the
rescue_from chain.

{ message: "Product Category URL [#{pc_url}] not found" }, 404, 'X-Robots-Tag' => 'noindex'

Parameters:

  • error (String) (defaults to: "An unknown error occurred")

    the error message to render

  • status (Integer, Symbol) (defaults to: 500)

    the HTTP status to respond with



76
77
78
# File 'app/controllers/api/v1/base_controller.rb', line 76

def error!(error: "An unknown error occurred", status: 500)
  render json: { error: error }, status: status
end

#locale_for_requestString (protected)

Returns the request's locale param, or the current I18n locale.

Returns:

  • (String)

    the request's locale param, or the current I18n locale



86
87
88
# File 'app/controllers/api/v1/base_controller.rb', line 86

def locale_for_request
  params[:locale] || I18n.locale || 'en-US'
end

#loggerLogger (protected)

Returns the Rails application logger.

Returns:

  • (Logger)

    the Rails application logger



25
26
27
# File 'app/controllers/api/v1/base_controller.rb', line 25

def logger
  Rails.logger
end

#render_bad_request_response(exception = nil) ⇒ void (protected)

This method returns an undefined value.

rescue_from handler for malformed-request errors (bad params, unparsable
JSON body).

Parameters:

  • exception (StandardError, nil) (defaults to: nil)

    the raised error, if any



50
51
52
# File 'app/controllers/api/v1/base_controller.rb', line 50

def render_bad_request_response(exception = nil)
  render json: { error: exception&.message || 'Bad Request' }, status: :bad_request
end

#render_internal_server_error(exception = nil) ⇒ void (protected)

This method returns an undefined value.

Catch-all rescue_from StandardError handler.

Parameters:

  • exception (StandardError, nil) (defaults to: nil)

    the raised error, if any



58
59
60
61
62
63
64
65
# File 'app/controllers/api/v1/base_controller.rb', line 58

def render_internal_server_error(exception = nil)
  # Report to AppSignal for monitoring
  ErrorReporting.error(exception) if exception && defined?(ErrorReporting)
  Rails.logger.error("[API Error] #{exception&.class}: #{exception&.message}")
  Rails.logger.error(exception&.backtrace&.first(10)&.join("\n")) if exception&.backtrace

  render json: { error: 'Internal Server Error' }, status: :internal_server_error
end

#render_not_found_response(error) ⇒ void (protected)

This method returns an undefined value.

rescue_from ActiveRecord::RecordNotFound handler.

Parameters:

  • error (ActiveRecord::RecordNotFound)

    the raised error



33
34
35
# File 'app/controllers/api/v1/base_controller.rb', line 33

def render_not_found_response(error)
  render json: { error: error.message }, status: :not_found
end

#render_result(result, allow_public_cache: true) ⇒ void (protected)

This method returns an undefined value.

Renders a service-object result as JSON, honoring its cache headers, or
renders its error when it failed.

Parameters:

  • result (#error_status, #error_message, #output, #etag, #last_modified)

    a service-object result

  • allow_public_cache (Boolean) (defaults to: true)

    whether the response may be cached by shared/public caches



118
119
120
121
122
# File 'app/controllers/api/v1/base_controller.rb', line 118

def render_result(result, allow_public_cache: true)
  error!(status: result.error_status, error: result.error_message) && return if result.error_status

  render json: result.output if stale?(etag: result.etag, last_modified: result.last_modified, public: allow_public_cache)
end

#render_unprocessable_entity_response(exception) ⇒ void (protected)

This method returns an undefined value.

rescue_from ActiveRecord::RecordInvalid handler.

Parameters:

  • exception (ActiveRecord::RecordInvalid)

    the raised error



41
42
43
# File 'app/controllers/api/v1/base_controller.rb', line 41

def render_unprocessable_entity_response(exception)
  render json: exception.record.errors, status: :unprocessable_content
end

#set_localevoid (protected)

This method returns an undefined value.

prepend_before_action — sets CurrentScope#locale from #locale_for_request.



93
94
95
# File 'app/controllers/api/v1/base_controller.rb', line 93

def set_locale
  CurrentScope.locale = locale_for_request.to_sym
end

#store_for_requestCatalog (protected)

Returns the catalog for the request's locale.

Returns:

  • (Catalog)

    the catalog for the request's locale



81
82
83
# File 'app/controllers/api/v1/base_controller.rb', line 81

def store_for_request
  Store.locale_to_catalog(locale_for_request)
end

#underscore_paramsvoid (protected)

This method returns an undefined value.

before_action — rewrites incoming param keys to underscore_case so
camelCase clients don't need bespoke handling downstream.



106
107
108
109
110
# File 'app/controllers/api/v1/base_controller.rb', line 106

def underscore_params
  self.params = params.to_h.each_with_object({}) do |(k, v), hsh|
    hsh[k.underscore.to_sym] = v
  end
end