mongo_connector 0.0.1.pre
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/mongo_connector.rb +40 -0
- metadata +68 -0
@@ -0,0 +1,40 @@
|
|
1
|
+
require 'mongo'
|
2
|
+
|
3
|
+
class MongoConnector
|
4
|
+
# Connect to a Mongo database and authenticate
|
5
|
+
#
|
6
|
+
# Example:
|
7
|
+
# >> mc = MongoConnector.new('http://user:password@my.mongo.host.com/my_db', 'my_db')
|
8
|
+
# >> mc.connection
|
9
|
+
# => <#Mongo::Connection ... >
|
10
|
+
#
|
11
|
+
# Arguments:
|
12
|
+
# default_collection: String
|
13
|
+
#
|
14
|
+
|
15
|
+
attr_accessor :connection
|
16
|
+
|
17
|
+
def initialize(mongo_uri = nil, default_collection = nil)
|
18
|
+
if [nil ,''].include? mongo_uri
|
19
|
+
if [nil, ''].include? default_collection
|
20
|
+
raise 'Could not connect to any MongoDB.'
|
21
|
+
else
|
22
|
+
@connection = Mongo::Connection.new.db(default_collection)
|
23
|
+
end
|
24
|
+
else
|
25
|
+
url = URI.parse(mongo_uri)
|
26
|
+
conn = Mongo::Connection.new(url.host, url.port)
|
27
|
+
@connection = conn.db(url.path.gsub(/^\//, ''))
|
28
|
+
@connection.authenticate(url.user, url.password) if authenticate?(url)
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
VERSION = '0.0.0.pre'
|
33
|
+
|
34
|
+
private
|
35
|
+
def authenticate?(url)
|
36
|
+
blanks = ['', nil]
|
37
|
+
not (blanks.include?(url.user) && blanks.include?(url.password))
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
metadata
ADDED
@@ -0,0 +1,68 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: mongo_connector
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1.pre
|
5
|
+
prerelease: 6
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Lucas Wilson-Richter
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-03-03 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: mongo
|
16
|
+
requirement: &70252856546660 !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ~>
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '1.6'
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: *70252856546660
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: bson_ext
|
27
|
+
requirement: &70252856545940 !ruby/object:Gem::Requirement
|
28
|
+
none: false
|
29
|
+
requirements:
|
30
|
+
- - ~>
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: 1.8.2
|
33
|
+
type: :runtime
|
34
|
+
prerelease: false
|
35
|
+
version_requirements: *70252856545940
|
36
|
+
description: Connect to a Mongo database using the given URI if it is not blank, or
|
37
|
+
else a default collection on the local Mongo.
|
38
|
+
email: da.maestro@gmail.com
|
39
|
+
executables: []
|
40
|
+
extensions: []
|
41
|
+
extra_rdoc_files: []
|
42
|
+
files:
|
43
|
+
- lib/mongo_connector.rb
|
44
|
+
homepage: http://github.com/lucaswilric/mongo_connector
|
45
|
+
licenses: []
|
46
|
+
post_install_message:
|
47
|
+
rdoc_options: []
|
48
|
+
require_paths:
|
49
|
+
- lib
|
50
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
51
|
+
none: false
|
52
|
+
requirements:
|
53
|
+
- - ! '>='
|
54
|
+
- !ruby/object:Gem::Version
|
55
|
+
version: '0'
|
56
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ! '>'
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: 1.3.1
|
62
|
+
requirements: []
|
63
|
+
rubyforge_project:
|
64
|
+
rubygems_version: 1.8.17
|
65
|
+
signing_key:
|
66
|
+
specification_version: 3
|
67
|
+
summary: Mongo connector
|
68
|
+
test_files: []
|