rackjson 0.3.1 → 0.3.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 +11 -1
- data/Rakefile +2 -2
- data/VERSION +1 -1
- data/lib/rackjson.rb +2 -0
- data/lib/rackjson/collection.rb +7 -20
- data/lib/rackjson/document.rb +29 -15
- data/lib/rackjson/end_point.rb +4 -0
- data/lib/rackjson/extensions/BSON/object_id.rb +7 -0
- data/lib/rackjson/extensions/BSON/ordered_hash.rb +9 -0
- data/lib/rackjson/filter.rb +1 -1
- data/lib/rackjson/json_document.rb +9 -12
- data/lib/rackjson/mongo_document.rb +2 -5
- data/lib/rackjson/resource.rb +34 -24
- data/lib/rackjson/response.rb +18 -8
- data/rackjson.gemspec +12 -12
- data/test/test_document.rb +10 -10
- data/test/test_json_document.rb +1 -17
- data/test/test_resource.rb +64 -19
- data/test/test_resource_modifier.rb +73 -0
- data/test/test_response.rb +11 -9
- metadata +49 -26
- data/lib/rackjson/base_document.rb +0 -18
- data/test/test_collection.rb +0 -111
data/README.markdown
CHANGED
@@ -23,6 +23,16 @@ In your rackup file:
|
|
23
23
|
|
24
24
|
This will set up a RESTful resource called 'notes' at /notes which will store any JSON document.
|
25
25
|
|
26
|
+
### Options
|
27
|
+
|
28
|
+
Currently you can choose which HTTP verbs the resource will support using the `:only` and `:except` options. For example this will expose the notes resource to only get methods:
|
29
|
+
|
30
|
+
expose_resource :collections => [:notes], :db => Mongo::Connection.new.db("mydb"), :only => [:get]
|
31
|
+
|
32
|
+
And to allow every kind of method except deletes:
|
33
|
+
|
34
|
+
expose_resource :collections => [:notes], :db => Mongo::Connection.new.db("mydb"), :except => [:delete]
|
35
|
+
|
26
36
|
### Restricting Access
|
27
37
|
|
28
38
|
There are three ways of mounting a restful resource with rackjson:
|
@@ -88,7 +98,7 @@ RackJSON will assign an id to this resource as _id. This can be used to access
|
|
88
98
|
Content-Type: application/json
|
89
99
|
Content-Length: 147
|
90
100
|
|
91
|
-
|
101
|
+
{"updated_at":"Sun Apr 11 11:14:17 UTC 2010","title":"hello world!","_id":"4bc1af0934701204fd000001","created_at":"Sun Apr 11 11:14:17 UTC 2010"}
|
92
102
|
|
93
103
|
This resource will also appear in the index of notes resources
|
94
104
|
|
data/Rakefile
CHANGED
@@ -10,9 +10,9 @@ begin
|
|
10
10
|
gem.email = "oliver.n@new-bamboo.co.uk"
|
11
11
|
gem.homepage = "http://github.com/olivernn/rackjson"
|
12
12
|
gem.authors = ["Oliver Nightingale"]
|
13
|
-
gem.add_dependency('mongo', '>=0.
|
13
|
+
gem.add_dependency('mongo', '>=1.0.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.3.
|
1
|
+
0.3.2
|
data/lib/rackjson.rb
CHANGED
@@ -19,5 +19,7 @@ module Rack::JSON
|
|
19
19
|
autoload :Request, 'rackjson/request'
|
20
20
|
autoload :Resource, 'rackjson/resource'
|
21
21
|
autoload :Response, 'rackjson/response'
|
22
|
+
autoload :ObjectID, 'rackjson/extensions/bson/object_id'
|
23
|
+
autoload :OrderedHash, 'rackjson/extensions/bson/ordered_hash'
|
22
24
|
|
23
25
|
end
|
data/lib/rackjson/collection.rb
CHANGED
@@ -4,41 +4,29 @@ module Rack::JSON
|
|
4
4
|
@collection = collection
|
5
5
|
end
|
6
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
7
|
def delete(selector={})
|
16
8
|
@collection.remove(prepared(selector))
|
17
9
|
end
|
18
10
|
|
19
|
-
def delete_all
|
20
|
-
@collection.remove
|
21
|
-
end
|
22
|
-
|
23
11
|
def exists?(selector)
|
24
12
|
!@collection.find(prepared(selector)).to_a.empty?
|
25
13
|
end
|
26
14
|
|
27
15
|
def find(selector, options={})
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
16
|
+
@collection.find(selector, options).inject([]) {|documents, row| documents << Rack::JSON::Document.create(row)}
|
17
|
+
end
|
18
|
+
|
19
|
+
def find_one(selector, options={})
|
20
|
+
find(prepared(selector), options).first
|
33
21
|
end
|
34
22
|
|
35
23
|
def save(document)
|
36
|
-
@collection.save(document)
|
24
|
+
@collection.save(document.to_h)
|
37
25
|
end
|
38
26
|
|
39
27
|
def update(selector, document, query={})
|
40
28
|
if exists?(prepared(selector).merge(query))
|
41
|
-
@collection.update(prepared(selector).merge(query), document, :upsert => false)
|
29
|
+
@collection.update(prepared(selector).merge(query), document.to_h, :upsert => false)
|
42
30
|
else
|
43
31
|
false
|
44
32
|
end
|
@@ -49,6 +37,5 @@ module Rack::JSON
|
|
49
37
|
def prepared selector
|
50
38
|
selector.is_a?(Hash) ? selector : {:_id => selector}
|
51
39
|
end
|
52
|
-
|
53
40
|
end
|
54
41
|
end
|
data/lib/rackjson/document.rb
CHANGED
@@ -1,32 +1,46 @@
|
|
1
1
|
module Rack::JSON
|
2
2
|
class Document
|
3
3
|
|
4
|
-
|
4
|
+
attr_accessor :attributes
|
5
|
+
|
6
|
+
class BadDocumentFormatError < ArgumentError ; end
|
7
|
+
|
8
|
+
def self.create(doc)
|
5
9
|
if doc.is_a? String
|
6
|
-
|
7
|
-
|
8
|
-
|
10
|
+
Rack::JSON::JSONDocument.new(doc)
|
11
|
+
elsif doc.is_a? BSON::OrderedHash
|
12
|
+
Rack::JSON::MongoDocument.new(doc)
|
13
|
+
else
|
14
|
+
raise Rack::JSON::Document::BadDocumentFormatError
|
9
15
|
end
|
10
16
|
end
|
11
17
|
|
18
|
+
|
12
19
|
def add_attributes(pair)
|
13
|
-
|
20
|
+
attributes.merge!(pair)
|
21
|
+
end
|
22
|
+
|
23
|
+
def set_id(val)
|
24
|
+
add_attributes('_id' => val) unless attributes.keys.include? '_id'
|
14
25
|
end
|
15
26
|
|
16
|
-
def
|
17
|
-
|
27
|
+
def to_h
|
28
|
+
attributes
|
18
29
|
end
|
19
30
|
|
20
|
-
def to_json(
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
31
|
+
def to_json(options={})
|
32
|
+
attributes.to_json
|
33
|
+
end
|
34
|
+
|
35
|
+
private
|
36
|
+
|
37
|
+
def set_attributes
|
38
|
+
private_methods.each do |method|
|
39
|
+
if method.match /^set_attribute_\w*$/
|
40
|
+
send method
|
27
41
|
end
|
28
42
|
end
|
29
|
-
gen_attrs.to_json
|
30
43
|
end
|
44
|
+
|
31
45
|
end
|
32
46
|
end
|
data/lib/rackjson/end_point.rb
CHANGED
data/lib/rackjson/filter.rb
CHANGED
@@ -21,7 +21,7 @@ module Rack::JSON
|
|
21
21
|
private
|
22
22
|
|
23
23
|
def append_filters_to_document_in request
|
24
|
-
document = Rack::JSON::Document.
|
24
|
+
document = Rack::JSON::Document.create(request.json)
|
25
25
|
document.add_attributes(request.session.reject { |key, value| !@filters.include?(key.to_sym) })
|
26
26
|
request.set_body(document.to_json)
|
27
27
|
end
|
@@ -1,36 +1,33 @@
|
|
1
1
|
module Rack::JSON
|
2
|
-
class JSONDocument
|
3
|
-
include Rack::JSON::BaseDocument
|
4
|
-
|
5
|
-
attr_reader :attributes
|
2
|
+
class JSONDocument < Rack::JSON::Document
|
6
3
|
|
7
4
|
def initialize(doc)
|
8
5
|
@attributes = JSON.parse(doc)
|
9
6
|
set_attributes
|
10
7
|
end
|
11
8
|
|
12
|
-
def add_id(id)
|
13
|
-
@attributes["_id"] = id unless @attributes["_id"]
|
14
|
-
end
|
15
|
-
|
16
9
|
private
|
17
10
|
|
18
11
|
def set_attribute_dates
|
19
|
-
|
12
|
+
attributes.each_pair do |key, value|
|
20
13
|
if value.class == String && value.match(/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}Z$/)
|
21
|
-
|
14
|
+
attributes[key] = Time.parse(value)
|
22
15
|
end
|
23
16
|
end
|
24
17
|
end
|
25
18
|
|
26
19
|
def set_attribute_ids
|
27
|
-
|
20
|
+
attributes["_id"] = BSON::ObjectID.from_string(attributes["_id"].to_s)
|
28
21
|
rescue BSON::InvalidObjectID
|
29
22
|
return false
|
30
23
|
end
|
31
24
|
|
25
|
+
def set_attribute_created_at
|
26
|
+
attributes["created_at"] = Time.now unless attributes["created_at"]
|
27
|
+
end
|
28
|
+
|
32
29
|
def set_attribute_updated_at
|
33
|
-
|
30
|
+
attributes["updated_at"] = Time.now
|
34
31
|
end
|
35
32
|
end
|
36
33
|
end
|
@@ -1,8 +1,5 @@
|
|
1
1
|
module Rack::JSON
|
2
|
-
class MongoDocument
|
3
|
-
include Rack::JSON::BaseDocument
|
4
|
-
|
5
|
-
attr_accessor :attributes
|
2
|
+
class MongoDocument < Rack::JSON::Document
|
6
3
|
|
7
4
|
def initialize(row)
|
8
5
|
@attributes = row
|
@@ -12,7 +9,7 @@ module Rack::JSON
|
|
12
9
|
private
|
13
10
|
|
14
11
|
def set_attribute_ids
|
15
|
-
|
12
|
+
attributes["_id"] = attributes["_id"].to_s if (attributes["_id"].is_a? BSON::ObjectID)
|
16
13
|
end
|
17
14
|
end
|
18
15
|
end
|
data/lib/rackjson/resource.rb
CHANGED
@@ -1,18 +1,21 @@
|
|
1
1
|
module Rack::JSON
|
2
2
|
class Resource
|
3
3
|
include Rack::JSON::EndPoint
|
4
|
-
|
4
|
+
HTTP_METHODS = [:get, :post, :put, :delete]
|
5
5
|
|
6
6
|
def initialize(app, options)
|
7
7
|
@app = app
|
8
8
|
@collections = options[:collections]
|
9
9
|
@db = options[:db]
|
10
|
+
@methods = options[:only] || HTTP_METHODS - (options[:except] || [])
|
10
11
|
end
|
11
12
|
|
12
13
|
def call(env)
|
13
14
|
request = Rack::JSON::Request.new(env)
|
14
15
|
if bypass? request
|
15
16
|
@app.call(env)
|
17
|
+
elsif method_not_allowed? request
|
18
|
+
render "", :status => 405
|
16
19
|
else
|
17
20
|
@collection = Rack::JSON::Collection.new(@db[request.collection])
|
18
21
|
send(request.request_method.downcase, request)
|
@@ -33,16 +36,28 @@ module Rack::JSON
|
|
33
36
|
|
34
37
|
[:get, :head].each do |method|
|
35
38
|
define_method method do |request|
|
36
|
-
request.
|
37
|
-
documents = @collection.find(request.query.selector, request.query.options)
|
38
|
-
if documents.empty? && request.member_path?
|
39
|
-
render "document not found", :status => 404, :head => (method == :head)
|
40
|
-
else
|
41
|
-
render JSON.generate(documents), :head => (method == :head)
|
42
|
-
end
|
39
|
+
request.member_path? ? get_member(request, method) : get_collection(request, method)
|
43
40
|
end
|
44
41
|
end
|
45
42
|
|
43
|
+
def get_member(request, method)
|
44
|
+
request.query.selector.merge!({:_id => request.resource_id})
|
45
|
+
document = @collection.find_one(request.query.selector, request.query.options)
|
46
|
+
if document
|
47
|
+
render document, :head => (method == :head)
|
48
|
+
else
|
49
|
+
render "document not found", :status => 404, :head => (method == :head)
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
def get_collection(request, method)
|
54
|
+
render @collection.find(request.query.selector, request.query.options)
|
55
|
+
end
|
56
|
+
|
57
|
+
def not_allowed?(request)
|
58
|
+
|
59
|
+
end
|
60
|
+
|
46
61
|
def options(request)
|
47
62
|
if request.collection_path?
|
48
63
|
headers = { "Allow" => "GET, POST" }
|
@@ -53,9 +68,9 @@ module Rack::JSON
|
|
53
68
|
end
|
54
69
|
|
55
70
|
def post(request)
|
56
|
-
document = Rack::JSON::Document.
|
57
|
-
@collection.
|
58
|
-
render document
|
71
|
+
document = Rack::JSON::Document.create(request.json)
|
72
|
+
@collection.save(document)
|
73
|
+
render document, :status => 201
|
59
74
|
rescue JSON::ParserError => error
|
60
75
|
invalid_json error
|
61
76
|
end
|
@@ -67,26 +82,21 @@ module Rack::JSON
|
|
67
82
|
end
|
68
83
|
|
69
84
|
def update(request)
|
70
|
-
document = Rack::JSON::Document.
|
71
|
-
document.
|
72
|
-
if @collection.update(request.resource_id, document
|
73
|
-
render document
|
85
|
+
document = Rack::JSON::Document.create(request.json)
|
86
|
+
document.set_id(request.resource_id)
|
87
|
+
if @collection.update(request.resource_id, document, request.query.selector)
|
88
|
+
render document, :status => 200
|
74
89
|
else
|
75
90
|
render "document not found", :status => 404
|
76
91
|
end
|
77
92
|
end
|
78
93
|
|
79
94
|
def upsert(request)
|
80
|
-
document = Rack::JSON::Document.
|
81
|
-
document.
|
82
|
-
@collection.save(document
|
83
|
-
render document
|
95
|
+
document = Rack::JSON::Document.create(request.json)
|
96
|
+
document.set_id(request.resource_id)
|
97
|
+
@collection.save(document)
|
98
|
+
render document, :status => 201
|
84
99
|
end
|
85
100
|
|
86
|
-
METHODS_NOT_ALLOWED.each do |method|
|
87
|
-
define_method method do |request|
|
88
|
-
render "", :status => 405
|
89
|
-
end
|
90
|
-
end
|
91
101
|
end
|
92
102
|
end
|
data/lib/rackjson/response.rb
CHANGED
@@ -1,16 +1,21 @@
|
|
1
1
|
module Rack::JSON
|
2
2
|
class Response
|
3
|
+
|
4
|
+
class Rack::JSON::Response::BodyFormatError < ArgumentError ; end
|
5
|
+
|
6
|
+
attr_reader :status, :headers, :body
|
7
|
+
|
3
8
|
def initialize(body, options={})
|
4
9
|
@status = options[:status] || 200
|
5
|
-
@body = body
|
6
10
|
@head = options[:head] || false
|
7
11
|
@headers = options[:headers] || {}
|
12
|
+
parse_body(body)
|
8
13
|
set_headers
|
9
14
|
head_response if @head
|
10
15
|
end
|
11
16
|
|
12
17
|
def to_a
|
13
|
-
[
|
18
|
+
[status, headers, [body]]
|
14
19
|
end
|
15
20
|
|
16
21
|
private
|
@@ -19,15 +24,20 @@ module Rack::JSON
|
|
19
24
|
@body = ""
|
20
25
|
end
|
21
26
|
|
22
|
-
def
|
23
|
-
|
24
|
-
|
25
|
-
JSON.parse(@body)
|
27
|
+
def parse_body(body)
|
28
|
+
if body.is_a?(Rack::JSON::Document) || body.is_a?(Array)
|
29
|
+
@body = body.to_json
|
26
30
|
@headers["Content-Type"] = "application/json"
|
27
|
-
|
28
|
-
|
31
|
+
elsif body.is_a? String
|
32
|
+
@body = body
|
29
33
|
@headers["Content-Type"] = "text/plain"
|
34
|
+
else
|
35
|
+
raise Rack::JSON::Response::BodyFormatError
|
30
36
|
end
|
31
37
|
end
|
38
|
+
|
39
|
+
def set_headers
|
40
|
+
@headers["Content-Length"] = body.length.to_s
|
41
|
+
end
|
32
42
|
end
|
33
43
|
end
|
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.3.
|
8
|
+
s.version = "0.3.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-
|
12
|
+
s.date = %q{2010-08-17}
|
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,10 +24,11 @@ Gem::Specification.new do |s|
|
|
24
24
|
"Rakefile",
|
25
25
|
"VERSION",
|
26
26
|
"lib/rackjson.rb",
|
27
|
-
"lib/rackjson/base_document.rb",
|
28
27
|
"lib/rackjson/collection.rb",
|
29
28
|
"lib/rackjson/document.rb",
|
30
29
|
"lib/rackjson/end_point.rb",
|
30
|
+
"lib/rackjson/extensions/BSON/object_id.rb",
|
31
|
+
"lib/rackjson/extensions/BSON/ordered_hash.rb",
|
31
32
|
"lib/rackjson/filter.rb",
|
32
33
|
"lib/rackjson/json_document.rb",
|
33
34
|
"lib/rackjson/json_query.rb",
|
@@ -39,7 +40,6 @@ Gem::Specification.new do |s|
|
|
39
40
|
"rackjson.gemspec",
|
40
41
|
"test/helper.rb",
|
41
42
|
"test/suite.rb",
|
42
|
-
"test/test_collection.rb",
|
43
43
|
"test/test_document.rb",
|
44
44
|
"test/test_filter.rb",
|
45
45
|
"test/test_json_document.rb",
|
@@ -52,12 +52,11 @@ Gem::Specification.new do |s|
|
|
52
52
|
s.homepage = %q{http://github.com/olivernn/rackjson}
|
53
53
|
s.rdoc_options = ["--charset=UTF-8"]
|
54
54
|
s.require_paths = ["lib"]
|
55
|
-
s.rubygems_version = %q{1.3.
|
55
|
+
s.rubygems_version = %q{1.3.6}
|
56
56
|
s.summary = %q{A rack end point for storing json documents.}
|
57
57
|
s.test_files = [
|
58
58
|
"test/helper.rb",
|
59
59
|
"test/suite.rb",
|
60
|
-
"test/test_collection.rb",
|
61
60
|
"test/test_document.rb",
|
62
61
|
"test/test_filter.rb",
|
63
62
|
"test/test_json_document.rb",
|
@@ -65,6 +64,7 @@ Gem::Specification.new do |s|
|
|
65
64
|
"test/test_mongo_document.rb",
|
66
65
|
"test/test_rack_builder.rb",
|
67
66
|
"test/test_resource.rb",
|
67
|
+
"test/test_resource_modifier.rb",
|
68
68
|
"test/test_response.rb"
|
69
69
|
]
|
70
70
|
|
@@ -73,20 +73,20 @@ Gem::Specification.new do |s|
|
|
73
73
|
s.specification_version = 3
|
74
74
|
|
75
75
|
if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
|
76
|
-
s.add_runtime_dependency(%q<mongo>, [">= 0.
|
76
|
+
s.add_runtime_dependency(%q<mongo>, [">= 1.0.0"])
|
77
77
|
s.add_runtime_dependency(%q<mongo_ext>, [">= 0.19.1"])
|
78
|
-
s.add_runtime_dependency(%q<json>, ["
|
78
|
+
s.add_runtime_dependency(%q<json>, ["= 1.2.3"])
|
79
79
|
s.add_runtime_dependency(%q<rack>, [">= 1.0.1"])
|
80
80
|
else
|
81
|
-
s.add_dependency(%q<mongo>, [">= 0.
|
81
|
+
s.add_dependency(%q<mongo>, [">= 1.0.0"])
|
82
82
|
s.add_dependency(%q<mongo_ext>, [">= 0.19.1"])
|
83
|
-
s.add_dependency(%q<json>, ["
|
83
|
+
s.add_dependency(%q<json>, ["= 1.2.3"])
|
84
84
|
s.add_dependency(%q<rack>, [">= 1.0.1"])
|
85
85
|
end
|
86
86
|
else
|
87
|
-
s.add_dependency(%q<mongo>, [">= 0.
|
87
|
+
s.add_dependency(%q<mongo>, [">= 1.0.0"])
|
88
88
|
s.add_dependency(%q<mongo_ext>, [">= 0.19.1"])
|
89
|
-
s.add_dependency(%q<json>, ["
|
89
|
+
s.add_dependency(%q<json>, ["= 1.2.3"])
|
90
90
|
s.add_dependency(%q<rack>, [">= 1.0.1"])
|
91
91
|
end
|
92
92
|
end
|
data/test/test_document.rb
CHANGED
@@ -13,14 +13,14 @@ class DocumentTest < Test::Unit::TestCase
|
|
13
13
|
|
14
14
|
def test_adding_attributes_to_the_document
|
15
15
|
json = '{"test":"hello"}'
|
16
|
-
document = Rack::JSON::Document.
|
16
|
+
document = Rack::JSON::Document.create(json)
|
17
17
|
document.add_attributes("user_id" => 1)
|
18
18
|
assert_equal(1, document.attributes["user_id"])
|
19
19
|
end
|
20
20
|
|
21
21
|
def test_creating_from_json
|
22
22
|
json = '{"test":"hello"}'
|
23
|
-
document = Rack::JSON::Document.
|
23
|
+
document = Rack::JSON::Document.create(json)
|
24
24
|
assert_equal("hello", document.attributes["test"])
|
25
25
|
assert_equal(Time.now.to_s, document.attributes["created_at"].to_s)
|
26
26
|
assert_equal(Time.now.to_s, document.attributes["updated_at"].to_s)
|
@@ -28,7 +28,7 @@ class DocumentTest < Test::Unit::TestCase
|
|
28
28
|
|
29
29
|
def test_creating_from_json_with_id
|
30
30
|
json = '{"_id": "4b9f783ba040140525000001", "test":"hello"}'
|
31
|
-
document = Rack::JSON::Document.
|
31
|
+
document = Rack::JSON::Document.create(json)
|
32
32
|
assert_equal(BSON::ObjectID.from_string('4b9f783ba040140525000001'), document.attributes["_id"])
|
33
33
|
assert_equal("hello", document.attributes["test"])
|
34
34
|
assert_equal(Time.now.to_s, document.attributes["created_at"].to_s)
|
@@ -37,7 +37,7 @@ class DocumentTest < Test::Unit::TestCase
|
|
37
37
|
|
38
38
|
def test_creating_from_json_with_non_object_id
|
39
39
|
json = '{"_id": 1, "test":"hello"}'
|
40
|
-
document = Rack::JSON::Document.
|
40
|
+
document = Rack::JSON::Document.create(json)
|
41
41
|
assert_equal(1, document.attributes["_id"])
|
42
42
|
assert_equal("hello", document.attributes["test"])
|
43
43
|
assert_equal(Time.now.to_s, document.attributes["created_at"].to_s)
|
@@ -46,14 +46,14 @@ class DocumentTest < Test::Unit::TestCase
|
|
46
46
|
|
47
47
|
test "creating from json with single nested document" do
|
48
48
|
json = '{"test":"hello", "nested":{"foo":"bar"}}'
|
49
|
-
document = Rack::JSON::Document.
|
49
|
+
document = Rack::JSON::Document.create(json)
|
50
50
|
assert document.attributes['nested'].is_a?(Hash)
|
51
51
|
assert_equal 'bar', document.attributes['nested']['foo']
|
52
52
|
end
|
53
53
|
|
54
54
|
test "creating from json with multiple nested documents" do
|
55
55
|
json = '{"test":"hello", "nested":[{"foo":"bar"}, {"boo":"far"}]}'
|
56
|
-
document = Rack::JSON::Document.
|
56
|
+
document = Rack::JSON::Document.create(json)
|
57
57
|
assert document.attributes['nested'].is_a?(Array)
|
58
58
|
assert_equal({'foo' => 'bar'}, document.attributes['nested'][0])
|
59
59
|
assert_equal 'far', document.attributes['nested'][1]['boo']
|
@@ -61,9 +61,9 @@ class DocumentTest < Test::Unit::TestCase
|
|
61
61
|
|
62
62
|
def test_adding_id
|
63
63
|
json = '{"test":"hello"}'
|
64
|
-
document = Rack::JSON::Document.
|
64
|
+
document = Rack::JSON::Document.create(json)
|
65
65
|
id = @collection.insert(document.attributes)
|
66
|
-
document.
|
66
|
+
document.set_id(id)
|
67
67
|
assert_equal(id.to_s, document.attributes[:_id].to_s)
|
68
68
|
end
|
69
69
|
|
@@ -71,13 +71,13 @@ class DocumentTest < Test::Unit::TestCase
|
|
71
71
|
@collection.insert({"test"=>"hello"})
|
72
72
|
rows = []
|
73
73
|
@collection.find.each { |r| rows << r }
|
74
|
-
document = Rack::JSON::Document.
|
74
|
+
document = Rack::JSON::Document.create(rows.first)
|
75
75
|
assert_equal("hello", document.attributes["test"])
|
76
76
|
end
|
77
77
|
|
78
78
|
def test_document_created_at
|
79
79
|
json = '{"test":"hello", "created_at": "01/01/2010"}'
|
80
|
-
document = Rack::JSON::Document.
|
80
|
+
document = Rack::JSON::Document.create(json)
|
81
81
|
assert_equal("hello", document.attributes["test"])
|
82
82
|
assert_equal("01/01/2010", document.attributes["created_at"])
|
83
83
|
assert_equal(Time.now.to_s, document.attributes["updated_at"].to_s)
|
data/test/test_json_document.rb
CHANGED
@@ -32,23 +32,7 @@ class JSONDocumentTest < Test::Unit::TestCase
|
|
32
32
|
doc = JSON.generate hash
|
33
33
|
assert_equal(hash["_id"], Rack::JSON::JSONDocument.new(doc).attributes["_id"])
|
34
34
|
end
|
35
|
-
|
36
|
-
def test_adding_an_id
|
37
|
-
hash = { "test" => "I don't have an ID" }
|
38
|
-
doc = JSON.generate hash
|
39
|
-
json_doc = Rack::JSON::JSONDocument.new(doc)
|
40
|
-
json_doc.add_id(1)
|
41
|
-
assert_equal(1, json_doc.attributes["_id"])
|
42
|
-
end
|
43
|
-
|
44
|
-
def test_not_overriding_an_id
|
45
|
-
hash = { "test" => "I do have an ID", "_id" => 2 }
|
46
|
-
doc = JSON.generate hash
|
47
|
-
json_doc = Rack::JSON::JSONDocument.new(doc)
|
48
|
-
json_doc.add_id(1)
|
49
|
-
assert_equal(2 , json_doc.attributes["_id"])
|
50
|
-
end
|
51
|
-
|
35
|
+
|
52
36
|
def test_adding_timestamps
|
53
37
|
hash = { "_id" => 1 }
|
54
38
|
doc = JSON.generate hash
|
data/test/test_resource.rb
CHANGED
@@ -6,6 +6,7 @@ class ResourceTest < Test::Unit::TestCase
|
|
6
6
|
def setup
|
7
7
|
@db = Mongo::Connection.new.db("test")
|
8
8
|
@collection = @db['testing']
|
9
|
+
use_basic_resource
|
9
10
|
end
|
10
11
|
|
11
12
|
def teardown
|
@@ -13,56 +14,87 @@ class ResourceTest < Test::Unit::TestCase
|
|
13
14
|
end
|
14
15
|
|
15
16
|
def app
|
16
|
-
Rack::JSON::Resource.new lambda { |env|
|
17
|
+
Rack::JSON::Resource.new lambda { |env|
|
17
18
|
[404, {'Content-Length' => '9', 'Content-Type' => 'text/plain'}, ["Not Found"]]
|
18
|
-
}, :collections => [:testing], :db => @db
|
19
|
+
}, :collections => [:testing], :db => @db
|
19
20
|
end
|
20
21
|
|
21
|
-
|
22
|
+
def basic_app
|
23
|
+
Rack::JSON::Resource.new lambda { |env|
|
24
|
+
[404, {'Content-Length' => '9', 'Content-Type' => 'text/plain'}, ["Not Found"]]
|
25
|
+
}, :collections => [:testing], :db => @db
|
26
|
+
end
|
27
|
+
|
28
|
+
def get_only_app
|
29
|
+
Rack::JSON::Resource.new lambda { |env|
|
30
|
+
[404, {'Content-Length' => '9', 'Content-Type' => 'text/plain'}, ["Not Found"]]
|
31
|
+
}, :collections => [:testing], :db => @db, :only => [:get]
|
32
|
+
end
|
33
|
+
|
34
|
+
def no_delete_app
|
35
|
+
Rack::JSON::Resource.new lambda { |env|
|
36
|
+
[404, {'Content-Length' => '9', 'Content-Type' => 'text/plain'}, ["Not Found"]]
|
37
|
+
}, :collections => [:testing], :db => @db, :except => [:delete]
|
38
|
+
end
|
39
|
+
|
40
|
+
def use_basic_resource
|
41
|
+
ResourceTest.class_eval { def app; basic_app; end }
|
42
|
+
end
|
43
|
+
|
44
|
+
def use_get_only_resource
|
45
|
+
ResourceTest.class_eval { def app; get_only_app; end }
|
46
|
+
end
|
47
|
+
|
48
|
+
def use_no_delete_resource
|
49
|
+
ResourceTest.class_eval { def app; no_delete_app; end }
|
50
|
+
end
|
51
|
+
|
52
|
+
test "get non existing resource" do
|
22
53
|
get '/blah'
|
23
54
|
assert_equal 404, last_response.status
|
24
55
|
end
|
25
56
|
|
26
|
-
test "
|
57
|
+
test "post non existing resource" do
|
27
58
|
post '/blah', '{ "title": "hello!" }'
|
28
59
|
assert_equal 404, last_response.status
|
29
60
|
end
|
30
61
|
|
31
|
-
test "
|
62
|
+
test "get root" do
|
32
63
|
get '/'
|
33
64
|
assert_equal 404, last_response.status
|
34
65
|
end
|
35
66
|
|
36
|
-
test "
|
67
|
+
test "index method" do
|
37
68
|
@collection.save({:testing => true})
|
38
69
|
get '/testing'
|
39
70
|
assert last_response.ok?
|
40
71
|
assert_match /"testing":true/, last_response.body
|
41
72
|
end
|
42
73
|
|
43
|
-
test "
|
74
|
+
test "creating a document" do
|
44
75
|
put '/testing/1', '{"title": "testing"}'
|
45
76
|
assert_equal 201, last_response.status
|
46
77
|
assert_match /"_id":1/, last_response.body
|
47
78
|
assert_match /"title":"testing"/, last_response.body
|
48
79
|
end
|
49
80
|
|
50
|
-
test "
|
81
|
+
test "show a single document" do
|
51
82
|
put '/testing/1', '{"title": "testing first"}'
|
52
83
|
post '/testing', '{"title": "testing second"}'
|
53
84
|
get '/testing/1'
|
54
85
|
assert last_response.ok?
|
55
86
|
assert_match /"title":"testing first"/, last_response.body
|
56
87
|
assert_no_match /"title":"testing second"/, last_response.body
|
88
|
+
assert_instance_of Hash, JSON.parse(last_response.body)
|
57
89
|
end
|
58
90
|
|
59
|
-
test "
|
91
|
+
test "not finding a specific document" do
|
60
92
|
get '/testing/1'
|
61
93
|
assert_equal 404, last_response.status
|
62
94
|
assert_equal "document not found", last_response.body
|
63
95
|
end
|
64
96
|
|
65
|
-
test "
|
97
|
+
test "index method with query parameters" do
|
66
98
|
@collection.save({:testing => true, :rating => 5, :title => 'testing'})
|
67
99
|
get '/testing?[?title=testing]'
|
68
100
|
assert last_response.ok?
|
@@ -72,7 +104,7 @@ class ResourceTest < Test::Unit::TestCase
|
|
72
104
|
assert_match /"title":"testing"/, last_response.body
|
73
105
|
end
|
74
106
|
|
75
|
-
test "
|
107
|
+
test "index method with sort" do
|
76
108
|
@collection.save({:testing => true, :rating => 5, :title => 'testing'})
|
77
109
|
get '/testing?[/title]'
|
78
110
|
assert last_response.ok?
|
@@ -82,22 +114,21 @@ class ResourceTest < Test::Unit::TestCase
|
|
82
114
|
assert_match /"title":"testing"/, last_response.body
|
83
115
|
end
|
84
116
|
|
85
|
-
test "
|
117
|
+
test "putting a new document" do
|
86
118
|
put '/testing/1', '{"title": "testing update"}'
|
87
119
|
assert_equal 201, last_response.status
|
88
120
|
assert_match /"_id":1/, last_response.body
|
89
121
|
assert_match /"title":"testing update"/, last_response.body
|
90
122
|
end
|
91
123
|
|
92
|
-
test "
|
124
|
+
test "updating a document" do
|
93
125
|
@collection.save({:title => 'testing', :_id => 1})
|
94
126
|
put '/testing/1', '{"title": "testing update"}'
|
95
|
-
assert last_response.ok?
|
96
127
|
assert_match /"_id":1/, last_response.body
|
97
128
|
assert_match /"title":"testing update"/, last_response.body
|
98
129
|
end
|
99
130
|
|
100
|
-
test "
|
131
|
+
test "updating a document with matching query params" do
|
101
132
|
@collection.save({:title => 'testing', :_id => 1, :user_id => 1})
|
102
133
|
put '/testing/1?[?user_id=1]', '{"title": "testing update"}'
|
103
134
|
assert last_response.ok?
|
@@ -105,7 +136,7 @@ class ResourceTest < Test::Unit::TestCase
|
|
105
136
|
assert_match /"title":"testing update"/, last_response.body
|
106
137
|
end
|
107
138
|
|
108
|
-
test "
|
139
|
+
test "updating a document with non matching query params" do
|
109
140
|
@collection.save({:title => 'testing', :_id => 1, :user_id => 2})
|
110
141
|
put '/testing/1?[?user_id=1]', '{"title": "testing update"}'
|
111
142
|
assert_equal 404, last_response.status
|
@@ -113,7 +144,7 @@ class ResourceTest < Test::Unit::TestCase
|
|
113
144
|
assert_nil @collection.find_one(:_id => 1, :user_id => 1)
|
114
145
|
end
|
115
146
|
|
116
|
-
test "
|
147
|
+
test "deleting a document" do
|
117
148
|
@collection.save({:title => 'testing', :_id => 1})
|
118
149
|
assert @collection.find_one({:_id => 1})
|
119
150
|
delete '/testing/1'
|
@@ -121,12 +152,12 @@ class ResourceTest < Test::Unit::TestCase
|
|
121
152
|
assert_nil @collection.find_one({:_id => 1})
|
122
153
|
end
|
123
154
|
|
124
|
-
test "
|
155
|
+
test "deleting only with member path" do
|
125
156
|
delete '/testing'
|
126
157
|
assert_equal 405, last_response.status
|
127
158
|
end
|
128
159
|
|
129
|
-
test "
|
160
|
+
test "posting a document" do
|
130
161
|
post '/testing', '{"title": "testing"}'
|
131
162
|
assert last_response.status == 201
|
132
163
|
assert_match /"title":"testing"/, last_response.body
|
@@ -141,4 +172,18 @@ class ResourceTest < Test::Unit::TestCase
|
|
141
172
|
assert_no_match /"foo":"bar"/, last_response.body
|
142
173
|
assert_match /"title":"testing"/, last_response.body
|
143
174
|
end
|
175
|
+
|
176
|
+
test "restricting the http methods supported using the 'only' option" do
|
177
|
+
use_get_only_resource
|
178
|
+
|
179
|
+
post '/testing', '{"title": "testing"}'
|
180
|
+
assert last_response.status == 405
|
181
|
+
end
|
182
|
+
|
183
|
+
test "restricting the http methods supported using the 'except' option" do
|
184
|
+
use_no_delete_resource
|
185
|
+
|
186
|
+
delete 'testing/1'
|
187
|
+
assert last_response.status == 405
|
188
|
+
end
|
144
189
|
end
|
@@ -0,0 +1,73 @@
|
|
1
|
+
# require 'helper'
|
2
|
+
#
|
3
|
+
# class ResourceModifierTest < Test::Unit::TestCase
|
4
|
+
# include Rack::Test::Methods
|
5
|
+
# include Rack::Utils
|
6
|
+
#
|
7
|
+
# def setup
|
8
|
+
# @db = Mongo::Connection.new.db("test")
|
9
|
+
# @collection = @db['testing']
|
10
|
+
# @doc = @collection.save(:_id => 1, :count => 0, :likers => [])
|
11
|
+
# end
|
12
|
+
#
|
13
|
+
# def teardown
|
14
|
+
# @collection.drop
|
15
|
+
# end
|
16
|
+
#
|
17
|
+
# def app
|
18
|
+
# Rack::JSON::ResourceModifier.new lambda { |env|
|
19
|
+
# [404, {'Content-Length' => '9', 'Content-Type' => 'text/plain'}, ["Not Found"]]
|
20
|
+
# }, :collections => [:testing], :db => @db
|
21
|
+
# end
|
22
|
+
#
|
23
|
+
# test "ignore non matched resources" do
|
24
|
+
# put '/foo/1'
|
25
|
+
# assert_status_not_found last_response
|
26
|
+
# end
|
27
|
+
#
|
28
|
+
# test "ignore non modifier resources" do
|
29
|
+
# put '/testing/1'
|
30
|
+
# assert_status_not_found last_response
|
31
|
+
# end
|
32
|
+
#
|
33
|
+
# test "ignoring modifier requests with the wrong method" do
|
34
|
+
# post '/testing/1/inc?field=count'
|
35
|
+
# assert_status_not_found last_response
|
36
|
+
# end
|
37
|
+
#
|
38
|
+
# test "incrementing a value" do
|
39
|
+
# put '/testing/1/inc?field=count'
|
40
|
+
# assert_status_ok last_response
|
41
|
+
# assert_equal 1, @collection.find_one({:_id => 1})["count"]
|
42
|
+
# end
|
43
|
+
#
|
44
|
+
# test "decrementing a value" do
|
45
|
+
# put '/testing/1/dec?field=count'
|
46
|
+
# assert_status_ok last_response
|
47
|
+
# assert_equal -1, @collection.find_one({:_id => 1})["count"]
|
48
|
+
# end
|
49
|
+
#
|
50
|
+
# test "incrementing a non-existant field" do
|
51
|
+
# put '/testing/1/inc?field=non_existant'
|
52
|
+
# assert_status_ok last_response
|
53
|
+
# assert_equal 0, @collection.find_one({:_id => 1})["count"]
|
54
|
+
# assert_equal 1, @collection.find_one({:_id => 1})["non_existant"]
|
55
|
+
# end
|
56
|
+
#
|
57
|
+
# test "decrementing a non-existent field" do
|
58
|
+
# put '/testing/1/dec?field=non_existant'
|
59
|
+
# assert_status_ok last_response
|
60
|
+
# assert_equal 0, @collection.find_one({:_id => 1})["count"]
|
61
|
+
# assert_equal -1, @collection.find_one({:_id => 1})["non_existant"]
|
62
|
+
# end
|
63
|
+
#
|
64
|
+
# test "pushing a value onto the end of an array" do
|
65
|
+
# testing/1/counter/_inc
|
66
|
+
# testing/1/counter/_dec
|
67
|
+
# testing/1/likers?operation
|
68
|
+
# put '/testing/1/push?field=asdfa' '{"value": [123] }'
|
69
|
+
# put '/testing/1/push', '{"field": "likers", "value": 1}'
|
70
|
+
# assert_status_ok last_response
|
71
|
+
# assert_equal [1], @collection.find_one({:_id => 1})['likers']
|
72
|
+
# end
|
73
|
+
# end
|
data/test/test_response.rb
CHANGED
@@ -8,38 +8,40 @@ class ResponseTest < Test::Unit::TestCase
|
|
8
8
|
|
9
9
|
def test_default_http_status_to_200
|
10
10
|
response = Rack::JSON::Response.new("test")
|
11
|
-
assert_equal(200, response.
|
11
|
+
assert_equal(200, response.status)
|
12
12
|
end
|
13
13
|
|
14
14
|
def test_setting_http_status_code
|
15
15
|
response = Rack::JSON::Response.new("test", :status => 422)
|
16
|
-
assert_equal(422, response.
|
16
|
+
assert_equal(422, response.status)
|
17
17
|
end
|
18
18
|
|
19
19
|
def test_response_body
|
20
20
|
response = Rack::JSON::Response.new("test")
|
21
|
-
assert_equal(
|
21
|
+
assert_equal("test", response.body)
|
22
22
|
end
|
23
23
|
|
24
24
|
def test_setting_the_content_length
|
25
25
|
response = Rack::JSON::Response.new("test")
|
26
|
-
assert_equal("4", response.
|
26
|
+
assert_equal("4", response.headers["Content-Length"])
|
27
27
|
end
|
28
28
|
|
29
29
|
def test_setting_the_content_type
|
30
30
|
response = Rack::JSON::Response.new("test")
|
31
|
-
assert_equal("text/plain", response.
|
31
|
+
assert_equal("text/plain", response.headers["Content-Type"])
|
32
32
|
end
|
33
33
|
|
34
|
-
|
35
|
-
|
36
|
-
|
34
|
+
test "sending document" do
|
35
|
+
h = BSON::OrderedHash.new
|
36
|
+
h[:title] = 'Hello'
|
37
|
+
response = Rack::JSON::Response.new(Rack::JSON::Document.create(h))
|
38
|
+
assert_match(JSON.parse(response.body)['title'], 'Hello')
|
37
39
|
end
|
38
40
|
|
39
41
|
def test_head_response
|
40
42
|
response = Rack::JSON::Response.new("test", :head => true)
|
41
43
|
assert_equal([""], response.to_a[2])
|
42
44
|
assert_equal("4", response.to_a[1]["Content-Length"])
|
43
|
-
assert_equal("text/plain", response.
|
45
|
+
assert_equal("text/plain", response.headers["Content-Type"])
|
44
46
|
end
|
45
47
|
end
|
metadata
CHANGED
@@ -1,7 +1,12 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rackjson
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
|
4
|
+
prerelease: false
|
5
|
+
segments:
|
6
|
+
- 0
|
7
|
+
- 3
|
8
|
+
- 2
|
9
|
+
version: 0.3.2
|
5
10
|
platform: ruby
|
6
11
|
authors:
|
7
12
|
- Oliver Nightingale
|
@@ -9,49 +14,65 @@ autorequire:
|
|
9
14
|
bindir: bin
|
10
15
|
cert_chain: []
|
11
16
|
|
12
|
-
date: 2010-
|
17
|
+
date: 2010-08-17 00:00:00 +01:00
|
13
18
|
default_executable:
|
14
19
|
dependencies:
|
15
20
|
- !ruby/object:Gem::Dependency
|
16
21
|
name: mongo
|
17
|
-
|
18
|
-
|
19
|
-
version_requirements: !ruby/object:Gem::Requirement
|
22
|
+
prerelease: false
|
23
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
20
24
|
requirements:
|
21
25
|
- - ">="
|
22
26
|
- !ruby/object:Gem::Version
|
23
|
-
|
24
|
-
|
27
|
+
segments:
|
28
|
+
- 1
|
29
|
+
- 0
|
30
|
+
- 0
|
31
|
+
version: 1.0.0
|
32
|
+
type: :runtime
|
33
|
+
version_requirements: *id001
|
25
34
|
- !ruby/object:Gem::Dependency
|
26
35
|
name: mongo_ext
|
27
|
-
|
28
|
-
|
29
|
-
version_requirements: !ruby/object:Gem::Requirement
|
36
|
+
prerelease: false
|
37
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
30
38
|
requirements:
|
31
39
|
- - ">="
|
32
40
|
- !ruby/object:Gem::Version
|
41
|
+
segments:
|
42
|
+
- 0
|
43
|
+
- 19
|
44
|
+
- 1
|
33
45
|
version: 0.19.1
|
34
|
-
|
46
|
+
type: :runtime
|
47
|
+
version_requirements: *id002
|
35
48
|
- !ruby/object:Gem::Dependency
|
36
49
|
name: json
|
37
|
-
|
38
|
-
|
39
|
-
version_requirements: !ruby/object:Gem::Requirement
|
50
|
+
prerelease: false
|
51
|
+
requirement: &id003 !ruby/object:Gem::Requirement
|
40
52
|
requirements:
|
41
|
-
- - "
|
53
|
+
- - "="
|
42
54
|
- !ruby/object:Gem::Version
|
55
|
+
segments:
|
56
|
+
- 1
|
57
|
+
- 2
|
58
|
+
- 3
|
43
59
|
version: 1.2.3
|
44
|
-
|
60
|
+
type: :runtime
|
61
|
+
version_requirements: *id003
|
45
62
|
- !ruby/object:Gem::Dependency
|
46
63
|
name: rack
|
47
|
-
|
48
|
-
|
49
|
-
version_requirements: !ruby/object:Gem::Requirement
|
64
|
+
prerelease: false
|
65
|
+
requirement: &id004 !ruby/object:Gem::Requirement
|
50
66
|
requirements:
|
51
67
|
- - ">="
|
52
68
|
- !ruby/object:Gem::Version
|
69
|
+
segments:
|
70
|
+
- 1
|
71
|
+
- 0
|
72
|
+
- 1
|
53
73
|
version: 1.0.1
|
54
|
-
|
74
|
+
type: :runtime
|
75
|
+
version_requirements: *id004
|
55
76
|
description: A rack end point for storing json documents.
|
56
77
|
email: oliver.n@new-bamboo.co.uk
|
57
78
|
executables: []
|
@@ -69,10 +90,11 @@ files:
|
|
69
90
|
- Rakefile
|
70
91
|
- VERSION
|
71
92
|
- lib/rackjson.rb
|
72
|
-
- lib/rackjson/base_document.rb
|
73
93
|
- lib/rackjson/collection.rb
|
74
94
|
- lib/rackjson/document.rb
|
75
95
|
- lib/rackjson/end_point.rb
|
96
|
+
- lib/rackjson/extensions/BSON/object_id.rb
|
97
|
+
- lib/rackjson/extensions/BSON/ordered_hash.rb
|
76
98
|
- lib/rackjson/filter.rb
|
77
99
|
- lib/rackjson/json_document.rb
|
78
100
|
- lib/rackjson/json_query.rb
|
@@ -84,7 +106,6 @@ files:
|
|
84
106
|
- rackjson.gemspec
|
85
107
|
- test/helper.rb
|
86
108
|
- test/suite.rb
|
87
|
-
- test/test_collection.rb
|
88
109
|
- test/test_document.rb
|
89
110
|
- test/test_filter.rb
|
90
111
|
- test/test_json_document.rb
|
@@ -106,25 +127,26 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
106
127
|
requirements:
|
107
128
|
- - ">="
|
108
129
|
- !ruby/object:Gem::Version
|
130
|
+
segments:
|
131
|
+
- 0
|
109
132
|
version: "0"
|
110
|
-
version:
|
111
133
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
112
134
|
requirements:
|
113
135
|
- - ">="
|
114
136
|
- !ruby/object:Gem::Version
|
137
|
+
segments:
|
138
|
+
- 0
|
115
139
|
version: "0"
|
116
|
-
version:
|
117
140
|
requirements: []
|
118
141
|
|
119
142
|
rubyforge_project:
|
120
|
-
rubygems_version: 1.3.
|
143
|
+
rubygems_version: 1.3.6
|
121
144
|
signing_key:
|
122
145
|
specification_version: 3
|
123
146
|
summary: A rack end point for storing json documents.
|
124
147
|
test_files:
|
125
148
|
- test/helper.rb
|
126
149
|
- test/suite.rb
|
127
|
-
- test/test_collection.rb
|
128
150
|
- test/test_document.rb
|
129
151
|
- test/test_filter.rb
|
130
152
|
- test/test_json_document.rb
|
@@ -132,4 +154,5 @@ test_files:
|
|
132
154
|
- test/test_mongo_document.rb
|
133
155
|
- test/test_rack_builder.rb
|
134
156
|
- test/test_resource.rb
|
157
|
+
- test/test_resource_modifier.rb
|
135
158
|
- test/test_response.rb
|
@@ -1,18 +0,0 @@
|
|
1
|
-
module Rack::JSON
|
2
|
-
module BaseDocument
|
3
|
-
|
4
|
-
private
|
5
|
-
|
6
|
-
def set_attribute_created_at
|
7
|
-
@attributes["created_at"] = Time.now unless @attributes["created_at"]
|
8
|
-
end
|
9
|
-
|
10
|
-
def set_attributes
|
11
|
-
private_methods.each do |method|
|
12
|
-
if method.match /^set_attribute_\w*$/
|
13
|
-
send method
|
14
|
-
end
|
15
|
-
end
|
16
|
-
end
|
17
|
-
end
|
18
|
-
end
|
data/test/test_collection.rb
DELETED
@@ -1,111 +0,0 @@
|
|
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
|
-
test "finding a single document by id" do
|
15
|
-
mongo_document = @mongo_collection.save({:testing => true, :rating => 5, :title => 'testing', :_id => 1})
|
16
|
-
assert_equal @collection.find(1).attributes['testing'], @mongo_collection.find_one(:_id => 1)['testing']
|
17
|
-
assert_equal @collection.find(1).attributes['rating'], @mongo_collection.find_one(:_id => 1)['rating']
|
18
|
-
assert_equal @collection.find(1).attributes['title'], @mongo_collection.find_one(:_id => 1)['title']
|
19
|
-
assert_kind_of Rack::JSON::Document, @collection.find(1)
|
20
|
-
end
|
21
|
-
|
22
|
-
def test_finding_documents_using_search_conditions
|
23
|
-
mongo_document = @mongo_collection.save({:testing => true, :rating => 5, :title => 'testing', :_id => 1})
|
24
|
-
mongo_results = []
|
25
|
-
@mongo_collection.find(:testing => true).each { |row| mongo_results << row }
|
26
|
-
assert_equal @collection.find(:testing => true, :rating => 5).first.attributes['testing'], mongo_results.first['testing']
|
27
|
-
assert_kind_of Rack::JSON::Document, @collection.find(:testing => true).first
|
28
|
-
end
|
29
|
-
|
30
|
-
def test_finding_documents_using_multiple_search_conditions
|
31
|
-
mongo_document = @mongo_collection.save({:testing => true, :rating => 5, :title => 'testing', :_id => 1})
|
32
|
-
mongo_results = []
|
33
|
-
@mongo_collection.find(:testing => true, :rating => 5).each { |row| mongo_results << row }
|
34
|
-
assert_equal @collection.find(:testing => true, :rating => 5).length, mongo_results.length
|
35
|
-
assert_equal @collection.find(:testing => true, :rating => 5).first.attributes['testing'], mongo_results.first['testing']
|
36
|
-
end
|
37
|
-
|
38
|
-
def test_finding_no_documents_using_search_conditions
|
39
|
-
mongo_document = @mongo_collection.save({:testing => true, :rating => 5, :title => 'testing', :_id => 1})
|
40
|
-
assert_equal @collection.find(:testing => false, :rating => 5), []
|
41
|
-
end
|
42
|
-
|
43
|
-
def test_finding_documents_with_options
|
44
|
-
first_mongo_document = @mongo_collection.save({:testing => true, :rating => 5, :title => 'testing', :_id => 1})
|
45
|
-
second_mongo_document = @mongo_collection.save({:testing => true, :rating => 10, :title => 'testing', :_id => 2})
|
46
|
-
third_mongo_document = @mongo_collection.save({:testing => false, :rating => 10, :title => 'testing', :_id => 3})
|
47
|
-
assert_equal @collection.find({:testing => true}, {:sort => [:rating, :desc]}).length, 2
|
48
|
-
assert_equal @collection.find({:testing => true}, {:sort => [:rating, :desc]}).first.attributes["rating"], 10
|
49
|
-
assert_equal @collection.find({:testing => true}, {:sort => [:rating, :desc]}).first.attributes["testing"], true
|
50
|
-
end
|
51
|
-
|
52
|
-
def test_finding_all_documents_with_options
|
53
|
-
first_mongo_document = @mongo_collection.save({:testing => true, :rating => 5, :title => 'testing', :_id => 1})
|
54
|
-
second_mongo_document = @mongo_collection.save({:testing => true, :rating => 10, :title => 'testing', :_id => 2})
|
55
|
-
assert_equal @collection.all({:sort => [:rating, :desc]}).length, 2
|
56
|
-
assert_equal @collection.all({:sort => [:rating, :desc]}).first["rating"], 10
|
57
|
-
end
|
58
|
-
|
59
|
-
def test_finding_all_documents
|
60
|
-
first_mongo_document = @mongo_collection.save({:testing => true, :rating => 5, :title => 'testing', :_id => 1})
|
61
|
-
second_mongo_document = @mongo_collection.save({:testing => true, :rating => 10, :title => 'testing', :_id => 2})
|
62
|
-
assert_equal @collection.all.length, 2
|
63
|
-
end
|
64
|
-
|
65
|
-
def test_removing_a_document
|
66
|
-
first_mongo_document = @mongo_collection.save({:testing => true, :rating => 5, :title => 'testing', :_id => 1})
|
67
|
-
second_mongo_document = @mongo_collection.save({:testing => true, :rating => 10, :title => 'testing', :_id => 2})
|
68
|
-
assert_equal @collection.all.length, 2
|
69
|
-
@collection.delete(1)
|
70
|
-
assert_equal @collection.all.length, 1
|
71
|
-
assert_equal @collection.all.first["_id"], 2
|
72
|
-
end
|
73
|
-
|
74
|
-
def test_removing_all_documents
|
75
|
-
first_mongo_document = @mongo_collection.save({:testing => true, :rating => 5, :title => 'testing', :_id => 1})
|
76
|
-
second_mongo_document = @mongo_collection.save({:testing => true, :rating => 10, :title => 'testing', :_id => 2})
|
77
|
-
assert_equal @collection.all.length, 2
|
78
|
-
@collection.delete
|
79
|
-
assert_equal @collection.all.length, 0
|
80
|
-
end
|
81
|
-
|
82
|
-
def test_whether_a_document_exists
|
83
|
-
first_mongo_document = @mongo_collection.save({:testing => true, :rating => 5, :title => 'testing', :_id => 1})
|
84
|
-
assert_equal @collection.all.length, 1
|
85
|
-
assert @collection.exists?(1)
|
86
|
-
assert !@collection.exists?(2)
|
87
|
-
end
|
88
|
-
|
89
|
-
def test_creating_a_document
|
90
|
-
assert_equal @collection.all.length, 0
|
91
|
-
assert @collection.create({:_id => 1, :title => 'testing'})
|
92
|
-
assert_equal @collection.all.length, 1
|
93
|
-
assert_equal @collection.find(1).attributes["title"], "testing"
|
94
|
-
end
|
95
|
-
|
96
|
-
def test_updating_an_existing_document_by_id
|
97
|
-
first_mongo_document = @mongo_collection.save({:testing => true, :rating => 5, :title => 'testing', :_id => 1})
|
98
|
-
assert_equal @collection.all.length, 1
|
99
|
-
assert @collection.exists?(1)
|
100
|
-
assert @collection.update(1, {:testing => false})
|
101
|
-
assert_equal @collection.find(1).attributes["testing"], false
|
102
|
-
end
|
103
|
-
|
104
|
-
def test_updating_an_existing_document_but_selector_fails
|
105
|
-
first_mongo_document = @mongo_collection.save({:testing => true, :rating => 5, :title => 'testing', :_id => 1})
|
106
|
-
assert_equal @collection.all.length, 1
|
107
|
-
assert @collection.exists?(1)
|
108
|
-
assert !@collection.update(1, {:testing => false}, {:rating => 6})
|
109
|
-
assert_equal @collection.find(1).attributes["testing"], true
|
110
|
-
end
|
111
|
-
end
|