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.4"
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.
11 12 13 14 15 16 17 |
# File 'lib/rack/mongo.rb', line 11 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
19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/rack/mongo.rb', line 19 def call(env) collection_name = env["PATH_INFO"].to_s.sub(/^\//, "") collection = @db.collection(collection_name) if env["REQUEST_METHOD"] == "POST" u = Addressable::URI.new u.query = env["rack.input"].read object_id = collection.insert(u.query_values) response(object_id, 201) else response(collection.find.to_a) end end |