Class: Api::V1::BaseController
- Inherits:
-
ActionController::API
- Object
- ActionController::API
- Api::V1::BaseController
- 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.
Direct Known Subclasses
AccountsController, BomController, CanadaPost::ReturnsController, DesignToolFixturesController, EdiMessagesController, EmailCanaryController, EmbeddedAssetsController, FallbackController, ImagesController, IntercomController, LocationsController, MyCarrierTmsMessagesController, OembedController, QuickEstimatorsController
Instance Method Summary collapse
-
#catalog_for_request ⇒ Catalog
protected
The catalog for the request's locale.
-
#error!(error: "An unknown error occurred", status: 500) ⇒ void
protected
Renders a JSON
{ error: }body with the given HTTP status. -
#locale_for_request ⇒ String
protected
The request's
localeparam, or the current I18n locale. -
#logger ⇒ Logger
protected
The Rails application logger.
-
#render_bad_request_response(exception = nil) ⇒ void
protected
rescue_fromhandler for malformed-request errors (bad params, unparsable JSON body). -
#render_internal_server_error(exception = nil) ⇒ void
protected
Catch-all
rescue_from StandardErrorhandler. -
#render_not_found_response(error) ⇒ void
protected
rescue_from ActiveRecord::RecordNotFoundhandler. -
#render_result(result, allow_public_cache: true) ⇒ void
protected
Renders a service-object result as JSON, honoring its cache headers, or renders its error when it failed.
-
#render_unprocessable_entity_response(exception) ⇒ void
protected
rescue_from ActiveRecord::RecordInvalidhandler. -
#set_locale ⇒ void
protected
prepend_before_action— sets CurrentScope#locale from #locale_for_request. -
#store_for_request ⇒ Catalog
protected
The catalog for the request's locale.
-
#underscore_params ⇒ void
protected
before_action— rewrites incoming param keys to underscore_case so camelCase clients don't need bespoke handling downstream.
Instance Method Details
#catalog_for_request ⇒ Catalog (protected)
Returns 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'
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_request ⇒ String (protected)
Returns 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 |
#logger ⇒ Logger (protected)
Returns 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).
50 51 52 |
# File 'app/controllers/api/v1/base_controller.rb', line 50 def render_bad_request_response(exception = nil) render json: { error: exception&. || '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.
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&.}") 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.
33 34 35 |
# File 'app/controllers/api/v1/base_controller.rb', line 33 def render_not_found_response(error) render json: { error: error. }, 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.
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.) && 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.
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_locale ⇒ void (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_request ⇒ Catalog (protected)
Returns 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_params ⇒ void (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 |