Class: RESTRack::ResourceRequest
- Defined in:
- lib/restrack/resource_request.rb
Overview
The ResourceRequest class handles all incoming requests.
Instance Attribute Summary collapse
-
#active_controller ⇒ Object
readonly
Returns the value of attribute active_controller.
-
#get_params ⇒ Object
readonly
Returns the value of attribute get_params.
-
#headers ⇒ Object
readonly
Returns the value of attribute headers.
-
#mime_type ⇒ Object
Returns the value of attribute mime_type.
-
#params ⇒ Object
readonly
Returns the value of attribute params.
-
#post_params ⇒ Object
readonly
Returns the value of attribute post_params.
-
#request ⇒ Object
readonly
Returns the value of attribute request.
-
#request_id ⇒ Object
readonly
Returns the value of attribute request_id.
-
#url_chain ⇒ Object
Returns the value of attribute url_chain.
Instance Method Summary collapse
-
#call_controller(resource_name) ⇒ Object
Call the next entity in the path stack.
- #content_type ⇒ Object
-
#initialize(opts) ⇒ ResourceRequest
constructor
Initialize the ResourceRequest by assigning a request_id and determining the path, format, and controller of the resource.
- #log_request_params(params_hash) ⇒ Object
- #prepare ⇒ Object
- #requires_async_defer ⇒ Object
- #requires_async_defer=(boolean) ⇒ Object
Constructor Details
#initialize(opts) ⇒ ResourceRequest
Initialize the ResourceRequest by assigning a request_id and determining the path, format, and controller of the resource. Accepting options to allow us to override request_id for testing.
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/restrack/resource_request.rb', line 9 def initialize(opts) @request = opts[:request] @request_id = opts[:request_id] || get_request_id # Write input details to logs RESTRack.request_log.info "{#{@request_id}} #{@request.request_method} #{@request.path_info} requested from #{@request.ip}" @headers = Rack::Utils::HeaderHash.new @request.env.select {|k,v| k.start_with? 'HTTP_'}.each do |k,v| @headers[k.sub(/^HTTP_/, '')] = v end # MIME type should be determined before raising any exceptions for proper error reporting # Set up the initial routing. @url_chain = @request.path_info.split('/') @url_chain.shift if @url_chain[0] == '' # Pull extension from URL extension = '' unless @url_chain[-1].nil? @url_chain[-1] = @url_chain[-1].sub(/\.([^.]*)$/) do |s| extension = $1.downcase '' # Return an empty string as the substitution so that the extension is removed from `@url_chain[-1]` end end # Determine MIME type from extension @mime_type = get_mime_type_from( extension ) end |
Instance Attribute Details
#active_controller ⇒ Object (readonly)
Returns the value of attribute active_controller.
4 5 6 |
# File 'lib/restrack/resource_request.rb', line 4 def active_controller @active_controller end |
#get_params ⇒ Object (readonly)
Returns the value of attribute get_params.
4 5 6 |
# File 'lib/restrack/resource_request.rb', line 4 def get_params @get_params end |
#headers ⇒ Object (readonly)
Returns the value of attribute headers.
4 5 6 |
# File 'lib/restrack/resource_request.rb', line 4 def headers @headers end |
#mime_type ⇒ Object
Returns the value of attribute mime_type.
5 6 7 |
# File 'lib/restrack/resource_request.rb', line 5 def mime_type @mime_type end |
#params ⇒ Object (readonly)
Returns the value of attribute params.
4 5 6 |
# File 'lib/restrack/resource_request.rb', line 4 def params @params end |
#post_params ⇒ Object (readonly)
Returns the value of attribute post_params.
4 5 6 |
# File 'lib/restrack/resource_request.rb', line 4 def post_params @post_params end |
#request ⇒ Object (readonly)
Returns the value of attribute request.
4 5 6 |
# File 'lib/restrack/resource_request.rb', line 4 def request @request end |
#request_id ⇒ Object (readonly)
Returns the value of attribute request_id.
4 5 6 |
# File 'lib/restrack/resource_request.rb', line 4 def request_id @request_id end |
#url_chain ⇒ Object
Returns the value of attribute url_chain.
5 6 7 |
# File 'lib/restrack/resource_request.rb', line 5 def url_chain @url_chain end |
Instance Method Details
#call_controller(resource_name) ⇒ Object
Call the next entity in the path stack. Method called by controller relationship methods.
80 81 82 83 84 |
# File 'lib/restrack/resource_request.rb', line 80 def call_controller(resource_name) @active_resource_name = resource_name @active_controller = instantiate_controller( resource_name.to_s.camelize ) @active_controller.call end |
#content_type ⇒ Object
86 87 88 |
# File 'lib/restrack/resource_request.rb', line 86 def content_type @mime_type.to_s end |
#log_request_params(params_hash) ⇒ Object
68 69 70 71 72 73 74 75 76 |
# File 'lib/restrack/resource_request.rb', line 68 def log_request_params(params_hash) params_to_log = params_hash.clone if RESTRack::CONFIG[:PARAMS_NOT_LOGGABLE] params_to_log.each_key do |param| params_to_log[param] = '*****' if RESTRack::CONFIG[:PARAMS_NOT_LOGGABLE].include?(param.to_s) end end RESTRack.request_log.debug 'Combined Request Params: ' + params_to_log.inspect end |
#prepare ⇒ Object
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 |
# File 'lib/restrack/resource_request.rb', line 34 def prepare # Now safe to raise exceptions raise HTTP400BadRequest, "Request path of #{@request.path_info} is invalid" if @request.path_info.include?('//') # For CORS support if RESTRack::CONFIG[:CORS] raise HTTP403Forbidden if @headers['Origin'].nil? raise HTTP403Forbidden unless RESTRack::CONFIG[:CORS]['Access-Control-Allow-Origin'] == '*' or RESTRack::CONFIG[:CORS]['Access-Control-Allow-Origin'].include?(@headers['Origin']) raise HTTP403Forbidden unless @request.env['REQUEST_METHOD'] == 'OPTIONS' or RESTRack::CONFIG[:CORS]['Access-Control-Allow-Methods'] == '*' or RESTRack::CONFIG[:CORS]['Access-Control-Allow-Methods'].include?(@request.env['REQUEST_METHOD']) end # Pull input data from POST body @post_params = parse_body( @request ) @get_params = parse_query_string( @request ) @params = {} if @post_params.respond_to?(:merge) @params = @post_params.merge( @get_params ) else @params = @get_params end @params.symbolize! log_request_params(@params) # Pull first controller from URL @active_resource_name = @url_chain.shift unless @active_resource_name.nil? or RESTRack.controller_exists?(@active_resource_name) @url_chain.unshift( @active_resource_name ) end if @active_resource_name.nil? or not RESTRack.controller_exists?(@active_resource_name) raise HTTP404ResourceNotFound unless RESTRack::CONFIG[:DEFAULT_RESOURCE] @active_resource_name = RESTRack::CONFIG[:DEFAULT_RESOURCE] end raise HTTP403Forbidden unless RESTRack::CONFIG[:ROOT_RESOURCE_ACCEPT].blank? or RESTRack::CONFIG[:ROOT_RESOURCE_ACCEPT].include?(@active_resource_name) raise HTTP403Forbidden if not RESTRack::CONFIG[:ROOT_RESOURCE_DENY].blank? and RESTRack::CONFIG[:ROOT_RESOURCE_DENY].include?(@active_resource_name) @active_controller = instantiate_controller( @active_resource_name ) end |
#requires_async_defer ⇒ Object
90 91 92 |
# File 'lib/restrack/resource_request.rb', line 90 def requires_async_defer @requires_async_defer ||= RESTRack::CONFIG[:REQUIRES_ASYNC_DEFER] || false end |
#requires_async_defer=(boolean) ⇒ Object
94 95 96 |
# File 'lib/restrack/resource_request.rb', line 94 def requires_async_defer=(boolean) @require_async_defer = boolean end |