Class: Apes::Controller
- Inherits:
-
ActionController::API
- Object
- ActionController::API
- Apes::Controller
- Includes:
- ActionController::ImplicitRender, ActionView::Layouts, Apes::Concerns::Errors, Apes::Concerns::Pagination, Apes::Concerns::Request, Apes::Concerns::Response
- Defined in:
- lib/apes/controller.rb
Overview
A ready to use controller for JSON API applications.
Constant Summary
Constants included from Apes::Concerns::Errors
Apes::Concerns::Errors::ERROR_HANDLERS
Constants included from Apes::Concerns::Request
Apes::Concerns::Request::CONTENT_TYPE
Instance Attribute Summary collapse
-
#current_account ⇒ Object
readonly
The current account making the request.
-
#cursor ⇒ Apes::PaginationCursor
readonly
The pagination cursor for this request.
-
#request_cursor ⇒ Apes::PaginationCursor
readonly
The original pagination cursor sent from the client.
Attributes included from Apes::Concerns::Response
Instance Method Summary collapse
-
#default_url_options ⇒ Hash
Returns the default URL options for this request.
-
#handle_cors ⇒ Object
Tiny handle to handle CORS OPTIONS requests.
-
#render_error(status, errors) ⇒ Object
Default handler to render errors.
Methods included from Apes::Concerns::Errors
#error_handle_bad_request, #error_handle_debug, #error_handle_exception, #error_handle_fordidden, #error_handle_general, #error_handle_invalid_data, #error_handle_missing_data, #error_handle_not_found, #error_handle_others, #error_handle_unknown_attribute, #error_handle_validation, #fail_request!
Methods included from Apes::Concerns::Pagination
#paginate, #pagination_field, #pagination_skip?, #pagination_supported?, #pagination_url
Methods included from Apes::Concerns::Response
#response_data, #response_include, #response_included, #response_links, #response_meta, #response_template_for, #response_timestamp
Methods included from Apes::Concerns::Request
#request_cast_attributes, #request_extract_model, #request_handle_cors, #request_source_host, #request_valid_content_type, #request_validate
Instance Attribute Details
#current_account ⇒ Object (readonly)
Returns The current account making the request.
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/apes/controller.rb', line 15 class Controller < ActionController::API include ActionController::ImplicitRender include ActionView::Layouts include Apes::Concerns::Request include Apes::Concerns::Response include Apes::Concerns::Pagination include Apes::Concerns::Errors layout "general" before_filter :request_handle_cors before_filter :request_validate attr_reader :current_account, :cursor, :request_cursor # Exception handling rescue_from Exception, with: :error_handle_exception # This allows to avoid to declare all the views rescue_from ActionView::MissingTemplate, with: :render_default_views # Returns the default URL options for this request. # It ensures that the host is always included and that is set properly in development mode. # # @return [Hash] Default URL options for the request. def rv = {only_path: false} rv = {host: request_source_host} if Apes::RuntimeConfiguration.development? rv end # Tiny handle to handle CORS OPTIONS requests. It just renders nothing as headers are handle in Apes::Concerns::Response module. # # To enable this route, add the following to the routes.rb: # # # This is to enable AJAX cross domain # match '*path', to: 'application#handle_cors', via: :options def handle_cors render(nothing: true, status: :no_content) end # Default handler to render errors. # # @param status [Symbol|Fixnum] The HTTP error code to return. # @param errors [Array] The list of occurred errors. def render_error(status, errors) @errors = errors status_code = status.is_a?(Fixnum) ? status : Rack::Utils::SYMBOL_TO_STATUS_CODE.fetch(status.to_sym, 500) render("errors/#{status_code}", status: status) end private # :nodoc: def render_default_views(exception) if defined?(@objects) render "/collection" elsif defined?(@object) render "/object" else error_handle_exception(exception) end end end |
#cursor ⇒ Apes::PaginationCursor (readonly)
Returns The pagination cursor for this request.
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/apes/controller.rb', line 15 class Controller < ActionController::API include ActionController::ImplicitRender include ActionView::Layouts include Apes::Concerns::Request include Apes::Concerns::Response include Apes::Concerns::Pagination include Apes::Concerns::Errors layout "general" before_filter :request_handle_cors before_filter :request_validate attr_reader :current_account, :cursor, :request_cursor # Exception handling rescue_from Exception, with: :error_handle_exception # This allows to avoid to declare all the views rescue_from ActionView::MissingTemplate, with: :render_default_views # Returns the default URL options for this request. # It ensures that the host is always included and that is set properly in development mode. # # @return [Hash] Default URL options for the request. def rv = {only_path: false} rv = {host: request_source_host} if Apes::RuntimeConfiguration.development? rv end # Tiny handle to handle CORS OPTIONS requests. It just renders nothing as headers are handle in Apes::Concerns::Response module. # # To enable this route, add the following to the routes.rb: # # # This is to enable AJAX cross domain # match '*path', to: 'application#handle_cors', via: :options def handle_cors render(nothing: true, status: :no_content) end # Default handler to render errors. # # @param status [Symbol|Fixnum] The HTTP error code to return. # @param errors [Array] The list of occurred errors. def render_error(status, errors) @errors = errors status_code = status.is_a?(Fixnum) ? status : Rack::Utils::SYMBOL_TO_STATUS_CODE.fetch(status.to_sym, 500) render("errors/#{status_code}", status: status) end private # :nodoc: def render_default_views(exception) if defined?(@objects) render "/collection" elsif defined?(@object) render "/object" else error_handle_exception(exception) end end end |
#request_cursor ⇒ Apes::PaginationCursor (readonly)
Returns The original pagination cursor sent from the client.
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/apes/controller.rb', line 15 class Controller < ActionController::API include ActionController::ImplicitRender include ActionView::Layouts include Apes::Concerns::Request include Apes::Concerns::Response include Apes::Concerns::Pagination include Apes::Concerns::Errors layout "general" before_filter :request_handle_cors before_filter :request_validate attr_reader :current_account, :cursor, :request_cursor # Exception handling rescue_from Exception, with: :error_handle_exception # This allows to avoid to declare all the views rescue_from ActionView::MissingTemplate, with: :render_default_views # Returns the default URL options for this request. # It ensures that the host is always included and that is set properly in development mode. # # @return [Hash] Default URL options for the request. def rv = {only_path: false} rv = {host: request_source_host} if Apes::RuntimeConfiguration.development? rv end # Tiny handle to handle CORS OPTIONS requests. It just renders nothing as headers are handle in Apes::Concerns::Response module. # # To enable this route, add the following to the routes.rb: # # # This is to enable AJAX cross domain # match '*path', to: 'application#handle_cors', via: :options def handle_cors render(nothing: true, status: :no_content) end # Default handler to render errors. # # @param status [Symbol|Fixnum] The HTTP error code to return. # @param errors [Array] The list of occurred errors. def render_error(status, errors) @errors = errors status_code = status.is_a?(Fixnum) ? status : Rack::Utils::SYMBOL_TO_STATUS_CODE.fetch(status.to_sym, 500) render("errors/#{status_code}", status: status) end private # :nodoc: def render_default_views(exception) if defined?(@objects) render "/collection" elsif defined?(@object) render "/object" else error_handle_exception(exception) end end end |
Instance Method Details
#default_url_options ⇒ Hash
Returns the default URL options for this request. It ensures that the host is always included and that is set properly in development mode.
38 39 40 41 42 |
# File 'lib/apes/controller.rb', line 38 def rv = {only_path: false} rv = {host: request_source_host} if Apes::RuntimeConfiguration.development? rv end |
#handle_cors ⇒ Object
Tiny handle to handle CORS OPTIONS requests. It just renders nothing as headers are handle in Apes::Concerns::Response module.
To enable this route, add the following to the routes.rb:
# This is to enable AJAX cross domain
match '*path', to: 'application#handle_cors', via: :options
50 51 52 |
# File 'lib/apes/controller.rb', line 50 def handle_cors render(nothing: true, status: :no_content) end |
#render_error(status, errors) ⇒ Object
Default handler to render errors.
58 59 60 61 62 |
# File 'lib/apes/controller.rb', line 58 def render_error(status, errors) @errors = errors status_code = status.is_a?(Fixnum) ? status : Rack::Utils::SYMBOL_TO_STATUS_CODE.fetch(status.to_sym, 500) render("errors/#{status_code}", status: status) end |