rackjson 0.2.0 → 0.2.1
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 +1 -5
- data/lib/rackjson/end_point.rb +22 -0
- data/lib/rackjson/filter.rb +5 -7
- data/lib/rackjson/resource.rb +1 -8
- data/lib/rackjson.rb +1 -22
- data/rackjson.gemspec +3 -2
- data/test/helper.rb +12 -0
- data/test/test_collection.rb +1 -1
- data/test/test_filter.rb +22 -8
- metadata +3 -2
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.2.
|
1
|
+
0.2.1
|
data/lib/rackjson/collection.rb
CHANGED
@@ -0,0 +1,22 @@
|
|
1
|
+
module Rack::JSON
|
2
|
+
module EndPoint
|
3
|
+
|
4
|
+
private
|
5
|
+
|
6
|
+
def bypass? request
|
7
|
+
request.collection.empty? || !(@collections.include? request.collection.to_sym)
|
8
|
+
end
|
9
|
+
|
10
|
+
def bypass_path? request
|
11
|
+
bypass? request
|
12
|
+
end
|
13
|
+
|
14
|
+
def bypass_method? request
|
15
|
+
!@methods.include?(request.request_method.downcase.to_sym)
|
16
|
+
end
|
17
|
+
|
18
|
+
def render body, options={}
|
19
|
+
Rack::JSON::Response.new(body, options).to_a
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
data/lib/rackjson/filter.rb
CHANGED
@@ -1,15 +1,17 @@
|
|
1
1
|
module Rack::JSON
|
2
2
|
class Filter
|
3
|
+
include Rack::JSON::EndPoint
|
3
4
|
|
4
5
|
def initialize(app, options)
|
5
6
|
@app = app
|
6
7
|
@collections = options[:collections]
|
7
8
|
@filters = options[:filters]
|
9
|
+
@methods = options[:methods]
|
8
10
|
end
|
9
11
|
|
10
12
|
def call(env)
|
11
13
|
request = Rack::JSON::Request.new(env)
|
12
|
-
if
|
14
|
+
if bypass_path?(request) || bypass_method?(request)
|
13
15
|
@app.call(env)
|
14
16
|
else
|
15
17
|
apply_filters(request)
|
@@ -26,19 +28,15 @@ module Rack::JSON
|
|
26
28
|
|
27
29
|
def apply_filters(request)
|
28
30
|
@filters.each do |filter|
|
29
|
-
return pre_condition_not_met unless request.session
|
31
|
+
return pre_condition_not_met unless request.session.keys.include? filter.to_s
|
30
32
|
request.add_query_param "[?#{filter}=#{request.session[filter.to_s]}]"
|
31
33
|
end
|
32
34
|
append_filters_to_document_in request if request.post? || request.put?
|
33
35
|
@app.call(request.env)
|
34
36
|
end
|
35
37
|
|
36
|
-
def bypass?(request)
|
37
|
-
request.collection.empty? || !(@collections.include? request.collection.to_sym)
|
38
|
-
end
|
39
|
-
|
40
38
|
def pre_condition_not_met
|
41
|
-
|
39
|
+
render "pre condition not met", :status => 412, :head => true
|
42
40
|
end
|
43
41
|
end
|
44
42
|
end
|
data/lib/rackjson/resource.rb
CHANGED
@@ -1,5 +1,6 @@
|
|
1
1
|
module Rack::JSON
|
2
2
|
class Resource
|
3
|
+
include Rack::JSON::EndPoint
|
3
4
|
METHODS_NOT_ALLOWED = [:trace, :connect]
|
4
5
|
|
5
6
|
def initialize(app, options)
|
@@ -20,10 +21,6 @@ module Rack::JSON
|
|
20
21
|
|
21
22
|
private
|
22
23
|
|
23
|
-
def bypass?(request)
|
24
|
-
request.collection.empty? || !(@collections.include? request.collection.to_sym)
|
25
|
-
end
|
26
|
-
|
27
24
|
def delete(request)
|
28
25
|
if request.member_path?
|
29
26
|
if @collection.delete({:_id => request.resource_id})
|
@@ -69,10 +66,6 @@ module Rack::JSON
|
|
69
66
|
render (error.class.to_s + " :" + error.message), :status => 422
|
70
67
|
end
|
71
68
|
|
72
|
-
def render(body, options={})
|
73
|
-
Rack::JSON::Response.new(body, options).to_a
|
74
|
-
end
|
75
|
-
|
76
69
|
def update(request)
|
77
70
|
document = Rack::JSON::Document.new(request.json)
|
78
71
|
document.add_id(request.resource_id)
|
data/lib/rackjson.rb
CHANGED
@@ -1,25 +1,3 @@
|
|
1
|
-
class Hash
|
2
|
-
def symbolize_keys
|
3
|
-
inject({}) do |options, (key, value)|
|
4
|
-
options[(key.to_sym rescue key) || key] = value
|
5
|
-
options
|
6
|
-
end
|
7
|
-
end
|
8
|
-
|
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
|
20
|
-
end
|
21
|
-
end
|
22
|
-
|
23
1
|
require 'rubygems'
|
24
2
|
require 'json'
|
25
3
|
require 'rack'
|
@@ -28,6 +6,7 @@ require 'time'
|
|
28
6
|
|
29
7
|
module Rack::JSON
|
30
8
|
|
9
|
+
autoload :EndPoint, 'rackjson/end_point'
|
31
10
|
autoload :Collection, 'rackjson/collection'
|
32
11
|
autoload :Filter, 'rackjson/filter'
|
33
12
|
autoload :Document, 'rackjson/document'
|
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.2.
|
8
|
+
s.version = "0.2.1"
|
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-29}
|
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 = [
|
@@ -26,6 +26,7 @@ Gem::Specification.new do |s|
|
|
26
26
|
"lib/rackjson.rb",
|
27
27
|
"lib/rackjson/collection.rb",
|
28
28
|
"lib/rackjson/document.rb",
|
29
|
+
"lib/rackjson/end_point.rb",
|
29
30
|
"lib/rackjson/filter.rb",
|
30
31
|
"lib/rackjson/json_document.rb",
|
31
32
|
"lib/rackjson/json_query.rb",
|
data/test/helper.rb
CHANGED
@@ -10,4 +10,16 @@ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
|
10
10
|
require 'rackjson'
|
11
11
|
|
12
12
|
class Test::Unit::TestCase
|
13
|
+
def self.test(name, &block)
|
14
|
+
test_name = "test_#{name.gsub(/\s+/,'_')}".to_sym
|
15
|
+
defined = instance_method(test_name) rescue false
|
16
|
+
raise "#{test_name} is already defined in #{self}" if defined
|
17
|
+
if block_given?
|
18
|
+
define_method(test_name, &block)
|
19
|
+
else
|
20
|
+
define_method(test_name) do
|
21
|
+
flunk "No implementation provided for #{name}"
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
13
25
|
end
|
data/test/test_collection.rb
CHANGED
@@ -11,7 +11,7 @@ class CollectionTest < Test::Unit::TestCase
|
|
11
11
|
@collection.delete_all
|
12
12
|
end
|
13
13
|
|
14
|
-
|
14
|
+
test "finding a single document by id" do
|
15
15
|
mongo_document = @mongo_collection.save({:testing => true, :rating => 5, :title => 'testing', :_id => 1})
|
16
16
|
assert_equal @collection.find(1).attributes, @mongo_collection.find_one(:_id => 1)
|
17
17
|
assert_kind_of Rack::JSON::Document, @collection.find(1)
|
data/test/test_filter.rb
CHANGED
@@ -11,46 +11,60 @@ class FilterTest < Test::Unit::TestCase
|
|
11
11
|
env['rack.session'] = {}
|
12
12
|
env['rack.session']['user_id'] = 1
|
13
13
|
[200, {'Content-Length' => request.json.length.to_s, 'Content-Type' => 'text/plain'}, [request.json]]
|
14
|
-
}, :collections => [:testing], :filters => [:user_id]
|
14
|
+
}, :collections => [:testing], :filters => [:user_id], :methods => @test_methods || [:get, :post, :put, :delete]
|
15
15
|
)
|
16
16
|
end
|
17
17
|
|
18
|
-
|
18
|
+
test "adding a user id query parameter" do
|
19
19
|
get '/login'
|
20
20
|
get '/testing'
|
21
21
|
assert_equal "[?user_id=1]", last_request.query_string
|
22
22
|
end
|
23
23
|
|
24
|
-
|
24
|
+
test "dont override any existing query parameters" do
|
25
25
|
get '/login'
|
26
26
|
get '/testing?[?title=awesome]'
|
27
27
|
assert_equal '[?title=awesome][?user_id=1]', URI.decode(last_request.query_string)
|
28
28
|
end
|
29
29
|
|
30
|
-
|
30
|
+
test "reject request if no session var" do
|
31
31
|
get '/testing'
|
32
32
|
assert_equal 412, last_response.status
|
33
33
|
end
|
34
34
|
|
35
|
-
|
35
|
+
test "setting query parameters on a post request" do
|
36
36
|
get '/login'
|
37
37
|
post '/testing', '{ "title": "hello!" }'
|
38
38
|
assert_equal "[?user_id=1]", last_request.query_string
|
39
39
|
end
|
40
40
|
|
41
|
-
|
41
|
+
test "setting query params on put requests" do
|
42
42
|
get '/login'
|
43
43
|
put '/testing/1', '{ "title": "hello!" }'
|
44
44
|
assert_equal "[?user_id=1]", last_request.query_string
|
45
45
|
end
|
46
46
|
|
47
|
-
|
47
|
+
test "setting query params on delete requests" do
|
48
48
|
get '/login'
|
49
49
|
delete '/testing'
|
50
50
|
assert_equal "[?user_id=1]", last_request.query_string
|
51
51
|
end
|
52
52
|
|
53
|
-
|
53
|
+
test "setting query params on get requests" do
|
54
|
+
@test_methods = [:get]
|
55
|
+
get '/login'
|
56
|
+
get '/testing'
|
57
|
+
assert_equal "[?user_id=1]", last_request.query_string
|
58
|
+
end
|
59
|
+
|
60
|
+
test "not adding methods when the request method is not filterable" do
|
61
|
+
@test_methods = [:post]
|
62
|
+
get '/login'
|
63
|
+
get '/testing'
|
64
|
+
assert_not_equal "[?user_id=1]", last_request.query_string
|
65
|
+
end
|
66
|
+
|
67
|
+
test "appending query params to a document" do
|
54
68
|
get '/login'
|
55
69
|
post '/testing', '{ "title": "hello!" }'
|
56
70
|
assert_match /"user_id":1/, last_response.body
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rackjson
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Oliver Nightingale
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2010-04-
|
12
|
+
date: 2010-04-29 00:00:00 +01:00
|
13
13
|
default_executable:
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
@@ -71,6 +71,7 @@ files:
|
|
71
71
|
- lib/rackjson.rb
|
72
72
|
- lib/rackjson/collection.rb
|
73
73
|
- lib/rackjson/document.rb
|
74
|
+
- lib/rackjson/end_point.rb
|
74
75
|
- lib/rackjson/filter.rb
|
75
76
|
- lib/rackjson/json_document.rb
|
76
77
|
- lib/rackjson/json_query.rb
|