Class: Api::V1::BaseController
- Inherits:
-
ActionController::API
- Object
- ActionController::API
- Api::V1::BaseController
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.
Direct Known Subclasses
AccountsController, BomController, CanadaPost::ReturnsController, DesignToolFixturesController, EdiMessagesController, EmailCanaryController, EmbeddedAssetsController, FallbackController, ImagesController, IntercomController, LocationsController, MyCarrierTmsMessagesController, OembedController, QuickEstimatorsController
Instance Method Summary
collapse
Instance Method Details
#catalog_for_request ⇒ Object
66
67
68
|
# File 'app/controllers/api/v1/base_controller.rb', line 66
def catalog_for_request
Catalog.locale_to_catalog(locale_for_request.to_sym)
end
|
#error!(error: "An unknown error occurred", status: 500) ⇒ Object
{ message: "Product Category URL [#pc_url] not found" }, 404, 'X-Robots-Tag' => 'noindex'
50
51
52
|
# File 'app/controllers/api/v1/base_controller.rb', line 50
def error!(error: "An unknown error occurred", status: 500)
render json: { error: error }, status: status
end
|
#locale_for_request ⇒ Object
58
59
60
|
# File 'app/controllers/api/v1/base_controller.rb', line 58
def locale_for_request
params[:locale] || I18n.locale || 'en-US'
end
|
#logger ⇒ Object
24
25
26
|
# File 'app/controllers/api/v1/base_controller.rb', line 24
def logger
Rails.logger
end
|
#render_bad_request_response(exception = nil) ⇒ Object
36
37
38
|
# File 'app/controllers/api/v1/base_controller.rb', line 36
def render_bad_request_response(exception = nil)
render json: { error: exception&.message || 'Bad Request' }, status: :bad_request
end
|
#render_internal_server_error(exception = nil) ⇒ Object
40
41
42
43
44
45
46
47
|
# File 'app/controllers/api/v1/base_controller.rb', line 40
def render_internal_server_error(exception = nil)
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) ⇒ Object
28
29
30
|
# File 'app/controllers/api/v1/base_controller.rb', line 28
def render_not_found_response(error)
render json: { error: error.message }, status: :not_found
end
|
#render_result(result, allow_public_cache: true) ⇒ Object
76
77
78
79
80
|
# File 'app/controllers/api/v1/base_controller.rb', line 76
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) ⇒ Object
32
33
34
|
# File 'app/controllers/api/v1/base_controller.rb', line 32
def render_unprocessable_entity_response(exception)
render json: exception.record.errors, status: :unprocessable_content
end
|
#set_locale ⇒ Object
62
63
64
|
# File 'app/controllers/api/v1/base_controller.rb', line 62
def set_locale
CurrentScope.locale = locale_for_request.to_sym
end
|
#store_for_request ⇒ Object
54
55
56
|
# File 'app/controllers/api/v1/base_controller.rb', line 54
def store_for_request
Store.locale_to_catalog(locale_for_request)
end
|
#underscore_params ⇒ Object
70
71
72
73
74
|
# File 'app/controllers/api/v1/base_controller.rb', line 70
def underscore_params
self.params = params.to_h.each_with_object({}) do |(k, v), hsh|
hsh[k.underscore.to_sym] = v
end
end
|