Class: Praxis::Request
- Inherits:
-
Object
- Object
- Praxis::Request
- Defined in:
- lib/praxis/request.rb
Constant Summary collapse
- PATH_VERSION_PREFIX =
"/v".freeze
- CONTENT_TYPE_NAME =
'CONTENT_TYPE'.freeze
- PATH_INFO_NAME =
'PATH_INFO'.freeze
- REQUEST_METHOD_NAME =
'REQUEST_METHOD'.freeze
- QUERY_STRING_NAME =
'QUERY_STRING'.freeze
- API_VERSION_HEADER_NAME =
"HTTP_X_API_VERSION".freeze
- API_VERSION_PARAM_NAME =
'api_version'.freeze
- API_NO_VERSION_NAME =
'n/a'.freeze
- VERSION_USING_DEFAULTS =
[:header, :params].freeze
- PATH_VERSION_MATCHER =
%r{^#{self.path_version_prefix}(?<version>[^\/]+)\/}.freeze
Instance Attribute Summary collapse
-
#action ⇒ Object
Returns the value of attribute action.
-
#env ⇒ Object
readonly
Returns the value of attribute env.
-
#headers ⇒ Object
Returns the value of attribute headers.
-
#params ⇒ Object
Returns the value of attribute params.
-
#payload ⇒ Object
Returns the value of attribute payload.
-
#query ⇒ Object
readonly
Returns the value of attribute query.
-
#route_params ⇒ Object
Returns the value of attribute route_params.
Class Method Summary collapse
Instance Method Summary collapse
- #coalesce_inputs! ⇒ Object
-
#content_type ⇒ nil, MediaTypeIdentifier
Determine the content type of this request as indicated by the Content-Type header.
-
#initialize(env) ⇒ Request
constructor
A new instance of Request.
- #load_headers(context) ⇒ Object
- #load_params(context) ⇒ Object
- #load_payload(context) ⇒ Object
-
#media_type ⇒ String
The media type (type/subtype+suffix) portion of the Content-Type header without any media type parameters.
- #params_hash ⇒ Object
- #path ⇒ Object
- #path_version_matcher ⇒ Object
- #raw_params ⇒ Object
- #raw_payload ⇒ Object
-
#unmatched_versions ⇒ Object
versions that matched all the conditions of the request (except its version).
- #validate_headers(context) ⇒ Object
- #validate_params(context) ⇒ Object
- #validate_payload(context) ⇒ Object
- #verb ⇒ Object
- #version(using: VERSION_USING_DEFAULTS) ⇒ Object
Constructor Details
#initialize(env) ⇒ Request
Returns a new instance of Request.
17 18 19 20 21 22 |
# File 'lib/praxis/request.rb', line 17 def initialize(env) @env = env @query = Rack::Utils.parse_nested_query(env[QUERY_STRING_NAME]) @route_params = {} @path_version_matcher = path_version_matcher end |
Instance Attribute Details
#action ⇒ Object
Returns the value of attribute action.
5 6 7 |
# File 'lib/praxis/request.rb', line 5 def action @action end |
#env ⇒ Object (readonly)
Returns the value of attribute env.
4 5 6 |
# File 'lib/praxis/request.rb', line 4 def env @env end |
#headers ⇒ Object
Returns the value of attribute headers.
49 50 51 |
# File 'lib/praxis/request.rb', line 49 def headers @headers end |
#params ⇒ Object
Returns the value of attribute params.
49 50 51 |
# File 'lib/praxis/request.rb', line 49 def params @params end |
#payload ⇒ Object
Returns the value of attribute payload.
49 50 51 |
# File 'lib/praxis/request.rb', line 49 def payload @payload end |
#query ⇒ Object (readonly)
Returns the value of attribute query.
4 5 6 |
# File 'lib/praxis/request.rb', line 4 def query @query end |
#route_params ⇒ Object
Returns the value of attribute route_params.
5 6 7 |
# File 'lib/praxis/request.rb', line 5 def route_params @route_params end |
Class Method Details
.path_version_prefix ⇒ Object
82 83 84 |
# File 'lib/praxis/request.rb', line 82 def self.path_version_prefix PATH_VERSION_PREFIX end |
Instance Method Details
#coalesce_inputs! ⇒ Object
77 78 79 80 |
# File 'lib/praxis/request.rb', line 77 def coalesce_inputs! self.raw_params self.raw_payload end |
#content_type ⇒ nil, MediaTypeIdentifier
Determine the content type of this request as indicated by the Content-Type header.
27 28 29 30 |
# File 'lib/praxis/request.rb', line 27 def content_type header = @env[CONTENT_TYPE_NAME] @content_type ||= (header && MediaTypeIdentifier.load(header)).freeze end |
#load_headers(context) ⇒ Object
110 111 112 113 114 115 116 |
# File 'lib/praxis/request.rb', line 110 def load_headers(context) return unless action.headers defined_headers = action.precomputed_header_keys_for_rack.each_with_object(Hash.new) do |(upper, original), hash| hash[original] = self.env[upper] if self.env.has_key? upper end self.headers = action.headers.load(defined_headers, context) end |
#load_params(context) ⇒ Object
118 119 120 121 |
# File 'lib/praxis/request.rb', line 118 def load_params(context) return unless action.params self.params = action.params.load(self.raw_params, context) end |
#load_payload(context) ⇒ Object
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 |
# File 'lib/praxis/request.rb', line 123 def load_payload(context) return unless action.payload return if content_type.nil? handler = Praxis::Application.instance.handlers[content_type.handler_name] if handler raw = handler.parse(self.raw_payload) else # TODO is this a good default? raw = self.raw_payload end self.payload = action.payload.load(raw, context, content_type: content_type.to_s) end |
#media_type ⇒ String
The media type (type/subtype+suffix) portion of the Content-Type header without any media type parameters. e.g., when Content-Type is “text/plain;charset=utf-8”, the media-type is “text/plain”.
For more information on the use of media types in HTTP, see: www.w3.org/Protocols/rfc2616/rfc2616-sec3.html#sec3.7
41 42 43 |
# File 'lib/praxis/request.rb', line 41 def media_type content_type.without_parameters.to_s end |
#params_hash ⇒ Object
51 52 53 54 |
# File 'lib/praxis/request.rb', line 51 def params_hash return {} if params.nil? params.attributes end |
#path ⇒ Object
45 46 47 |
# File 'lib/praxis/request.rb', line 45 def path @env[PATH_INFO_NAME] end |
#path_version_matcher ⇒ Object
88 89 90 |
# File 'lib/praxis/request.rb', line 88 def path_version_matcher PATH_VERSION_MATCHER end |
#raw_params ⇒ Object
60 61 62 63 64 65 66 |
# File 'lib/praxis/request.rb', line 60 def raw_params @raw_params ||= begin params = query.merge(route_params) params.delete(API_VERSION_PARAM_NAME) params end end |
#raw_payload ⇒ Object
68 69 70 71 72 73 74 75 |
# File 'lib/praxis/request.rb', line 68 def raw_payload @raw_payload ||= begin if (input = env['rack.input'.freeze].read) env['rack.input'.freeze].rewind input end end end |
#unmatched_versions ⇒ Object
versions that matched all the conditions of the request (except its version)
158 159 160 |
# File 'lib/praxis/request.rb', line 158 def unmatched_versions @unmatched_versions ||= Set.new end |
#validate_headers(context) ⇒ Object
139 140 141 142 143 |
# File 'lib/praxis/request.rb', line 139 def validate_headers(context) return [] unless action.headers action.headers.validate(self.headers, context) end |
#validate_params(context) ⇒ Object
145 146 147 148 149 |
# File 'lib/praxis/request.rb', line 145 def validate_params(context) return [] unless action.params action.params.validate(self.params, context) end |
#validate_payload(context) ⇒ Object
151 152 153 154 155 |
# File 'lib/praxis/request.rb', line 151 def validate_payload(context) return [] unless action.payload action.payload.validate(self.payload, context) end |
#verb ⇒ Object
56 57 58 |
# File 'lib/praxis/request.rb', line 56 def verb @env[REQUEST_METHOD_NAME] end |
#version(using: VERSION_USING_DEFAULTS) ⇒ Object
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/praxis/request.rb', line 92 def version(using: VERSION_USING_DEFAULTS) result = nil Array(using).find do |mode| case mode when :header; result = env[API_VERSION_HEADER_NAME] when :params; result = @query[API_VERSION_PARAM_NAME] when :path; m = self.path.match(@path_version_matcher) result = m[:version] unless m.nil? else raise "Unknown method for retrieving the API version: #{mode}" end end return result || API_NO_VERSION_NAME end |