Class: Cathode::Request
- Inherits:
-
Object
- Object
- Cathode::Request
- Defined in:
- lib/cathode/request.rb
Overview
A ‘Request` object is created when a Rails request is intercepted by Cathode. This object is responsible for enforcing version headers as well as token authorization headers. After header enforcement, the object figures out which version and resource/action to use to process the request. Finally, the request’s ‘_body` (HTTP response body) and `_status` (HTTP status code) are set and rendered from the controller that initiated the request.
Direct Known Subclasses
CreateRequest, CustomRequest, DestroyRequest, IndexRequest, ShowRequest, UpdateRequest
Instance Attribute Summary collapse
-
#_body ⇒ Object
readonly
Returns the value of attribute _body.
-
#_status ⇒ Object
readonly
Returns the value of attribute _status.
-
#action ⇒ Object
readonly
Returns the value of attribute action.
-
#context ⇒ Object
readonly
Returns the value of attribute context.
-
#custom_logic ⇒ Object
readonly
Returns the value of attribute custom_logic.
-
#resource ⇒ Object
readonly
Returns the value of attribute resource.
Class Method Summary collapse
-
.create(context) ⇒ IndexRequest, ...
Creates a request by initializing the appropriate subclass.
Instance Method Summary collapse
-
#initialize(context) ⇒ Request
constructor
Initializes the request.
Constructor Details
#initialize(context) ⇒ Request
Initializes the request
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 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/cathode/request.rb', line 48 def initialize(context) @context = context version_number = context.request.headers['HTTP_ACCEPT_VERSION'] if version_number.nil? @_status, @_body = :bad_request, 'A version number must be passed in the Accept-Version header' return self end version = Version.find(version_number) unless version.present? @_status, @_body = :bad_request, "Unknown API version: #{version_number}" return self end action_name = params[:action] if action_name == 'custom' action_name = context.request.path.split('/').last end params[:controller].slice! 'cathode/' resources = params[:controller].split('_').map(&:to_sym) resource = version._resources.find(resources.first) @resource_tree = [resource] subresources = resources.drop(1).collect do |r| resource = resource._resources.find(r) end @resource_tree += subresources resource = @resource_tree.last @resource = resource if @resource_tree.size > 1 @action = resource.actions.find(action_name.to_sym) else unless version.action?(resource.try(:name) || '', action_name) @_status = :not_found return self end @action = version._resources.find(resource.name).actions.find(action_name.to_sym) end @strong_params = @action.strong_params @_status = :ok if action.override_block context.instance_eval(&action.override_block) else action_block = action.action_block if action_block.nil? && respond_to?(:default_action_block) action_block = default_action_block end instance_eval(&action_block) end body if @_body.nil? end |
Instance Attribute Details
#_body ⇒ Object (readonly)
Returns the value of attribute _body.
10 11 12 |
# File 'lib/cathode/request.rb', line 10 def _body @_body end |
#_status ⇒ Object (readonly)
Returns the value of attribute _status.
10 11 12 |
# File 'lib/cathode/request.rb', line 10 def _status @_status end |
#action ⇒ Object (readonly)
Returns the value of attribute action.
10 11 12 |
# File 'lib/cathode/request.rb', line 10 def action @action end |
#context ⇒ Object (readonly)
Returns the value of attribute context.
10 11 12 |
# File 'lib/cathode/request.rb', line 10 def context @context end |
#custom_logic ⇒ Object (readonly)
Returns the value of attribute custom_logic.
10 11 12 |
# File 'lib/cathode/request.rb', line 10 def custom_logic @custom_logic end |
#resource ⇒ Object (readonly)
Returns the value of attribute resource.
10 11 12 |
# File 'lib/cathode/request.rb', line 10 def resource @resource end |
Class Method Details
.create(context) ⇒ IndexRequest, ...
Creates a request by initializing the appropriate subclass.
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/cathode/request.rb', line 27 def create(context) klass = case context.params[:action].to_sym when :index IndexRequest when :show ShowRequest when :create CreateRequest when :update UpdateRequest when :destroy DestroyRequest else CustomRequest end klass.new(context) end |