Class: Rack::JSON::Request

Inherits:
Request
  • Object
show all
Includes:
Utils
Defined in:
lib/rackjson/request.rb

Instance Method Summary collapse

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

#collectionObject



10
11
12
# File 'lib/rackjson/request.rb', line 10

def collection
  self.path_info.split('/')[1] || ""
end

#collection_path?Boolean

Returns:

  • (Boolean)


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

def collection_path?
  self.path_info.match /^\/[\w-]+$/
end

#fieldObject



18
19
20
# File 'lib/rackjson/request.rb', line 18

def field
  path_info.split('/')[3] || ""
end

#field_path?Boolean

Returns:

  • (Boolean)


26
27
28
# File 'lib/rackjson/request.rb', line 26

def field_path?
  path_info.match(/^\/[\w-]+\/[\w-]+\/[\w-]+(\/[\w-]+)*$/) && !modifier_path?
end

#fieldsObject



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

#jsonObject



73
74
75
# File 'lib/rackjson/request.rb', line 73

def json
  raw_body
end

#member_path?Boolean

Returns:

  • (Boolean)


30
31
32
# File 'lib/rackjson/request.rb', line 30

def member_path?
  self.path_info.match /^\/[\w-]+\/[\w-]+$/
end

#modifierObject



34
35
36
# File 'lib/rackjson/request.rb', line 34

def modifier
  modifier_path? ? path_info.split('/').last : nil
end

#modifier_path?Boolean

Returns:

  • (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_typeObject



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

#payloadObject



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

#propertyObject



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

#queryObject



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_bodyObject



81
82
83
84
# File 'lib/rackjson/request.rb', line 81

def raw_body
  self.body.rewind
  self.body.read
end

#resource_idObject



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