Class: Rack::Mongo

Inherits:
Object
  • Object
show all
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

Constructor Details

#initialize(options = {}) ⇒ Mongo

Returns a new instance of Mongo.

Raises:



10
11
12
13
14
15
16
# File 'lib/rack/mongo.rb', line 10

def initialize(options = {})
  raise ConfigurationError, "You need to setup :db configuration in order to use Rack::Mongo" unless [ Symbol, String ].include?(options[:db].class)

  options = { :host => "localhost", :port => 27017 }.merge(options)
  @db = ::Mongo::Connection.new(options[:host], options[:port]).db(options[:db])
  @db.authenticate(options[:username], options[:password]) if options[:username] || options[: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