rack-mongo 0.0.3

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/.gitignore ADDED
@@ -0,0 +1,4 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color --format documentation
2
+
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in rack-mongo.gemspec
4
+ gemspec
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require 'bundler/gem_tasks'
@@ -0,0 +1,5 @@
1
+ module Rack
2
+ class Mongo
3
+ VERSION = "0.0.3"
4
+ end
5
+ end
data/lib/rack/mongo.rb ADDED
@@ -0,0 +1,38 @@
1
+ require "rack/mongo/version"
2
+ require "mongo"
3
+ require "yajl"
4
+
5
+ module Rack
6
+ class Mongo
7
+ class ConfigurationError < Exception
8
+ end
9
+
10
+ def initialize(options = {})
11
+ raise ConfigurationError, "You need to setup :db configuration in order to use Rack::Mongo" unless [ Symbol, String ].include?(options[:db].class)
12
+
13
+ options = { :host => "localhost", :port => 27017 }.merge(options)
14
+ @db = ::Mongo::Connection.new(options[:host], options[:port]).db(options[:db])
15
+ @db.authenticate(options[:username], options[:password]) if options[:username] || options[:password]
16
+ end
17
+
18
+ def call(env)
19
+ request = Rack::Request.new(env)
20
+ collection_name = request.path_info.sub(/^\//, "")
21
+ collection = @db.collection(collection_name)
22
+
23
+ if request.post?
24
+ object_id = collection.insert(request.params)
25
+ response(object_id, 201)
26
+ else
27
+ response(collection.find.to_a)
28
+ end
29
+ end
30
+
31
+ private
32
+
33
+ def response(data, status = 200)
34
+ [ status, { "Content-Type" => "application/json" }, [ Yajl::Encoder.encode(data) ] ]
35
+ end
36
+ end
37
+ end
38
+
@@ -0,0 +1,26 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "rack/mongo/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "rack-mongo"
7
+ s.version = Rack::Mongo::VERSION
8
+ s.authors = ["Rafael Souza"]
9
+ s.email = ["me@rafaelss.com"]
10
+ s.homepage = "http://github.com/rafaelss/rack-mongo"
11
+ s.summary = %q{Save data into your mongo collections through a rack app}
12
+ s.description = %q{Save data into your mongo collections through a rack app}
13
+
14
+ s.files = `git ls-files`.split("\n")
15
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
16
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
17
+ s.require_paths = ["lib"]
18
+
19
+ s.add_runtime_dependency "rack", ">= 1.0.0"
20
+ s.add_runtime_dependency "yajl-ruby", "~> 0.8.2"
21
+ s.add_runtime_dependency "mongo", "~> 1.3.1"
22
+ s.add_runtime_dependency "bson_ext", "~> 1.3.1"
23
+ s.add_development_dependency "rack-test", "~> 0.6.1"
24
+ s.add_development_dependency "rspec", "~> 2.6.0"
25
+ end
26
+
@@ -0,0 +1,84 @@
1
+ require "spec_helper"
2
+ require "yajl"
3
+
4
+ describe Rack::Mongo do
5
+ include Rack::Test::Methods
6
+
7
+ def app(options = {})
8
+ options = { :db => "test_db" }.merge(options)
9
+ Rack::Mongo.new(options)
10
+ end
11
+
12
+ describe "db configuration" do
13
+ it "should use default configuration for host and port" do
14
+ connection = mock("Connection")
15
+ connection.should_receive(:db).with("test_db")
16
+ Mongo::Connection.should_receive(:new).with("localhost", 27017).and_return(connection)
17
+
18
+ app
19
+ end
20
+
21
+ it "should raise an error if :db is not configured" do
22
+ lambda { app(:db => nil) }.should raise_error(Rack::Mongo::ConfigurationError)
23
+ end
24
+
25
+ it "should accept custom host and port configuration" do
26
+ connection = mock("Connection")
27
+ connection.should_receive(:db).with("test_db")
28
+ Mongo::Connection.should_receive(:new).with("my.custom.host", 9876).and_return(connection)
29
+
30
+ app(:host => "my.custom.host", :port => 9876)
31
+ end
32
+
33
+ it "should authenticate if username and password is provided" do
34
+ db = mock("DB")
35
+ db.should_receive(:authenticate).with("foo", "bar")
36
+ connection = mock("Connection")
37
+ connection.should_receive(:db).with("test_db").and_return(db)
38
+ Mongo::Connection.should_receive(:new).with("localhost", 27017).and_return(connection)
39
+
40
+ app(:username => "foo", :password => "bar")
41
+ end
42
+ end
43
+
44
+ describe "POST /people" do
45
+ before :each do
46
+ object_id = mock("ObjectId")
47
+ object_id.should_receive(:to_json).and_return("{\"$oid\": \"4e31f5a97cb7243591000002\"}")
48
+ collection = mock("Collection")
49
+ collection.should_receive(:insert).with("first_name" => "Rafael", "last_name" => "Souza").and_return(object_id)
50
+ db = mock("DB")
51
+ db.should_receive(:collection).with("people").and_return(collection)
52
+ connection = mock("Connection")
53
+ connection.should_receive(:db).with("test_db").and_return(db)
54
+ Mongo::Connection.should_receive(:new).with("localhost", 27017).and_return(connection)
55
+ end
56
+
57
+ it "should save person data in a collection called 'people'" do
58
+ post "/people", :first_name => "Rafael", :last_name => "Souza"
59
+
60
+ last_response.status.should == 201
61
+ Yajl::Parser.parse(last_response.body).should == { '$oid' => "4e31f5a97cb7243591000002" }
62
+ end
63
+ end
64
+
65
+ describe "GET /people" do
66
+ before :each do
67
+ collection = mock("Collection")
68
+ collection.should_receive(:find).and_return([{ "_id" => BSON::ObjectId('4e31f5987cb7243591000001'), "first_name" => "Rafael", "last_name" => "Souza" }])
69
+ db = mock("DB")
70
+ db.should_receive(:collection).with("people").and_return(collection)
71
+ connection = mock("Connection")
72
+ connection.should_receive(:db).with("test_db").and_return(db)
73
+ Mongo::Connection.should_receive(:new).with("localhost", 27017).and_return(connection)
74
+ end
75
+
76
+ it "should return rows stored in people collection" do
77
+ get "/people"
78
+
79
+ last_response.status.should == 200
80
+ Yajl::Parser.parse(last_response.body).should == [ { "_id" => { "$oid" => "4e31f5987cb7243591000001" }, "first_name" => "Rafael", "last_name" => "Souza" } ]
81
+ end
82
+ end
83
+ end
84
+
@@ -0,0 +1,9 @@
1
+ require "rack/mongo"
2
+ require "rack/test"
3
+
4
+ Dir[File.dirname(__FILE__) + "/support/**/*.rb"].each {|file| require file}
5
+
6
+ RSpec.configure do |config|
7
+ # config.include Helpers
8
+ end
9
+
metadata ADDED
@@ -0,0 +1,122 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rack-mongo
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.3
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Rafael Souza
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2011-08-03 00:00:00.000000000Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rack
16
+ requirement: &2154352540 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: 1.0.0
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *2154352540
25
+ - !ruby/object:Gem::Dependency
26
+ name: yajl-ruby
27
+ requirement: &2154352040 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ~>
31
+ - !ruby/object:Gem::Version
32
+ version: 0.8.2
33
+ type: :runtime
34
+ prerelease: false
35
+ version_requirements: *2154352040
36
+ - !ruby/object:Gem::Dependency
37
+ name: mongo
38
+ requirement: &2154351580 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ~>
42
+ - !ruby/object:Gem::Version
43
+ version: 1.3.1
44
+ type: :runtime
45
+ prerelease: false
46
+ version_requirements: *2154351580
47
+ - !ruby/object:Gem::Dependency
48
+ name: bson_ext
49
+ requirement: &2154351120 !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: 1.3.1
55
+ type: :runtime
56
+ prerelease: false
57
+ version_requirements: *2154351120
58
+ - !ruby/object:Gem::Dependency
59
+ name: rack-test
60
+ requirement: &2154350660 !ruby/object:Gem::Requirement
61
+ none: false
62
+ requirements:
63
+ - - ~>
64
+ - !ruby/object:Gem::Version
65
+ version: 0.6.1
66
+ type: :development
67
+ prerelease: false
68
+ version_requirements: *2154350660
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec
71
+ requirement: &2154350200 !ruby/object:Gem::Requirement
72
+ none: false
73
+ requirements:
74
+ - - ~>
75
+ - !ruby/object:Gem::Version
76
+ version: 2.6.0
77
+ type: :development
78
+ prerelease: false
79
+ version_requirements: *2154350200
80
+ description: Save data into your mongo collections through a rack app
81
+ email:
82
+ - me@rafaelss.com
83
+ executables: []
84
+ extensions: []
85
+ extra_rdoc_files: []
86
+ files:
87
+ - .gitignore
88
+ - .rspec
89
+ - Gemfile
90
+ - Rakefile
91
+ - lib/rack/mongo.rb
92
+ - lib/rack/mongo/version.rb
93
+ - rack-mongo.gemspec
94
+ - spec/rack/mongo_spec.rb
95
+ - spec/spec_helper.rb
96
+ homepage: http://github.com/rafaelss/rack-mongo
97
+ licenses: []
98
+ post_install_message:
99
+ rdoc_options: []
100
+ require_paths:
101
+ - lib
102
+ required_ruby_version: !ruby/object:Gem::Requirement
103
+ none: false
104
+ requirements:
105
+ - - ! '>='
106
+ - !ruby/object:Gem::Version
107
+ version: '0'
108
+ required_rubygems_version: !ruby/object:Gem::Requirement
109
+ none: false
110
+ requirements:
111
+ - - ! '>='
112
+ - !ruby/object:Gem::Version
113
+ version: '0'
114
+ requirements: []
115
+ rubyforge_project:
116
+ rubygems_version: 1.8.6
117
+ signing_key:
118
+ specification_version: 3
119
+ summary: Save data into your mongo collections through a rack app
120
+ test_files:
121
+ - spec/rack/mongo_spec.rb
122
+ - spec/spec_helper.rb