rackjson 0.1.1 → 0.2.0
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/VERSION +1 -1
- data/lib/rackjson/collection.rb +58 -0
- data/lib/rackjson/document.rb +6 -3
- data/lib/rackjson/filter.rb +44 -0
- data/lib/rackjson/request.rb +17 -0
- data/lib/rackjson/resource.rb +26 -14
- data/lib/rackjson/response.rb +1 -1
- data/lib/rackjson.rb +19 -35
- data/rackjson.gemspec +9 -4
- data/test/helper.rb +1 -0
- data/test/test_collection.rb +109 -0
- data/test/test_document.rb +7 -0
- data/test/test_filter.rb +58 -0
- data/test/test_resource.rb +29 -3
- data/test/test_response.rb +3 -3
- metadata +27 -45
- data/test/test_builder.rb +0 -17
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.
|
1
|
+
0.2.0
|
@@ -0,0 +1,58 @@
|
|
1
|
+
module Rack::JSON
|
2
|
+
class Collection
|
3
|
+
def initialize(collection)
|
4
|
+
@collection = collection
|
5
|
+
end
|
6
|
+
|
7
|
+
def all(options={})
|
8
|
+
@collection.find({}, options).to_a
|
9
|
+
end
|
10
|
+
|
11
|
+
def create(document)
|
12
|
+
@collection.save(document)
|
13
|
+
end
|
14
|
+
|
15
|
+
def delete(selector={})
|
16
|
+
@collection.remove(prepared(selector))
|
17
|
+
end
|
18
|
+
|
19
|
+
def delete_all
|
20
|
+
@collection.remove
|
21
|
+
end
|
22
|
+
|
23
|
+
def exists?(selector)
|
24
|
+
!@collection.find(prepared(selector)).to_a.empty?
|
25
|
+
end
|
26
|
+
|
27
|
+
def find(selector, options={})
|
28
|
+
if selector.is_a? Hash
|
29
|
+
@collection.find(selector, options).inject([]) {|documents, row| documents << Rack::JSON::Document.new(row)}
|
30
|
+
else
|
31
|
+
Rack::JSON::Document.new(@collection.find_one(:_id => selector))
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
def save(document)
|
36
|
+
@collection.save(document)
|
37
|
+
end
|
38
|
+
|
39
|
+
def update(selector, document, query={})
|
40
|
+
if exists?(prepared(selector).merge(query))
|
41
|
+
@collection.update(prepared(selector).merge(query), document, :upsert => false)
|
42
|
+
else
|
43
|
+
false
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
private
|
48
|
+
|
49
|
+
def prepared selector
|
50
|
+
if selector.is_a? Hash
|
51
|
+
selector
|
52
|
+
else
|
53
|
+
{:_id => selector}
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
end
|
58
|
+
end
|
data/lib/rackjson/document.rb
CHANGED
@@ -9,21 +9,24 @@ module Rack::JSON
|
|
9
9
|
end
|
10
10
|
end
|
11
11
|
|
12
|
+
def add_attributes(pair)
|
13
|
+
@document.attributes.merge!(pair)
|
14
|
+
end
|
15
|
+
|
12
16
|
def method_missing(name, *args)
|
13
17
|
@document.send(name, *args)
|
14
18
|
end
|
15
19
|
|
16
|
-
def to_json
|
20
|
+
def to_json(*a)
|
17
21
|
unless @json
|
18
22
|
gen_attrs = @document.attributes
|
19
23
|
gen_attrs.each_pair do |key, value|
|
20
|
-
|
21
24
|
if value.is_a? BSON::ObjectID
|
22
25
|
gen_attrs[key] = gen_attrs[key].to_s
|
23
26
|
end
|
24
27
|
end
|
25
28
|
end
|
26
|
-
|
29
|
+
gen_attrs.to_json
|
27
30
|
end
|
28
31
|
end
|
29
32
|
end
|
@@ -0,0 +1,44 @@
|
|
1
|
+
module Rack::JSON
|
2
|
+
class Filter
|
3
|
+
|
4
|
+
def initialize(app, options)
|
5
|
+
@app = app
|
6
|
+
@collections = options[:collections]
|
7
|
+
@filters = options[:filters]
|
8
|
+
end
|
9
|
+
|
10
|
+
def call(env)
|
11
|
+
request = Rack::JSON::Request.new(env)
|
12
|
+
if bypass? request
|
13
|
+
@app.call(env)
|
14
|
+
else
|
15
|
+
apply_filters(request)
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
private
|
20
|
+
|
21
|
+
def append_filters_to_document_in request
|
22
|
+
document = Rack::JSON::Document.new(request.json)
|
23
|
+
document.add_attributes(request.session.reject { |key, value| !@filters.include?(key.to_sym) })
|
24
|
+
request.set_body(document.to_json)
|
25
|
+
end
|
26
|
+
|
27
|
+
def apply_filters(request)
|
28
|
+
@filters.each do |filter|
|
29
|
+
return pre_condition_not_met unless request.session[filter.to_s]
|
30
|
+
request.add_query_param "[?#{filter}=#{request.session[filter.to_s]}]"
|
31
|
+
end
|
32
|
+
append_filters_to_document_in request if request.post? || request.put?
|
33
|
+
@app.call(request.env)
|
34
|
+
end
|
35
|
+
|
36
|
+
def bypass?(request)
|
37
|
+
request.collection.empty? || !(@collections.include? request.collection.to_sym)
|
38
|
+
end
|
39
|
+
|
40
|
+
def pre_condition_not_met
|
41
|
+
Rack::JSON::Response.new("pre condition not met", :status => 412, :head => true).to_a
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
data/lib/rackjson/request.rb
CHANGED
@@ -1,10 +1,18 @@
|
|
1
1
|
module Rack::JSON
|
2
2
|
class Request < Rack::Request
|
3
3
|
include Rack::Utils
|
4
|
+
|
5
|
+
attr_reader :env
|
6
|
+
|
4
7
|
def initialize(env)
|
8
|
+
@env = env
|
5
9
|
super(env)
|
6
10
|
end
|
7
11
|
|
12
|
+
def add_query_param(param)
|
13
|
+
self.query_string << param
|
14
|
+
end
|
15
|
+
|
8
16
|
def collection
|
9
17
|
self.path_info.split('/')[1] || ""
|
10
18
|
end
|
@@ -34,5 +42,14 @@ module Rack::JSON
|
|
34
42
|
id_string.match(/^\d+$/) ? id_string.to_i : id_string
|
35
43
|
end
|
36
44
|
end
|
45
|
+
|
46
|
+
def session
|
47
|
+
@env['rack.session']
|
48
|
+
end
|
49
|
+
|
50
|
+
def set_body json
|
51
|
+
@env['rack.input'] = StringIO.new(json)
|
52
|
+
@env['rack.input'].rewind
|
53
|
+
end
|
37
54
|
end
|
38
55
|
end
|
data/lib/rackjson/resource.rb
CHANGED
@@ -13,7 +13,7 @@ module Rack::JSON
|
|
13
13
|
if bypass? request
|
14
14
|
@app.call(env)
|
15
15
|
else
|
16
|
-
@collection = @db[request.collection]
|
16
|
+
@collection = Rack::JSON::Collection.new(@db[request.collection])
|
17
17
|
send(request.request_method.downcase, request)
|
18
18
|
end
|
19
19
|
end
|
@@ -21,12 +21,12 @@ module Rack::JSON
|
|
21
21
|
private
|
22
22
|
|
23
23
|
def bypass?(request)
|
24
|
-
!(@collections.include? request.collection.to_sym)
|
24
|
+
request.collection.empty? || !(@collections.include? request.collection.to_sym)
|
25
25
|
end
|
26
26
|
|
27
27
|
def delete(request)
|
28
28
|
if request.member_path?
|
29
|
-
if @collection.
|
29
|
+
if @collection.delete({:_id => request.resource_id})
|
30
30
|
render "{'ok': true}"
|
31
31
|
end
|
32
32
|
else
|
@@ -37,12 +37,11 @@ module Rack::JSON
|
|
37
37
|
[:get, :head].each do |method|
|
38
38
|
define_method method do |request|
|
39
39
|
request.query.selector.merge!({:_id => request.resource_id}) if request.member_path?
|
40
|
-
|
41
|
-
|
42
|
-
if rows.empty? && request.member_path?
|
40
|
+
documents = @collection.find(request.query.selector, request.query.options)
|
41
|
+
if documents.empty? && request.member_path?
|
43
42
|
render "document not found", :status => 404, :head => (method == :head)
|
44
43
|
else
|
45
|
-
render JSON.generate(
|
44
|
+
render JSON.generate(documents), :head => (method == :head)
|
46
45
|
end
|
47
46
|
end
|
48
47
|
end
|
@@ -58,18 +57,14 @@ module Rack::JSON
|
|
58
57
|
|
59
58
|
def post(request)
|
60
59
|
document = Rack::JSON::Document.new(request.json)
|
61
|
-
@collection.
|
60
|
+
@collection.create(document.attributes)
|
62
61
|
render document.to_json, :status => 201
|
63
62
|
rescue JSON::ParserError => error
|
64
63
|
render (error.class.to_s + " :" + error.message), :status => 422
|
65
64
|
end
|
66
65
|
|
67
66
|
def put(request)
|
68
|
-
@collection.
|
69
|
-
document = Rack::JSON::Document.new(request.json)
|
70
|
-
document.add_id(request.resource_id)
|
71
|
-
@collection.save(document.attributes)
|
72
|
-
render document.to_json, :status => status
|
67
|
+
@collection.exists?(request.resource_id) ? update(request) : upsert(request)
|
73
68
|
rescue JSON::ParserError => error
|
74
69
|
render (error.class.to_s + " :" + error.message), :status => 422
|
75
70
|
end
|
@@ -78,8 +73,25 @@ module Rack::JSON
|
|
78
73
|
Rack::JSON::Response.new(body, options).to_a
|
79
74
|
end
|
80
75
|
|
76
|
+
def update(request)
|
77
|
+
document = Rack::JSON::Document.new(request.json)
|
78
|
+
document.add_id(request.resource_id)
|
79
|
+
if @collection.update(request.resource_id, document.attributes, request.query.selector)
|
80
|
+
render document.to_json, :status => 200
|
81
|
+
else
|
82
|
+
render "document not found", :status => 404
|
83
|
+
end
|
84
|
+
end
|
85
|
+
|
86
|
+
def upsert(request)
|
87
|
+
document = Rack::JSON::Document.new(request.json)
|
88
|
+
document.add_id(request.resource_id)
|
89
|
+
@collection.save(document.attributes)
|
90
|
+
render document.to_json, :status => 201
|
91
|
+
end
|
92
|
+
|
81
93
|
METHODS_NOT_ALLOWED.each do |method|
|
82
|
-
define_method method do
|
94
|
+
define_method method do |request|
|
83
95
|
render "", :status => 405
|
84
96
|
end
|
85
97
|
end
|
data/lib/rackjson/response.rb
CHANGED
data/lib/rackjson.rb
CHANGED
@@ -1,40 +1,22 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
# ===Example
|
7
|
-
# contain :notes, :projects
|
8
|
-
#
|
9
|
-
# def contain(*args)
|
10
|
-
# @ins << lambda do |app|
|
11
|
-
# Rack::Session::Pool.new(
|
12
|
-
# CloudKit::OAuthFilter.new(
|
13
|
-
# CloudKit::OpenIDFilter.new(
|
14
|
-
# CloudKit::Service.new(app, :collections => args.to_a))))
|
15
|
-
# end
|
16
|
-
# @last_cloudkit_id = @ins.last.object_id
|
17
|
-
# end
|
18
|
-
|
19
|
-
def private_resource
|
20
|
-
@ins << lambda do |app|
|
21
|
-
Rack::JSON::UserFilter.new(
|
22
|
-
Rack::JSON::Resource.new(app, args.first),
|
23
|
-
args.first
|
24
|
-
)
|
25
|
-
end
|
1
|
+
class Hash
|
2
|
+
def symbolize_keys
|
3
|
+
inject({}) do |options, (key, value)|
|
4
|
+
options[(key.to_sym rescue key) || key] = value
|
5
|
+
options
|
26
6
|
end
|
7
|
+
end
|
27
8
|
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
9
|
+
def symbolize_keys!
|
10
|
+
self.replace(self.symbolize_keys)
|
11
|
+
end
|
12
|
+
|
13
|
+
def recursive_symbolize_keys!
|
14
|
+
symbolize_keys!
|
15
|
+
# symbolize each hash in .values
|
16
|
+
values.each{|h| h.recursive_symbolize_keys! if h.is_a?(Hash) }
|
17
|
+
# symbolize each hash inside an array in .values
|
18
|
+
values.select{|v| v.is_a?(Array) }.flatten.each{|h| h.recursive_symbolize_keys! if h.is_a?(Hash) }
|
19
|
+
self
|
38
20
|
end
|
39
21
|
end
|
40
22
|
|
@@ -46,6 +28,8 @@ require 'time'
|
|
46
28
|
|
47
29
|
module Rack::JSON
|
48
30
|
|
31
|
+
autoload :Collection, 'rackjson/collection'
|
32
|
+
autoload :Filter, 'rackjson/filter'
|
49
33
|
autoload :Document, 'rackjson/document'
|
50
34
|
autoload :JSONDocument, 'rackjson/json_document'
|
51
35
|
autoload :JSONQuery, 'rackjson/json_query'
|
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.
|
8
|
+
s.version = "0.2.0"
|
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-04-
|
12
|
+
s.date = %q{2010-04-26}
|
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 = [
|
@@ -24,7 +24,9 @@ Gem::Specification.new do |s|
|
|
24
24
|
"Rakefile",
|
25
25
|
"VERSION",
|
26
26
|
"lib/rackjson.rb",
|
27
|
+
"lib/rackjson/collection.rb",
|
27
28
|
"lib/rackjson/document.rb",
|
29
|
+
"lib/rackjson/filter.rb",
|
28
30
|
"lib/rackjson/json_document.rb",
|
29
31
|
"lib/rackjson/json_query.rb",
|
30
32
|
"lib/rackjson/mongo_document.rb",
|
@@ -34,7 +36,9 @@ Gem::Specification.new do |s|
|
|
34
36
|
"rackjson.gemspec",
|
35
37
|
"test/helper.rb",
|
36
38
|
"test/suite.rb",
|
39
|
+
"test/test_collection.rb",
|
37
40
|
"test/test_document.rb",
|
41
|
+
"test/test_filter.rb",
|
38
42
|
"test/test_json_document.rb",
|
39
43
|
"test/test_json_query.rb",
|
40
44
|
"test/test_mongo_document.rb",
|
@@ -44,13 +48,14 @@ Gem::Specification.new do |s|
|
|
44
48
|
s.homepage = %q{http://github.com/olivernn/rackjson}
|
45
49
|
s.rdoc_options = ["--charset=UTF-8"]
|
46
50
|
s.require_paths = ["lib"]
|
47
|
-
s.rubygems_version = %q{1.3.
|
51
|
+
s.rubygems_version = %q{1.3.5}
|
48
52
|
s.summary = %q{A rack end point for storing json documents.}
|
49
53
|
s.test_files = [
|
50
54
|
"test/helper.rb",
|
51
55
|
"test/suite.rb",
|
52
|
-
"test/
|
56
|
+
"test/test_collection.rb",
|
53
57
|
"test/test_document.rb",
|
58
|
+
"test/test_filter.rb",
|
54
59
|
"test/test_json_document.rb",
|
55
60
|
"test/test_json_query.rb",
|
56
61
|
"test/test_mongo_document.rb",
|
data/test/helper.rb
CHANGED
@@ -0,0 +1,109 @@
|
|
1
|
+
require 'helper'
|
2
|
+
|
3
|
+
class CollectionTest < Test::Unit::TestCase
|
4
|
+
def setup
|
5
|
+
@db = Mongo::Connection.new.db("test")
|
6
|
+
@mongo_collection = @db['testing']
|
7
|
+
@collection = Rack::JSON::Collection.new(@mongo_collection)
|
8
|
+
end
|
9
|
+
|
10
|
+
def teardown
|
11
|
+
@collection.delete_all
|
12
|
+
end
|
13
|
+
|
14
|
+
def test_finding_a_single_document_by_id
|
15
|
+
mongo_document = @mongo_collection.save({:testing => true, :rating => 5, :title => 'testing', :_id => 1})
|
16
|
+
assert_equal @collection.find(1).attributes, @mongo_collection.find_one(:_id => 1)
|
17
|
+
assert_kind_of Rack::JSON::Document, @collection.find(1)
|
18
|
+
end
|
19
|
+
|
20
|
+
def test_finding_documents_using_search_conditions
|
21
|
+
mongo_document = @mongo_collection.save({:testing => true, :rating => 5, :title => 'testing', :_id => 1})
|
22
|
+
mongo_results = []
|
23
|
+
@mongo_collection.find(:testing => true).each { |row| mongo_results << row }
|
24
|
+
assert_equal @collection.find(:testing => true).first.attributes, mongo_results.first
|
25
|
+
assert_kind_of Rack::JSON::Document, @collection.find(:testing => true).first
|
26
|
+
end
|
27
|
+
|
28
|
+
def test_finding_documents_using_multiple_search_conditions
|
29
|
+
mongo_document = @mongo_collection.save({:testing => true, :rating => 5, :title => 'testing', :_id => 1})
|
30
|
+
mongo_results = []
|
31
|
+
@mongo_collection.find(:testing => true, :rating => 5).each { |row| mongo_results << row }
|
32
|
+
assert_equal @collection.find(:testing => true, :rating => 5).length, mongo_results.length
|
33
|
+
assert_equal @collection.find(:testing => true, :rating => 5).first.attributes, mongo_results.first
|
34
|
+
end
|
35
|
+
|
36
|
+
def test_finding_no_documents_using_search_conditions
|
37
|
+
mongo_document = @mongo_collection.save({:testing => true, :rating => 5, :title => 'testing', :_id => 1})
|
38
|
+
assert_equal @collection.find(:testing => false, :rating => 5), []
|
39
|
+
end
|
40
|
+
|
41
|
+
def test_finding_documents_with_options
|
42
|
+
first_mongo_document = @mongo_collection.save({:testing => true, :rating => 5, :title => 'testing', :_id => 1})
|
43
|
+
second_mongo_document = @mongo_collection.save({:testing => true, :rating => 10, :title => 'testing', :_id => 2})
|
44
|
+
third_mongo_document = @mongo_collection.save({:testing => false, :rating => 10, :title => 'testing', :_id => 3})
|
45
|
+
assert_equal @collection.find({:testing => true}, {:sort => [:rating, :desc]}).length, 2
|
46
|
+
assert_equal @collection.find({:testing => true}, {:sort => [:rating, :desc]}).first.attributes["rating"], 10
|
47
|
+
assert_equal @collection.find({:testing => true}, {:sort => [:rating, :desc]}).first.attributes["testing"], true
|
48
|
+
end
|
49
|
+
|
50
|
+
def test_finding_all_documents_with_options
|
51
|
+
first_mongo_document = @mongo_collection.save({:testing => true, :rating => 5, :title => 'testing', :_id => 1})
|
52
|
+
second_mongo_document = @mongo_collection.save({:testing => true, :rating => 10, :title => 'testing', :_id => 2})
|
53
|
+
assert_equal @collection.all({:sort => [:rating, :desc]}).length, 2
|
54
|
+
assert_equal @collection.all({:sort => [:rating, :desc]}).first["rating"], 10
|
55
|
+
end
|
56
|
+
|
57
|
+
def test_finding_all_documents
|
58
|
+
first_mongo_document = @mongo_collection.save({:testing => true, :rating => 5, :title => 'testing', :_id => 1})
|
59
|
+
second_mongo_document = @mongo_collection.save({:testing => true, :rating => 10, :title => 'testing', :_id => 2})
|
60
|
+
assert_equal @collection.all.length, 2
|
61
|
+
end
|
62
|
+
|
63
|
+
def test_removing_a_document
|
64
|
+
first_mongo_document = @mongo_collection.save({:testing => true, :rating => 5, :title => 'testing', :_id => 1})
|
65
|
+
second_mongo_document = @mongo_collection.save({:testing => true, :rating => 10, :title => 'testing', :_id => 2})
|
66
|
+
assert_equal @collection.all.length, 2
|
67
|
+
@collection.delete(1)
|
68
|
+
assert_equal @collection.all.length, 1
|
69
|
+
assert_equal @collection.all.first["_id"], 2
|
70
|
+
end
|
71
|
+
|
72
|
+
def test_removing_all_documents
|
73
|
+
first_mongo_document = @mongo_collection.save({:testing => true, :rating => 5, :title => 'testing', :_id => 1})
|
74
|
+
second_mongo_document = @mongo_collection.save({:testing => true, :rating => 10, :title => 'testing', :_id => 2})
|
75
|
+
assert_equal @collection.all.length, 2
|
76
|
+
@collection.delete
|
77
|
+
assert_equal @collection.all.length, 0
|
78
|
+
end
|
79
|
+
|
80
|
+
def test_whether_a_document_exists
|
81
|
+
first_mongo_document = @mongo_collection.save({:testing => true, :rating => 5, :title => 'testing', :_id => 1})
|
82
|
+
assert_equal @collection.all.length, 1
|
83
|
+
assert @collection.exists?(1)
|
84
|
+
assert !@collection.exists?(2)
|
85
|
+
end
|
86
|
+
|
87
|
+
def test_creating_a_document
|
88
|
+
assert_equal @collection.all.length, 0
|
89
|
+
assert @collection.create({:_id => 1, :title => 'testing'})
|
90
|
+
assert_equal @collection.all.length, 1
|
91
|
+
assert_equal @collection.find(1).attributes["title"], "testing"
|
92
|
+
end
|
93
|
+
|
94
|
+
def test_updating_an_existing_document_by_id
|
95
|
+
first_mongo_document = @mongo_collection.save({:testing => true, :rating => 5, :title => 'testing', :_id => 1})
|
96
|
+
assert_equal @collection.all.length, 1
|
97
|
+
assert @collection.exists?(1)
|
98
|
+
assert @collection.update(1, {:testing => false})
|
99
|
+
assert_equal @collection.find(1).attributes["testing"], false
|
100
|
+
end
|
101
|
+
|
102
|
+
def test_updating_an_existing_document_but_selector_fails
|
103
|
+
first_mongo_document = @mongo_collection.save({:testing => true, :rating => 5, :title => 'testing', :_id => 1})
|
104
|
+
assert_equal @collection.all.length, 1
|
105
|
+
assert @collection.exists?(1)
|
106
|
+
assert !@collection.update(1, {:testing => false}, {:rating => 6})
|
107
|
+
assert_equal @collection.find(1).attributes["testing"], true
|
108
|
+
end
|
109
|
+
end
|
data/test/test_document.rb
CHANGED
@@ -11,6 +11,13 @@ class DocumentTest < Test::Unit::TestCase
|
|
11
11
|
@collection.drop
|
12
12
|
end
|
13
13
|
|
14
|
+
def test_adding_attributes_to_the_document
|
15
|
+
json = '{"test":"hello"}'
|
16
|
+
document = Rack::JSON::Document.new(json)
|
17
|
+
document.add_attributes("user_id" => 1)
|
18
|
+
assert_equal(1, document.attributes["user_id"])
|
19
|
+
end
|
20
|
+
|
14
21
|
def test_creating_from_json
|
15
22
|
json = '{"test":"hello"}'
|
16
23
|
document = Rack::JSON::Document.new(json)
|
data/test/test_filter.rb
ADDED
@@ -0,0 +1,58 @@
|
|
1
|
+
require 'helper'
|
2
|
+
|
3
|
+
class FilterTest < Test::Unit::TestCase
|
4
|
+
include Rack::Test::Methods
|
5
|
+
include Rack::Utils
|
6
|
+
|
7
|
+
def app
|
8
|
+
Rack::Session::Cookie.new(
|
9
|
+
Rack::JSON::Filter.new lambda { |env|
|
10
|
+
request = Rack::JSON::Request.new(env)
|
11
|
+
env['rack.session'] = {}
|
12
|
+
env['rack.session']['user_id'] = 1
|
13
|
+
[200, {'Content-Length' => request.json.length.to_s, 'Content-Type' => 'text/plain'}, [request.json]]
|
14
|
+
}, :collections => [:testing], :filters => [:user_id]
|
15
|
+
)
|
16
|
+
end
|
17
|
+
|
18
|
+
def test_adding_user_id_query_parameter
|
19
|
+
get '/login'
|
20
|
+
get '/testing'
|
21
|
+
assert_equal "[?user_id=1]", last_request.query_string
|
22
|
+
end
|
23
|
+
|
24
|
+
def test_not_overriding_existing_query_parameters
|
25
|
+
get '/login'
|
26
|
+
get '/testing?[?title=awesome]'
|
27
|
+
assert_equal '[?title=awesome][?user_id=1]', URI.decode(last_request.query_string)
|
28
|
+
end
|
29
|
+
|
30
|
+
def test_reject_request_if_no_session_var
|
31
|
+
get '/testing'
|
32
|
+
assert_equal 412, last_response.status
|
33
|
+
end
|
34
|
+
|
35
|
+
def test_setting_query_params_on_post
|
36
|
+
get '/login'
|
37
|
+
post '/testing', '{ "title": "hello!" }'
|
38
|
+
assert_equal "[?user_id=1]", last_request.query_string
|
39
|
+
end
|
40
|
+
|
41
|
+
def test_setting_query_params_on_put
|
42
|
+
get '/login'
|
43
|
+
put '/testing/1', '{ "title": "hello!" }'
|
44
|
+
assert_equal "[?user_id=1]", last_request.query_string
|
45
|
+
end
|
46
|
+
|
47
|
+
def test_setting_query_params_on_delete
|
48
|
+
get '/login'
|
49
|
+
delete '/testing'
|
50
|
+
assert_equal "[?user_id=1]", last_request.query_string
|
51
|
+
end
|
52
|
+
|
53
|
+
def test_appending_query_param_to_document
|
54
|
+
get '/login'
|
55
|
+
post '/testing', '{ "title": "hello!" }'
|
56
|
+
assert_match /"user_id":1/, last_response.body
|
57
|
+
end
|
58
|
+
end
|
data/test/test_resource.rb
CHANGED
@@ -14,15 +14,25 @@ class ResourceTest < Test::Unit::TestCase
|
|
14
14
|
|
15
15
|
def app
|
16
16
|
Rack::JSON::Resource.new lambda { |env|
|
17
|
-
[404, {'Content-Length' => '9', 'Content-Type' => 'text/plain'}, "Not Found"]
|
17
|
+
[404, {'Content-Length' => '9', 'Content-Type' => 'text/plain'}, ["Not Found"]]
|
18
18
|
}, :collections => [:testing], :db => @db
|
19
19
|
end
|
20
20
|
|
21
|
-
def
|
21
|
+
def test_get_non_existing_resource
|
22
22
|
get '/blah'
|
23
23
|
assert_equal 404, last_response.status
|
24
24
|
end
|
25
25
|
|
26
|
+
def test_post_non_existing_resource
|
27
|
+
post '/blah', '{ "title": "hello!" }'
|
28
|
+
assert_equal 404, last_response.status
|
29
|
+
end
|
30
|
+
|
31
|
+
def test_get_root
|
32
|
+
get '/'
|
33
|
+
assert_equal 404, last_response.status
|
34
|
+
end
|
35
|
+
|
26
36
|
def test_index_method
|
27
37
|
@collection.save({:testing => true})
|
28
38
|
get '/testing'
|
@@ -39,7 +49,7 @@ class ResourceTest < Test::Unit::TestCase
|
|
39
49
|
|
40
50
|
def test_show_a_single_document
|
41
51
|
put '/testing/1', '{"title": "testing first"}'
|
42
|
-
|
52
|
+
post '/testing', '{"title": "testing second"}'
|
43
53
|
get '/testing/1'
|
44
54
|
assert last_response.ok?
|
45
55
|
assert_match /"title":"testing first"/, last_response.body
|
@@ -87,6 +97,22 @@ class ResourceTest < Test::Unit::TestCase
|
|
87
97
|
assert_match /"title":"testing update"/, last_response.body
|
88
98
|
end
|
89
99
|
|
100
|
+
def test_updating_a_document_with_matching_query_params
|
101
|
+
@collection.save({:title => 'testing', :_id => 1, :user_id => 1})
|
102
|
+
put '/testing/1?[?user_id=1]', '{"title": "testing update"}'
|
103
|
+
assert last_response.ok?
|
104
|
+
assert_match /"_id":1/, last_response.body
|
105
|
+
assert_match /"title":"testing update"/, last_response.body
|
106
|
+
end
|
107
|
+
|
108
|
+
def test_updating_a_document_with_non_matching_query_params
|
109
|
+
@collection.save({:title => 'testing', :_id => 1, :user_id => 2})
|
110
|
+
put '/testing/1?[?user_id=1]', '{"title": "testing update"}'
|
111
|
+
assert_equal 404, last_response.status
|
112
|
+
assert_equal @collection.find_one(:_id => 1)["title"], 'testing'
|
113
|
+
assert_nil @collection.find_one(:_id => 1, :user_id => 1)
|
114
|
+
end
|
115
|
+
|
90
116
|
def test_deleting_a_document
|
91
117
|
@collection.save({:title => 'testing', :_id => 1})
|
92
118
|
assert @collection.find_one({:_id => 1})
|
data/test/test_response.rb
CHANGED
@@ -18,7 +18,7 @@ class ResponseTest < Test::Unit::TestCase
|
|
18
18
|
|
19
19
|
def test_response_body
|
20
20
|
response = Rack::JSON::Response.new("test")
|
21
|
-
assert_equal("test", response.to_a[2])
|
21
|
+
assert_equal(["test"], response.to_a[2])
|
22
22
|
end
|
23
23
|
|
24
24
|
def test_setting_the_content_length
|
@@ -33,12 +33,12 @@ class ResponseTest < Test::Unit::TestCase
|
|
33
33
|
|
34
34
|
def test_sending_json
|
35
35
|
response = Rack::JSON::Response.new("{'title': 'hello'}")
|
36
|
-
assert_equal("{'title': 'hello'}", response.to_a[2])
|
36
|
+
assert_equal(["{'title': 'hello'}"], response.to_a[2])
|
37
37
|
end
|
38
38
|
|
39
39
|
def test_head_response
|
40
40
|
response = Rack::JSON::Response.new("test", :head => true)
|
41
|
-
assert_equal("", response.to_a[2])
|
41
|
+
assert_equal([""], response.to_a[2])
|
42
42
|
assert_equal("4", response.to_a[1]["Content-Length"])
|
43
43
|
assert_equal("text/plain", response.to_a[1]["Content-Type"])
|
44
44
|
end
|
metadata
CHANGED
@@ -1,12 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rackjson
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
|
5
|
-
segments:
|
6
|
-
- 0
|
7
|
-
- 1
|
8
|
-
- 1
|
9
|
-
version: 0.1.1
|
4
|
+
version: 0.2.0
|
10
5
|
platform: ruby
|
11
6
|
authors:
|
12
7
|
- Oliver Nightingale
|
@@ -14,65 +9,49 @@ autorequire:
|
|
14
9
|
bindir: bin
|
15
10
|
cert_chain: []
|
16
11
|
|
17
|
-
date: 2010-04-
|
12
|
+
date: 2010-04-26 00:00:00 +01:00
|
18
13
|
default_executable:
|
19
14
|
dependencies:
|
20
15
|
- !ruby/object:Gem::Dependency
|
21
16
|
name: mongo
|
22
|
-
|
23
|
-
|
17
|
+
type: :runtime
|
18
|
+
version_requirement:
|
19
|
+
version_requirements: !ruby/object:Gem::Requirement
|
24
20
|
requirements:
|
25
21
|
- - ">="
|
26
22
|
- !ruby/object:Gem::Version
|
27
|
-
segments:
|
28
|
-
- 0
|
29
|
-
- 19
|
30
|
-
- 1
|
31
23
|
version: 0.19.1
|
32
|
-
|
33
|
-
version_requirements: *id001
|
24
|
+
version:
|
34
25
|
- !ruby/object:Gem::Dependency
|
35
26
|
name: mongo_ext
|
36
|
-
|
37
|
-
|
27
|
+
type: :runtime
|
28
|
+
version_requirement:
|
29
|
+
version_requirements: !ruby/object:Gem::Requirement
|
38
30
|
requirements:
|
39
31
|
- - ">="
|
40
32
|
- !ruby/object:Gem::Version
|
41
|
-
segments:
|
42
|
-
- 0
|
43
|
-
- 19
|
44
|
-
- 1
|
45
33
|
version: 0.19.1
|
46
|
-
|
47
|
-
version_requirements: *id002
|
34
|
+
version:
|
48
35
|
- !ruby/object:Gem::Dependency
|
49
36
|
name: json
|
50
|
-
|
51
|
-
|
37
|
+
type: :runtime
|
38
|
+
version_requirement:
|
39
|
+
version_requirements: !ruby/object:Gem::Requirement
|
52
40
|
requirements:
|
53
41
|
- - ">="
|
54
42
|
- !ruby/object:Gem::Version
|
55
|
-
segments:
|
56
|
-
- 1
|
57
|
-
- 2
|
58
|
-
- 3
|
59
43
|
version: 1.2.3
|
60
|
-
|
61
|
-
version_requirements: *id003
|
44
|
+
version:
|
62
45
|
- !ruby/object:Gem::Dependency
|
63
46
|
name: rack
|
64
|
-
|
65
|
-
|
47
|
+
type: :runtime
|
48
|
+
version_requirement:
|
49
|
+
version_requirements: !ruby/object:Gem::Requirement
|
66
50
|
requirements:
|
67
51
|
- - ">="
|
68
52
|
- !ruby/object:Gem::Version
|
69
|
-
segments:
|
70
|
-
- 1
|
71
|
-
- 0
|
72
|
-
- 1
|
73
53
|
version: 1.0.1
|
74
|
-
|
75
|
-
version_requirements: *id004
|
54
|
+
version:
|
76
55
|
description: A rack end point for storing json documents.
|
77
56
|
email: oliver.n@new-bamboo.co.uk
|
78
57
|
executables: []
|
@@ -90,7 +69,9 @@ files:
|
|
90
69
|
- Rakefile
|
91
70
|
- VERSION
|
92
71
|
- lib/rackjson.rb
|
72
|
+
- lib/rackjson/collection.rb
|
93
73
|
- lib/rackjson/document.rb
|
74
|
+
- lib/rackjson/filter.rb
|
94
75
|
- lib/rackjson/json_document.rb
|
95
76
|
- lib/rackjson/json_query.rb
|
96
77
|
- lib/rackjson/mongo_document.rb
|
@@ -100,7 +81,9 @@ files:
|
|
100
81
|
- rackjson.gemspec
|
101
82
|
- test/helper.rb
|
102
83
|
- test/suite.rb
|
84
|
+
- test/test_collection.rb
|
103
85
|
- test/test_document.rb
|
86
|
+
- test/test_filter.rb
|
104
87
|
- test/test_json_document.rb
|
105
88
|
- test/test_json_query.rb
|
106
89
|
- test/test_mongo_document.rb
|
@@ -119,28 +102,27 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
119
102
|
requirements:
|
120
103
|
- - ">="
|
121
104
|
- !ruby/object:Gem::Version
|
122
|
-
segments:
|
123
|
-
- 0
|
124
105
|
version: "0"
|
106
|
+
version:
|
125
107
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
126
108
|
requirements:
|
127
109
|
- - ">="
|
128
110
|
- !ruby/object:Gem::Version
|
129
|
-
segments:
|
130
|
-
- 0
|
131
111
|
version: "0"
|
112
|
+
version:
|
132
113
|
requirements: []
|
133
114
|
|
134
115
|
rubyforge_project:
|
135
|
-
rubygems_version: 1.3.
|
116
|
+
rubygems_version: 1.3.5
|
136
117
|
signing_key:
|
137
118
|
specification_version: 3
|
138
119
|
summary: A rack end point for storing json documents.
|
139
120
|
test_files:
|
140
121
|
- test/helper.rb
|
141
122
|
- test/suite.rb
|
142
|
-
- test/
|
123
|
+
- test/test_collection.rb
|
143
124
|
- test/test_document.rb
|
125
|
+
- test/test_filter.rb
|
144
126
|
- test/test_json_document.rb
|
145
127
|
- test/test_json_query.rb
|
146
128
|
- test/test_mongo_document.rb
|
data/test/test_builder.rb
DELETED
@@ -1,17 +0,0 @@
|
|
1
|
-
require 'helper'
|
2
|
-
require 'rack/builder'
|
3
|
-
|
4
|
-
class BuilderTest < Test::Unit::TestCase
|
5
|
-
|
6
|
-
def test_expose_resource_shortcut
|
7
|
-
app = Rack::Builder.new do
|
8
|
-
expose_resource :collections => [:testing], :db => Mongo::Connection.new.db("test")
|
9
|
-
run lambda { |env|
|
10
|
-
[404, {'Content-Length' => '9', 'Content-Type' => 'text/plain'}, "Not Found"]
|
11
|
-
}
|
12
|
-
end
|
13
|
-
|
14
|
-
response = Rack::MockRequest.new(app).get('/testing')
|
15
|
-
assert response.ok?
|
16
|
-
end
|
17
|
-
end
|