Class: Rack::Mongo
- Inherits:
-
Object
- Object
- Rack::Mongo
- Defined in:
- lib/rack/mongo.rb,
lib/rack/mongo/version.rb
Defined Under Namespace
Classes: ConfigurationError
Constant Summary collapse
- VERSION =
"0.0.3"
Instance Method Summary collapse
- #call(env) ⇒ Object
-
#initialize(options = {}) ⇒ Mongo
constructor
A new instance of Mongo.
Constructor Details
#initialize(options = {}) ⇒ Mongo
Returns a new instance of Mongo.
10 11 12 13 14 15 16 |
# File 'lib/rack/mongo.rb', line 10 def initialize( = {}) raise ConfigurationError, "You need to setup :db configuration in order to use Rack::Mongo" unless [ Symbol, String ].include?([:db].class) = { :host => "localhost", :port => 27017 }.merge() @db = ::Mongo::Connection.new([:host], [:port]).db([:db]) @db.authenticate([:username], [:password]) if [:username] || [:password] end |
Instance Method Details
#call(env) ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/rack/mongo.rb', line 18 def call(env) request = Rack::Request.new(env) collection_name = request.path_info.sub(/^\//, "") collection = @db.collection(collection_name) if request.post? object_id = collection.insert(request.params) response(object_id, 201) else response(collection.find.to_a) end end |