Class: Rack::JSON::Request
- Inherits:
-
Request
- Object
- Request
- Rack::JSON::Request
- Includes:
- Utils
- Defined in:
- lib/rackjson/request.rb
Instance Attribute Summary collapse
-
#env ⇒ Object
readonly
Returns the value of attribute env.
Instance Method Summary collapse
- #add_query_param(param) ⇒ Object
- #collection ⇒ Object
- #collection_path? ⇒ Boolean
-
#initialize(env) ⇒ Request
constructor
A new instance of Request.
- #json ⇒ Object
- #member_path? ⇒ Boolean
- #query ⇒ Object
- #resource_id ⇒ Object
- #session ⇒ Object
- #set_body(json) ⇒ Object
Constructor Details
#initialize(env) ⇒ Request
Returns a new instance of Request.
7 8 9 10 |
# File 'lib/rackjson/request.rb', line 7 def initialize(env) @env = env super(env) end |
Instance Attribute Details
#env ⇒ Object (readonly)
Returns the value of attribute env.
5 6 7 |
# File 'lib/rackjson/request.rb', line 5 def env @env end |
Instance Method Details
#add_query_param(param) ⇒ Object
12 13 14 |
# File 'lib/rackjson/request.rb', line 12 def add_query_param(param) self.query_string << param end |
#collection ⇒ Object
16 17 18 |
# File 'lib/rackjson/request.rb', line 16 def collection self.path_info.split('/')[1] || "" end |
#collection_path? ⇒ Boolean
20 21 22 |
# File 'lib/rackjson/request.rb', line 20 def collection_path? self.path_info.match /^\/\w+$/ end |
#json ⇒ Object
28 29 30 31 |
# File 'lib/rackjson/request.rb', line 28 def json self.body.rewind self.body.read end |
#member_path? ⇒ Boolean
24 25 26 |
# File 'lib/rackjson/request.rb', line 24 def member_path? self.path_info.match /^\/\w+\/\w+$/ end |
#query ⇒ Object
33 34 35 |
# File 'lib/rackjson/request.rb', line 33 def query @query ||= Rack::JSON::JSONQuery.new(unescape(query_string)) end |
#resource_id ⇒ Object
37 38 39 40 41 42 43 44 |
# File 'lib/rackjson/request.rb', line 37 def resource_id id_string = self.path_info.split('/').last.to_s begin BSON::ObjectID.from_string(id_string) rescue BSON::InvalidObjectID id_string.match(/^\d+$/) ? id_string.to_i : id_string end end |
#session ⇒ Object
46 47 48 |
# File 'lib/rackjson/request.rb', line 46 def session @env['rack.session'] || {} end |
#set_body(json) ⇒ Object
50 51 52 53 |
# File 'lib/rackjson/request.rb', line 50 def set_body json @env['rack.input'] = StringIO.new(json) @env['rack.input'].rewind end |