Class: Moonrope::Request
- Inherits:
-
Object
- Object
- Moonrope::Request
- Defined in:
- lib/moonrope/request.rb
Class Attribute Summary collapse
-
.path_prefix ⇒ Regex
The initial path of the prefix to be matched.
-
.path_regex ⇒ Regex
The regex which should be matched for all API requests.
Instance Attribute Summary collapse
-
#action_name ⇒ String
readonly
The name of the action which was requested.
-
#controller_name ⇒ String
readonly
The name of the controller which was requested.
-
#env ⇒ Hash
readonly
The rack environment.
-
#identity ⇒ Object
readonly
The authenticated user.
Instance Method Summary collapse
-
#action ⇒ Object
Return the action object for the request.
-
#anonymous? ⇒ Boolean
Is this request to the API anonymous?.
-
#authenticated? ⇒ Boolean
Is this request to the API authenticated?.
-
#controller ⇒ Moonrope::Controller
Return the controller object for the request.
-
#execute ⇒ Moonrope::ActionResult
Execute the appropriate action for the request after running the various authentication checks.
-
#headers ⇒ Rack::Utils::HeaderHash
Return all HTTP headers from the request.
-
#initialize(base, env, path = nil) ⇒ Request
constructor
Initialize a new Moonrope::Request.
-
#ip ⇒ String
Return the remote IP.
-
#params ⇒ Moonrope::ParamSet
Return all user supplier parameters.
-
#ssl? ⇒ Boolean
Is this request on SSL?.
-
#valid? ⇒ Boolean
Return whether or not this request is valid and can continue?.
-
#version ⇒ Integer
Return the requested API version from the request.
Constructor Details
#initialize(base, env, path = nil) ⇒ Request
Initialize a new Moonrope::Request
35 36 37 38 39 40 41 42 |
# File 'lib/moonrope/request.rb', line 35 def initialize(base, env, path = nil) @base = base @env = env if path.nil? && env['PATH_INFO'] =~ self.class.path_regex path = $1 end @version, @controller_name, @action_name = path ? path.split("/") : [nil, nil, nil] end |
Class Attribute Details
.path_prefix ⇒ Regex
Returns the initial path of the prefix to be matched.
14 15 16 |
# File 'lib/moonrope/request.rb', line 14 def path_prefix @path_prefix end |
.path_regex ⇒ Regex
Returns the regex which should be matched for all API requests.
9 10 11 |
# File 'lib/moonrope/request.rb', line 9 def path_regex @path_regex end |
Instance Attribute Details
#action_name ⇒ String (readonly)
Returns the name of the action which was requested.
24 25 26 |
# File 'lib/moonrope/request.rb', line 24 def action_name @action_name end |
#controller_name ⇒ String (readonly)
Returns the name of the controller which was requested.
22 23 24 |
# File 'lib/moonrope/request.rb', line 22 def controller_name @controller_name end |
#env ⇒ Hash (readonly)
Returns the rack environment.
20 21 22 |
# File 'lib/moonrope/request.rb', line 20 def env @env end |
#identity ⇒ Object (readonly)
Returns the authenticated user.
26 27 28 |
# File 'lib/moonrope/request.rb', line 26 def identity @identity end |
Instance Method Details
#action ⇒ Object
Return the action object for the request
return [Moonrope::Action]
78 79 80 |
# File 'lib/moonrope/request.rb', line 78 def action @action ||= controller.actions[action_name.to_sym] end |
#anonymous? ⇒ Boolean
Is this request to the API anonymous?
145 146 147 |
# File 'lib/moonrope/request.rb', line 145 def anonymous? identity.nil? end |
#authenticated? ⇒ Boolean
Is this request to the API authenticated?
154 155 156 |
# File 'lib/moonrope/request.rb', line 154 def authenticated? !(identity.nil? || identity == false) end |
#controller ⇒ Moonrope::Controller
Return the controller object for the request
69 70 71 |
# File 'lib/moonrope/request.rb', line 69 def controller @controller ||= @base.controller(controller_name.to_sym) end |
#execute ⇒ Moonrope::ActionResult
Execute the appropriate action for the request after running the various authentication checks.
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/moonrope/request.rb', line 88 def execute eval_env = EvalEnvironment.new(@base, self, action) if action.authenticator_to_use.is_a?(Moonrope::Authenticator) result = action.convert_errors_to_action_result do if block = action.authenticator_to_use.lookup @identity = eval_env.instance_eval(&block) end unless action.check_access(eval_env) == true if rule = action.authenticator_to_use.rules[action.access_rule_to_use] eval_env.structured_error(rule[:error_code], rule[:description], :action => action.name, :controller => controller.name) else eval_env.structured_error("NotPermitted", "You are not permitted to access #{controller.name}/#{action.name}", :action => action.name, :controller => controller.name) end end end if result.is_a?(Moonrope::ActionResult) return result end elsif action.authenticator_to_use == :not_found raise Moonrope::Errors::MissingAuthenticator, "Wanted to use authenticator that was not defined." end action.execute(eval_env) end |
#headers ⇒ Rack::Utils::HeaderHash
Return all HTTP headers from the request
136 137 138 |
# File 'lib/moonrope/request.rb', line 136 def headers @headers ||= self.class.extract_http_request_headers(@env) end |
#ip ⇒ String
Return the remote IP
163 164 165 |
# File 'lib/moonrope/request.rb', line 163 def ip rack_request.ip end |
#params ⇒ Moonrope::ParamSet
Return all user supplier parameters
121 122 123 124 125 126 127 128 129 |
# File 'lib/moonrope/request.rb', line 121 def params @params ||= begin if @env['CONTENT_TYPE'] && @env['CONTENT_TYPE'] =~ /\Aapplication\/json(;|\z)/i Moonrope::ParamSet.new(rack_request.body.read) else Moonrope::ParamSet.new(rack_request.params['params']) end end end |
#ssl? ⇒ Boolean
Is this request on SSL?
171 172 173 |
# File 'lib/moonrope/request.rb', line 171 def ssl? rack_request.ssl? end |
#valid? ⇒ Boolean
Return whether or not this request is valid and can continue?
60 61 62 |
# File 'lib/moonrope/request.rb', line 60 def valid? !!(version > 0 && [controller_name, action_name].all? { |c| c =~ /\A[\w\-\.]+\z/} && controller && action) end |
#version ⇒ Integer
Return the requested API version from the request
49 50 51 52 53 |
# File 'lib/moonrope/request.rb', line 49 def version version = @version.to_s.gsub(/[^0-9]/, '').to_i version = 1 if version == 0 version end |