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.4"

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Mongo

Returns a new instance of Mongo.

Raises:



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

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



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