Class: Moonrope::Request
- Inherits:
-
Object
- Object
- Moonrope::Request
- Defined in:
- lib/moonrope/request.rb
Class Attribute Summary collapse
-
.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.
-
#authenticated_user ⇒ Object
readonly
The authenticated user.
-
#controller_name ⇒ String
readonly
The name of the controller which was requested.
-
#env ⇒ Hash
readonly
The rack environment.
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.
-
#params ⇒ Moonrope::ParamSet
Return all user supplier parameters.
-
#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
29 30 31 32 33 34 35 36 |
# File 'lib/moonrope/request.rb', line 29 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_regex ⇒ Regex
Returns the regex which should be matched for all API requests.
8 9 10 |
# File 'lib/moonrope/request.rb', line 8 def path_regex @path_regex end |
Instance Attribute Details
#action_name ⇒ String (readonly)
Returns the name of the action which was requested.
18 19 20 |
# File 'lib/moonrope/request.rb', line 18 def action_name @action_name end |
#authenticated_user ⇒ Object (readonly)
Returns the authenticated user.
20 21 22 |
# File 'lib/moonrope/request.rb', line 20 def authenticated_user @authenticated_user end |
#controller_name ⇒ String (readonly)
Returns the name of the controller which was requested.
16 17 18 |
# File 'lib/moonrope/request.rb', line 16 def controller_name @controller_name end |
#env ⇒ Hash (readonly)
Returns the rack environment.
14 15 16 |
# File 'lib/moonrope/request.rb', line 14 def env @env end |
Instance Method Details
#action ⇒ Object
Return the action object for the request
return [Moonrope::Action]
72 73 74 |
# File 'lib/moonrope/request.rb', line 72 def action @action ||= controller.actions[action_name.to_sym] end |
#anonymous? ⇒ Boolean
Is this request to the API anonymous?
128 129 130 |
# File 'lib/moonrope/request.rb', line 128 def anonymous? authenticated_user.nil? end |
#authenticated? ⇒ Boolean
Is this request to the API authenticated?
137 138 139 |
# File 'lib/moonrope/request.rb', line 137 def authenticated? !(authenticated_user.nil? || authenticated_user == false) end |
#controller ⇒ Moonrope::Controller
Return the controller object for the request
63 64 65 |
# File 'lib/moonrope/request.rb', line 63 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.
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/moonrope/request.rb', line 82 def execute eval_env = EvalEnvironment.new(@base, self) if @base.authenticator result = action.convert_errors_to_action_result do @authenticated_user = eval_env.instance_eval(&@base.authenticator) # If we are authenticated, check whether the action permits access to # this user, if not raise an error. if authenticated? unless action.check_access(eval_env) == true raise Moonrope::Errors::AccessDenied, "Access to #{controller.name}/#{action.name} is not permitted." end end end if result.is_a?(Moonrope::ActionResult) # If we already have a result, we should return it and no longer execute # this request. return result end end action.execute(eval_env) end |
#headers ⇒ Rack::Utils::HeaderHash
Return all HTTP headers from the request
119 120 121 |
# File 'lib/moonrope/request.rb', line 119 def headers @headers ||= self.class.extract_http_request_headers(@env) end |
#params ⇒ Moonrope::ParamSet
Return all user supplier parameters
110 111 112 |
# File 'lib/moonrope/request.rb', line 110 def params @params ||= Moonrope::ParamSet.new(rack_request.params['params']) end |
#valid? ⇒ Boolean
Return whether or not this request is valid and can continue?
54 55 56 |
# File 'lib/moonrope/request.rb', line 54 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
43 44 45 46 47 |
# File 'lib/moonrope/request.rb', line 43 def version version = @version.to_s.gsub(/[^0-9]/, '').to_i version = 1 if version == 0 version end |