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)



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_regexRegex

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

Returns:

  • (Regex)

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

Returns the name of the action which was requested.

Returns:

  • (String)

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

Returns the authenticated user.

Returns:

  • (Object)

    the authenticated user



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

def authenticated_user
  @authenticated_user
end

#controller_nameString (readonly)

Returns the name of the controller which was requested.

Returns:

  • (String)

    the name of the controller which was requested



16
17
18
# File 'lib/moonrope/request.rb', line 16

def controller_name
  @controller_name
end

#envHash (readonly)

Returns the rack environment.

Returns:

  • (Hash)

    the rack environment



14
15
16
# File 'lib/moonrope/request.rb', line 14

def env
  @env
end

Instance Method Details

#actionObject

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?

Returns:

  • (Boolean)


134
135
136
# File 'lib/moonrope/request.rb', line 134

def anonymous?
  authenticated_user.nil?
end

#authenticated?Boolean

Is this request to the API authenticated?

Returns:

  • (Boolean)


143
144
145
# File 'lib/moonrope/request.rb', line 143

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

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

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

#headersRack::Utils::HeaderHash

Return all HTTP headers from the request

Returns:

  • (Rack::Utils::HeaderHash)


125
126
127
# File 'lib/moonrope/request.rb', line 125

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

#paramsMoonrope::ParamSet

Return all user supplier parameters

Returns:



110
111
112
113
114
115
116
117
118
# File 'lib/moonrope/request.rb', line 110

def params
  @params ||= begin
    if @env['CONTENT_TYPE'] == 'application/json'
      Moonrope::ParamSet.new(rack_request.body.read)
    else
      Moonrope::ParamSet.new(rack_request.params['params'])
    end
  end
end

#valid?Boolean

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

Returns:

  • (Boolean)


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

#versionInteger

Return the requested API version from the request

Returns:

  • (Integer)


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