Class: Moonrope::Request

Inherits:
Object
  • Object
show all
Defined in:
lib/moonrope/request.rb

Class Attribute Summary collapse

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(base, env, path = nil) ⇒ Request

Initialize a new Moonrope::Request

Parameters:

  • base (Moonrope::Base)
  • env (Hash)

    a rack environment has

  • path (String) (defaults to: nil)

    the reqested path (after the /api/ prefix)



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_prefixRegex

Returns the initial path of the prefix to be matched.

Returns:

  • (Regex)

    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_regexRegex

Returns the regex which should be matched for all API requests.

Returns:

  • (Regex)

    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_nameString (readonly)

Returns the name of the action which was requested.

Returns:

  • (String)

    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_nameString (readonly)

Returns the name of the controller which was requested.

Returns:

  • (String)

    the name of the controller which was requested



22
23
24
# File 'lib/moonrope/request.rb', line 22

def controller_name
  @controller_name
end

#envHash (readonly)

Returns the rack environment.

Returns:

  • (Hash)

    the rack environment



20
21
22
# File 'lib/moonrope/request.rb', line 20

def env
  @env
end

#identityObject (readonly)

Returns the authenticated user.

Returns:

  • (Object)

    the authenticated user



26
27
28
# File 'lib/moonrope/request.rb', line 26

def identity
  @identity
end

Instance Method Details

#actionObject

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?

Returns:

  • (Boolean)


145
146
147
# File 'lib/moonrope/request.rb', line 145

def anonymous?
  identity.nil?
end

#authenticated?Boolean

Is this request to the API authenticated?

Returns:

  • (Boolean)


154
155
156
# File 'lib/moonrope/request.rb', line 154

def authenticated?
  !(identity.nil? || identity == false)
end

#controllerMoonrope::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

#executeMoonrope::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

#headersRack::Utils::HeaderHash

Return all HTTP headers from the request

Returns:

  • (Rack::Utils::HeaderHash)


136
137
138
# File 'lib/moonrope/request.rb', line 136

def headers
  @headers ||= self.class.extract_http_request_headers(@env)
end

#ipString

 Return the remote IP

Returns:

  • (String)


163
164
165
# File 'lib/moonrope/request.rb', line 163

def ip
  rack_request.ip
end

#paramsMoonrope::ParamSet

Return all user supplier parameters

Returns:



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?

Returns:

  • (Boolean)


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?

Returns:

  • (Boolean)


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

#versionInteger

Return the requested API version from the request

Returns:

  • (Integer)


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