Class CloudKit::Request

  1. lib/cloudkit/request.rb
Parent: Rack::Request

A subclass of Rack::Request providing CloudKit-specific features.

Included modules

  1. CloudKit::Util

Public class methods

new (env)
[show source]
    # File lib/cloudkit/request.rb, line 8
 8:     def initialize(env)
 9:       super(env)
10:     end

Public instance methods

announce_auth (via)

Report to downstream middleware that authentication is in use.

[show source]
     # File lib/cloudkit/request.rb, line 135
135:     def announce_auth(via)
136:       inject_via(via)
137:       @env[CLOUDKIT_AUTH_PRESENCE] = 1
138:     end
cloudkit_params ()

Alias for params

current_user ()

Return the current user URI.

[show source]
     # File lib/cloudkit/request.rb, line 119
119:     def current_user
120:       return nil unless @env[CLOUDKIT_AUTH_KEY] && @env[CLOUDKIT_AUTH_KEY] != ''
121:       @env[CLOUDKIT_AUTH_KEY]
122:     end
current_user= (user)

Set the current user URI.

[show source]
     # File lib/cloudkit/request.rb, line 125
125:     def current_user=(user)
126:       @env[CLOUDKIT_AUTH_KEY] = user
127:     end
domain_root ()

Return the host and scheme

[show source]
     # File lib/cloudkit/request.rb, line 173
173:     def domain_root
174:       "#{scheme}://#{@env['HTTP_HOST']}"
175:     end
flash ()

Return the flash session for this request.

[show source]
     # File lib/cloudkit/request.rb, line 168
168:     def flash
169:       session[CLOUDKIT_FLASH] ||= CloudKit::FlashSession.new
170:     end
if_match ()

Return parsed contents of an If-Match header.

Note: Only a single ETag is useful in the context of CloudKit, so a list is treated as one ETag; the result of using the wrong ETag or a list of ETags is the same in the context of PUT and DELETE where If-Match headers are required.

[show source]
     # File lib/cloudkit/request.rb, line 103
103:     def if_match
104:       etag = @env['HTTP_IF_MATCH']
105:       return nil unless etag
106:       etag.strip!
107:       etag = unquote(etag)
108:       return nil if etag == '*'
109:       etag
110:     end
inject_via (key)

Add a via entry to the Rack environment.

[show source]
     # File lib/cloudkit/request.rb, line 113
113:     def inject_via(key)
114:       items = via << key
115:       @env[CLOUDKIT_VIA] = items.join(', ')
116:     end
json ()

Return the JSON content from the request body

[show source]
    # File lib/cloudkit/request.rb, line 18
18:     def json
19:       self.body.rewind
20:       raw = self.body.read
21:       # extract the json from the body to avoid tunneled _method param from being parsed as json
22:       (matches = raw.match(/(\{.*\})/)) ? matches[1] : raw
23:     end
last_path_element ()

Return the last path element in the request URI.

[show source]
    # File lib/cloudkit/request.rb, line 80
80:     def last_path_element
81:       path_element(-1)
82:     end
login_url ()

Return the login URL for this request. This is stashed in the Rack environment so the OpenID and OAuth middleware can cooperate during the token authorization step in the OAuth flow.

[show source]
     # File lib/cloudkit/request.rb, line 148
148:     def login_url
149:       @env[CLOUDKIT_LOGIN_URL] || '/login'
150:     end
login_url= (url)

Set the login url for this request.

[show source]
     # File lib/cloudkit/request.rb, line 153
153:     def login_url=(url)
154:       @env[CLOUDKIT_LOGIN_URL] = url
155:     end
logout_url ()

Return the logout URL for this request.

[show source]
     # File lib/cloudkit/request.rb, line 158
158:     def logout_url
159:       @env[CLOUDKIT_LOGOUT_URL] || '/logout'
160:     end
logout_url= (url)

Set the logout URL for this request.

[show source]
     # File lib/cloudkit/request.rb, line 163
163:     def logout_url=(url)
164:       @env[CLOUDKIT_LOGOUT_URL] = url
165:     end
match? (method, path, required_params=[])

Return true if method, path, and required_params match.

[show source]
    # File lib/cloudkit/request.rb, line 31
31:     def match?(method, path, required_params=[])
32:       (request_method == method) &&
33:         path_info.match(path.gsub(':id', '*')) && # just enough to work for now
34:         param_match?(required_params)
35:     end
oauth_header_params ()

Return OAuth header params in a hash.

[show source]
    # File lib/cloudkit/request.rb, line 56
56:     def oauth_header_params
57:       # This is a copy of the same method from the OAuth gem.
58:       # TODO: Refactor the OAuth gem so that this method is available via a
59:       # mixin, outside of the request proxy context.
60:       %w( X-HTTP_AUTHORIZATION Authorization HTTP_AUTHORIZATION ).each do |header|
61:         next unless @env.include?(header)
62:         header = @env[header]
63:         next unless header[0,6] == 'OAuth '
64:         oauth_param_string = header[6,header.length].split(/[,=]/)
65:         oauth_param_string.map!{|v| unescape(v.strip)}
66:         oauth_param_string.map!{|v| v =~ /^\".*\"$/ ? v[1..-2] : v}
67:         oauth_params = Hash[*oauth_param_string.flatten]
68:         oauth_params.reject!{|k,v| k !~ /^oauth_/}
69:         return oauth_params
70:       end
71:       return {}
72:     end
param_match? (required_params)

Return true of the array of required params match the request params. If a hash in passed in for a param, its value is also used in the match.

[show source]
    # File lib/cloudkit/request.rb, line 39
39:     def param_match?(required_params)
40:       required_params.all? do |required_param|
41:         case required_param
42:         when Hash
43:           key = required_param.keys.first
44:           return false unless params.has_key? key
45:           return false unless params[key] == required_param[key]
46:         when String
47:           return false unless params.has_key? required_param
48:         else
49:           false
50:         end
51:         true
52:       end
53:     end
params ()

Return a merged set of both standard params and OAuth header params.

[show source]
    # File lib/cloudkit/request.rb, line 13
13:     def params
14:       @cloudkit_params ||= cloudkit_params.merge(oauth_header_params)
15:     end
path_element (index)

Return a specific path element

[show source]
    # File lib/cloudkit/request.rb, line 85
85:     def path_element(index)
86:       path_info.split('/')[index] rescue nil
87:     end
session ()

Return the session associated with this request.

[show source]
     # File lib/cloudkit/request.rb, line 141
141:     def session
142:       @env['rack.session']
143:     end
unescape (value)

Unescape a value according to the OAuth spec.

[show source]
    # File lib/cloudkit/request.rb, line 75
75:     def unescape(value)
76:       ::URI.unescape(value.gsub('+', '%2B'))
77:     end
uri ()

Return a CloudKit::URI instance representing the rack request’s path info.

[show source]
    # File lib/cloudkit/request.rb, line 26
26:     def uri
27:       @uri ||= CloudKit::URI.new(self.path_info)
28:     end
using_auth? ()

Return true if authentication is being used.

[show source]
     # File lib/cloudkit/request.rb, line 130
130:     def using_auth?
131:       @env[CLOUDKIT_AUTH_PRESENCE] != nil
132:     end
via ()

Return an array containing one entry for each piece of upstream middleware. This is in the same spirit as Via headers in HTTP, but does not use the header because the transition from one piece of middleware to the next does not use HTTP.

[show source]
    # File lib/cloudkit/request.rb, line 93
93:     def via
94:       @env[CLOUDKIT_VIA].split(', ') rescue []
95:     end