rackjson 0.4.1 → 0.4.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README.markdown +3 -3
- data/Rakefile +1 -1
- data/VERSION +1 -1
- data/lib/rackjson.rb +1 -0
- data/lib/rackjson/collection.rb +0 -2
- data/lib/rackjson/document.rb +1 -3
- data/lib/rackjson/errors.rb +7 -0
- data/lib/rackjson/filter.rb +1 -0
- data/lib/rackjson/rack/builder.rb +2 -2
- data/lib/rackjson/request.rb +1 -14
- data/lib/rackjson/response.rb +2 -4
- data/rackjson.gemspec +7 -5
- data/test/test_embeded_resource.rb +37 -0
- data/test/test_filter.rb +27 -2
- data/test/test_response.rb +1 -1
- metadata +6 -4
data/README.markdown
CHANGED
@@ -18,7 +18,7 @@ In your rackup file:
|
|
18
18
|
expose_resource :collections => [:notes], :db => Mongo::Connection.new.db("mydb")
|
19
19
|
|
20
20
|
run lambda { |env|
|
21
|
-
[404, {'Content-
|
21
|
+
[404, {'Content-Type' => 'text/plain'}, ["Not Found"]]
|
22
22
|
}
|
23
23
|
|
24
24
|
This will set up a RESTful resource called 'notes' at /notes which will store any JSON document.
|
@@ -45,9 +45,9 @@ Using `expose_resource` won't restrict access to the resource at all, all reques
|
|
45
45
|
|
46
46
|
Using `public_resource` will allow everyone to perform GET requests against the resource, however to make POST, PUT or DELETE requests the requester must have a specific session param, and will only be able to make requests to documents which match this session param. In the above example this session param is set to user_id and could be set when logging in.
|
47
47
|
|
48
|
-
`
|
48
|
+
`private_resource :collections => [:notes], :filters => [:user_id], :db => Mongo::Connection.new.db("mydb")`
|
49
49
|
|
50
|
-
|
50
|
+
`private_resource` is very similar except that all requests, including GET requests must also satisfy the pre-condition of having a specific session param.
|
51
51
|
|
52
52
|
When creating resources with either the public or private resources the specified session param will be included in the document automatically.
|
53
53
|
|
data/Rakefile
CHANGED
@@ -12,7 +12,7 @@ begin
|
|
12
12
|
gem.authors = ["Oliver Nightingale"]
|
13
13
|
gem.add_dependency('mongo', '>=1.1.0')
|
14
14
|
gem.add_dependency('mongo_ext', '>=0.19.1')
|
15
|
-
gem.add_dependency('json', '
|
15
|
+
gem.add_dependency('json', '>=1.2.3')
|
16
16
|
gem.add_dependency('rack', '>=1.0.1')
|
17
17
|
# gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
|
18
18
|
end
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.4.
|
1
|
+
0.4.2
|
data/lib/rackjson.rb
CHANGED
data/lib/rackjson/collection.rb
CHANGED
data/lib/rackjson/document.rb
CHANGED
@@ -3,15 +3,13 @@ module Rack::JSON
|
|
3
3
|
|
4
4
|
attr_accessor :attributes
|
5
5
|
|
6
|
-
class BadDocumentFormatError < ArgumentError ; end
|
7
|
-
|
8
6
|
def self.create(doc)
|
9
7
|
if doc.is_a? String
|
10
8
|
Rack::JSON::JSONDocument.new(doc)
|
11
9
|
elsif doc.is_a? BSON::OrderedHash
|
12
10
|
Rack::JSON::MongoDocument.new(doc)
|
13
11
|
else
|
14
|
-
raise Rack::JSON::
|
12
|
+
raise Rack::JSON::DocumentFormatError
|
15
13
|
end
|
16
14
|
end
|
17
15
|
|
data/lib/rackjson/filter.rb
CHANGED
@@ -27,6 +27,7 @@ module Rack::JSON
|
|
27
27
|
end
|
28
28
|
|
29
29
|
def apply_filters(request)
|
30
|
+
raise Rack::JSON::NoFilterError if @filters.nil?
|
30
31
|
@filters.each do |filter|
|
31
32
|
return pre_condition_not_met unless request.session.keys.include? filter.to_s
|
32
33
|
request.add_query_param "[?#{filter}=#{request.session[filter.to_s]}]"
|
@@ -22,7 +22,7 @@ module Rack
|
|
22
22
|
@ins << lambda do |app|
|
23
23
|
Rack::JSON::Filter.new(
|
24
24
|
Rack::JSON::Resource.new(app, options),
|
25
|
-
|
25
|
+
options.merge(:methods => [:post, :put, :delete]))
|
26
26
|
end
|
27
27
|
end
|
28
28
|
|
@@ -37,7 +37,7 @@ module Rack
|
|
37
37
|
@ins << lambda do |app|
|
38
38
|
Rack::JSON::Filter.new(
|
39
39
|
Rack::JSON::Resource.new(app, options),
|
40
|
-
|
40
|
+
options.merge(:methods => [:get, :post, :put, :delete]))
|
41
41
|
end
|
42
42
|
end
|
43
43
|
end
|
data/lib/rackjson/request.rb
CHANGED
@@ -1,17 +1,8 @@
|
|
1
1
|
module Rack::JSON
|
2
2
|
class Request < Rack::Request
|
3
3
|
|
4
|
-
class Rack::JSON::Request::UnrecognisedPathTypeError < StandardError ; end
|
5
|
-
|
6
4
|
include Rack::Utils
|
7
5
|
|
8
|
-
attr_reader :env
|
9
|
-
|
10
|
-
def initialize(env)
|
11
|
-
@env = env
|
12
|
-
super(env)
|
13
|
-
end
|
14
|
-
|
15
6
|
def add_query_param(param)
|
16
7
|
self.query_string << param
|
17
8
|
end
|
@@ -56,7 +47,7 @@ module Rack::JSON
|
|
56
47
|
elsif collection_path?
|
57
48
|
:collection
|
58
49
|
else
|
59
|
-
raise UnrecognisedPathTypeError
|
50
|
+
raise Rack::JSON::UnrecognisedPathTypeError
|
60
51
|
end
|
61
52
|
end
|
62
53
|
|
@@ -103,10 +94,6 @@ module Rack::JSON
|
|
103
94
|
end
|
104
95
|
end
|
105
96
|
|
106
|
-
def session
|
107
|
-
@env['rack.session'] || {}
|
108
|
-
end
|
109
|
-
|
110
97
|
def set_body json
|
111
98
|
@env['rack.input'] = StringIO.new(json)
|
112
99
|
@env['rack.input'].rewind
|
data/lib/rackjson/response.rb
CHANGED
@@ -1,8 +1,6 @@
|
|
1
1
|
module Rack::JSON
|
2
2
|
class Response
|
3
3
|
|
4
|
-
class Rack::JSON::Response::BodyFormatError < ArgumentError ; end
|
5
|
-
|
6
4
|
attr_reader :status, :headers, :body
|
7
5
|
|
8
6
|
def initialize(body, options={})
|
@@ -10,8 +8,8 @@ module Rack::JSON
|
|
10
8
|
@head = options[:head] || false
|
11
9
|
@headers = options[:headers] || {}
|
12
10
|
parse_body(body)
|
13
|
-
set_headers
|
14
11
|
head_response if @head
|
12
|
+
set_headers
|
15
13
|
end
|
16
14
|
|
17
15
|
def to_a
|
@@ -32,7 +30,7 @@ module Rack::JSON
|
|
32
30
|
@body = body.to_s
|
33
31
|
@headers["Content-Type"] = "text/plain"
|
34
32
|
else
|
35
|
-
raise Rack::JSON::
|
33
|
+
raise Rack::JSON::BodyFormatError
|
36
34
|
end
|
37
35
|
end
|
38
36
|
|
data/rackjson.gemspec
CHANGED
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{rackjson}
|
8
|
-
s.version = "0.4.
|
8
|
+
s.version = "0.4.2"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Oliver Nightingale"]
|
12
|
-
s.date = %q{2010-10-
|
12
|
+
s.date = %q{2010-10-28}
|
13
13
|
s.description = %q{A rack end point for storing json documents.}
|
14
14
|
s.email = %q{oliver.n@new-bamboo.co.uk}
|
15
15
|
s.extra_rdoc_files = [
|
@@ -27,6 +27,7 @@ Gem::Specification.new do |s|
|
|
27
27
|
"lib/rackjson/collection.rb",
|
28
28
|
"lib/rackjson/document.rb",
|
29
29
|
"lib/rackjson/end_point.rb",
|
30
|
+
"lib/rackjson/errors.rb",
|
30
31
|
"lib/rackjson/extensions/BSON/object_id.rb",
|
31
32
|
"lib/rackjson/extensions/BSON/ordered_hash.rb",
|
32
33
|
"lib/rackjson/extensions/core/array.rb",
|
@@ -62,6 +63,7 @@ Gem::Specification.new do |s|
|
|
62
63
|
"test/suite.rb",
|
63
64
|
"test/test_collection.rb",
|
64
65
|
"test/test_document.rb",
|
66
|
+
"test/test_embeded_resource.rb",
|
65
67
|
"test/test_filter.rb",
|
66
68
|
"test/test_json_document.rb",
|
67
69
|
"test/test_json_query.rb",
|
@@ -78,18 +80,18 @@ Gem::Specification.new do |s|
|
|
78
80
|
if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
|
79
81
|
s.add_runtime_dependency(%q<mongo>, [">= 1.1.0"])
|
80
82
|
s.add_runtime_dependency(%q<mongo_ext>, [">= 0.19.1"])
|
81
|
-
s.add_runtime_dependency(%q<json>, ["
|
83
|
+
s.add_runtime_dependency(%q<json>, [">= 1.2.3"])
|
82
84
|
s.add_runtime_dependency(%q<rack>, [">= 1.0.1"])
|
83
85
|
else
|
84
86
|
s.add_dependency(%q<mongo>, [">= 1.1.0"])
|
85
87
|
s.add_dependency(%q<mongo_ext>, [">= 0.19.1"])
|
86
|
-
s.add_dependency(%q<json>, ["
|
88
|
+
s.add_dependency(%q<json>, [">= 1.2.3"])
|
87
89
|
s.add_dependency(%q<rack>, [">= 1.0.1"])
|
88
90
|
end
|
89
91
|
else
|
90
92
|
s.add_dependency(%q<mongo>, [">= 1.1.0"])
|
91
93
|
s.add_dependency(%q<mongo_ext>, [">= 0.19.1"])
|
92
|
-
s.add_dependency(%q<json>, ["
|
94
|
+
s.add_dependency(%q<json>, [">= 1.2.3"])
|
93
95
|
s.add_dependency(%q<rack>, [">= 1.0.1"])
|
94
96
|
end
|
95
97
|
end
|
@@ -0,0 +1,37 @@
|
|
1
|
+
# require 'helper'
|
2
|
+
#
|
3
|
+
# class EmbeddedResourceTest < Test::Unit::TestCase
|
4
|
+
# include Rack::Test::Methods
|
5
|
+
# include Rack::Utils
|
6
|
+
# def setup
|
7
|
+
# @db = Mongo::Connection.new.db("test")
|
8
|
+
# @collection = @db['testing']
|
9
|
+
# @doc = { :title => 'blah' }
|
10
|
+
# @collection.insert(@doc)
|
11
|
+
# end
|
12
|
+
#
|
13
|
+
# def teardown
|
14
|
+
# @collection.drop
|
15
|
+
# end
|
16
|
+
#
|
17
|
+
# def app
|
18
|
+
# Rack::JSON::EmbeddedResource.new lambda { |env|
|
19
|
+
# [404, {'Content-Length' => '9', 'Content-Type' => 'text/plain'}, ["Not Found"]]
|
20
|
+
# }, :collections => [:foo], :contains => [:bar], :db => @db
|
21
|
+
# end
|
22
|
+
#
|
23
|
+
# test "ignoring non embedded resource requests" do
|
24
|
+
# get '/foo/1'
|
25
|
+
# assert_equal 404, last_response.status
|
26
|
+
#
|
27
|
+
# get '/foo/1/not_bar'
|
28
|
+
# assert_equal 404, last_response.status
|
29
|
+
# end
|
30
|
+
#
|
31
|
+
# test "return all embedded objects on index path" do
|
32
|
+
# get '/foo/1/bar'
|
33
|
+
#
|
34
|
+
# assert_equal JSON.generate([@doc]), last_response.body
|
35
|
+
# end
|
36
|
+
#
|
37
|
+
# end
|
data/test/test_filter.rb
CHANGED
@@ -15,6 +15,21 @@ class FilterTest < Test::Unit::TestCase
|
|
15
15
|
)
|
16
16
|
end
|
17
17
|
|
18
|
+
def app_without_filter
|
19
|
+
Rack::Session::Cookie.new(
|
20
|
+
Rack::JSON::Filter.new lambda { |env|
|
21
|
+
request = Rack::JSON::Request.new(env)
|
22
|
+
env['rack.session'] = {}
|
23
|
+
env['rack.session']['user_id'] = 1
|
24
|
+
[200, {'Content-Length' => request.json.length.to_s, 'Content-Type' => 'text/plain'}, [request.json]]
|
25
|
+
}, :collections => [:testing], :methods => @test_methods || [:get, :post, :put, :delete]
|
26
|
+
)
|
27
|
+
end
|
28
|
+
|
29
|
+
def use_app_without_filter
|
30
|
+
FilterTest.class_eval { def app; app_without_filter; end }
|
31
|
+
end
|
32
|
+
|
18
33
|
test "adding a user id query parameter" do
|
19
34
|
get '/login'
|
20
35
|
get '/testing'
|
@@ -23,8 +38,10 @@ class FilterTest < Test::Unit::TestCase
|
|
23
38
|
|
24
39
|
test "dont override any existing query parameters" do
|
25
40
|
get '/login'
|
26
|
-
get '/testing?
|
27
|
-
|
41
|
+
get '/testing?title=awesome]'
|
42
|
+
# this is using the incorrect jsonquery structure but rack-test seems to be dropping
|
43
|
+
# the first square bracket under ruby 1.8.7
|
44
|
+
assert_equal 'title=awesome][?user_id=1]', URI.decode(last_request.query_string)
|
28
45
|
end
|
29
46
|
|
30
47
|
test "reject request if no session var" do
|
@@ -75,4 +92,12 @@ class FilterTest < Test::Unit::TestCase
|
|
75
92
|
post '/testing', 'invalid json'
|
76
93
|
assert_equal 422, last_response.status
|
77
94
|
end
|
95
|
+
|
96
|
+
test "should raise an error if no filters are passed" do
|
97
|
+
use_app_without_filter
|
98
|
+
|
99
|
+
assert_raise Rack::JSON::NoFilterError do
|
100
|
+
get '/testing'
|
101
|
+
end
|
102
|
+
end
|
78
103
|
end
|
data/test/test_response.rb
CHANGED
@@ -46,7 +46,7 @@ class ResponseTest < Test::Unit::TestCase
|
|
46
46
|
def test_head_response
|
47
47
|
response = Rack::JSON::Response.new("test", :head => true)
|
48
48
|
assert_equal([""], response.to_a[2])
|
49
|
-
assert_equal("
|
49
|
+
assert_equal("0", response.to_a[1]["Content-Length"])
|
50
50
|
assert_equal("text/plain", response.headers["Content-Type"])
|
51
51
|
end
|
52
52
|
end
|
metadata
CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
|
|
5
5
|
segments:
|
6
6
|
- 0
|
7
7
|
- 4
|
8
|
-
-
|
9
|
-
version: 0.4.
|
8
|
+
- 2
|
9
|
+
version: 0.4.2
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- Oliver Nightingale
|
@@ -14,7 +14,7 @@ autorequire:
|
|
14
14
|
bindir: bin
|
15
15
|
cert_chain: []
|
16
16
|
|
17
|
-
date: 2010-10-
|
17
|
+
date: 2010-10-28 00:00:00 +01:00
|
18
18
|
default_executable:
|
19
19
|
dependencies:
|
20
20
|
- !ruby/object:Gem::Dependency
|
@@ -50,7 +50,7 @@ dependencies:
|
|
50
50
|
prerelease: false
|
51
51
|
requirement: &id003 !ruby/object:Gem::Requirement
|
52
52
|
requirements:
|
53
|
-
- - "
|
53
|
+
- - ">="
|
54
54
|
- !ruby/object:Gem::Version
|
55
55
|
segments:
|
56
56
|
- 1
|
@@ -93,6 +93,7 @@ files:
|
|
93
93
|
- lib/rackjson/collection.rb
|
94
94
|
- lib/rackjson/document.rb
|
95
95
|
- lib/rackjson/end_point.rb
|
96
|
+
- lib/rackjson/errors.rb
|
96
97
|
- lib/rackjson/extensions/BSON/object_id.rb
|
97
98
|
- lib/rackjson/extensions/BSON/ordered_hash.rb
|
98
99
|
- lib/rackjson/extensions/core/array.rb
|
@@ -152,6 +153,7 @@ test_files:
|
|
152
153
|
- test/suite.rb
|
153
154
|
- test/test_collection.rb
|
154
155
|
- test/test_document.rb
|
156
|
+
- test/test_embeded_resource.rb
|
155
157
|
- test/test_filter.rb
|
156
158
|
- test/test_json_document.rb
|
157
159
|
- test/test_json_query.rb
|