class Moonrope::Request

Attributes

path_prefix[RW]
path_regex[RW]
action_name[R]

@return [String] the name of the action which was requested

controller_name[R]

@return [String] the name of the controller which was requested

env[R]

@return [Hash] the rack environment

identity[R]

@return [Object] the authenticated user

Public Class Methods

new(base, env, path = nil) click to toggle source

Initialize a new Moonrope::Request

@param base [Moonrope::Base] @param env [Hash] a rack environment has @param path [String] the reqested path (after the /api/ prefix)

# 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

Private Class Methods

extract_http_request_headers(env) click to toggle source

Extract headers from the rack env

@return [Rack::Utils::HeaderHash]

# File lib/moonrope/request.rb, line 191
def self.extract_http_request_headers(env)
  env.reject do |k, v|
    !(/^HTTP_[A-Z_]+$/ === k) || v.nil?
  end.map do |k, v|
    [k.sub(/^HTTP_/, "").gsub("_", "-"), v]
  end.inject(::Rack::Utils::HeaderHash.new) do |hash, k_v|
    k, v = k_v
    hash[k] = v
    hash
  end
end

Public Instance Methods

action() click to toggle source

Return the action object for the request

return [Moonrope::Action]

# File lib/moonrope/request.rb, line 78
def action
  @action ||= controller.actions[action_name.to_sym]
end
anonymous?() click to toggle source

Is this request to the API anonymous?

@return [Boolean]

# File lib/moonrope/request.rb, line 145
def anonymous?
  identity.nil?
end
authenticated?() click to toggle source

Is this request to the API authenticated?

@return [Boolean]

# File lib/moonrope/request.rb, line 154
def authenticated?
  !(identity.nil? || identity == false)
end
controller() click to toggle source

Return the controller object for the request

@return [Moonrope::Controller]

# File lib/moonrope/request.rb, line 69
def controller
  @controller ||= @base.controller(controller_name.to_sym)
end
execute() click to toggle source

Execute the appropriate action for the request after running the various authentication checks.

@return [Moonrope::ActionResult]

# 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() click to toggle source

Return all HTTP headers from the request

@return [Rack::Utils::HeaderHash]

# File lib/moonrope/request.rb, line 136
def headers
  @headers ||= self.class.extract_http_request_headers(@env)
end
ip() click to toggle source

 Return the remote IP

@return [String]
# File lib/moonrope/request.rb, line 163
def ip
  rack_request.ip
end
params() click to toggle source

Return all user supplier parameters

@return [Moonrope::ParamSet]

# 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?() click to toggle source

Is this request on SSL?

@return [Boolean]

# File lib/moonrope/request.rb, line 171
def ssl?
  rack_request.ssl?
end
valid?() click to toggle source

Return whether or not this request is valid and can continue?

@return [Boolean]

# 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() click to toggle source

Return the requested API version from the request

@return [Integer]

# 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

Private Instance Methods

rack_request() click to toggle source

Return/create a rack request object for use internally

@return [Rack::Request]

# File lib/moonrope/request.rb, line 182
def rack_request
  @rack_request ||= ::Rack::Request.new(@env)
end