Class: Rack::JSON::Request
- Inherits:
-
Request
- Object
- Request
- Rack::JSON::Request
- Includes:
- Utils
- Defined in:
- lib/rackjson/request.rb
Instance Method Summary collapse
- #add_query_param(param) ⇒ Object
- #collection ⇒ Object
- #collection_path? ⇒ Boolean
- #field ⇒ Object
- #field_path? ⇒ Boolean
- #fields ⇒ Object
- #json ⇒ Object
- #member_path? ⇒ Boolean
- #modifier ⇒ Object
- #modifier_path? ⇒ Boolean
- #path_type ⇒ Object
- #payload ⇒ Object
- #property ⇒ Object
- #query ⇒ Object
- #raw_body ⇒ Object
- #resource_id ⇒ Object
- #set_body(json) ⇒ Object
Instance Method Details
#add_query_param(param) ⇒ Object
6 7 8 |
# File 'lib/rackjson/request.rb', line 6 def add_query_param(param) self.query_string << param end |
#collection ⇒ Object
10 11 12 |
# File 'lib/rackjson/request.rb', line 10 def collection self.path_info.split('/')[1] || "" end |
#collection_path? ⇒ Boolean
14 15 16 |
# File 'lib/rackjson/request.rb', line 14 def collection_path? self.path_info.match /^\/[\w-]+$/ end |
#field ⇒ Object
18 19 20 |
# File 'lib/rackjson/request.rb', line 18 def field path_info.split('/')[3] || "" end |
#field_path? ⇒ Boolean
26 27 28 |
# File 'lib/rackjson/request.rb', line 26 def field_path? path_info.match(/^\/[\w-]+\/[\w-]+\/[\w-]+(\/[\w-]+)*$/) && !modifier_path? end |
#fields ⇒ Object
22 23 24 |
# File 'lib/rackjson/request.rb', line 22 def fields path_info.split('/').slice(3..-1).reject { |f| f.match(/(_increment|_decrement|_push|_pull|_push_all|_pull_all|_add_to_set)/)} || [] end |
#json ⇒ Object
73 74 75 |
# File 'lib/rackjson/request.rb', line 73 def json raw_body end |
#member_path? ⇒ Boolean
30 31 32 |
# File 'lib/rackjson/request.rb', line 30 def member_path? self.path_info.match /^\/[\w-]+\/[\w-]+$/ end |
#modifier ⇒ Object
34 35 36 |
# File 'lib/rackjson/request.rb', line 34 def modifier modifier_path? ? path_info.split('/').last : nil end |
#modifier_path? ⇒ Boolean
38 39 40 |
# File 'lib/rackjson/request.rb', line 38 def modifier_path? path_info.match /^\/[\w-]+\/[\w-]+\/[\w-]+(\/[\w-]+)*\/(_increment|_decrement|_push|_pull|_push_all|_pull_all|_add_to_set)$/ end |
#path_type ⇒ Object
42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/rackjson/request.rb', line 42 def path_type if member_path? :member elsif field_path? :field elsif collection_path? :collection else raise Rack::JSON::UnrecognisedPathTypeError end end |
#payload ⇒ Object
54 55 56 57 58 59 60 61 62 |
# File 'lib/rackjson/request.rb', line 54 def payload if content_type == 'application/json' JSON.parse(raw_body) elsif raw_body.empty? nil else raw_body.numeric? ? raw_body.to_number : raw_body end end |
#property ⇒ Object
64 65 66 67 68 69 70 71 |
# File 'lib/rackjson/request.rb', line 64 def property property = path_info.split('/')[4] if property property.match(/^\d+$/)? property.to_i : property else nil end end |
#query ⇒ Object
77 78 79 |
# File 'lib/rackjson/request.rb', line 77 def query @query ||= Rack::JSON::JSONQuery.new(unescape(query_string), :resource_id => resource_id) end |
#raw_body ⇒ Object
81 82 83 84 |
# File 'lib/rackjson/request.rb', line 81 def raw_body self.body.rewind self.body.read end |
#resource_id ⇒ Object
86 87 88 89 90 91 92 93 94 95 |
# File 'lib/rackjson/request.rb', line 86 def resource_id unless collection_path? id_string = self.path_info.split('/')[2].to_s begin BSON::ObjectId.from_string(id_string) rescue BSON::InvalidObjectId id_string.match(/^\d+$/) ? id_string.to_i : id_string end end end |
#set_body(json) ⇒ Object
97 98 99 100 |
# File 'lib/rackjson/request.rb', line 97 def set_body json @env['rack.input'] = StringIO.new(json) @env['rack.input'].rewind end |