low 0.0.3 → 0.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- low (0.0.2)
4
+ low (0.0.4)
5
5
  bson_ext
6
6
  mongo
7
7
  rack
@@ -9,12 +9,12 @@ PATH
9
9
  GEM
10
10
  remote: https://rubygems.org/
11
11
  specs:
12
- bson (1.7.0)
13
- bson_ext (1.7.0)
14
- bson (~> 1.7.0)
12
+ bson (1.6.4)
13
+ bson_ext (1.6.4)
14
+ bson (~> 1.6.4)
15
15
  diff-lcs (1.1.3)
16
- mongo (1.7.0)
17
- bson (~> 1.7.0)
16
+ mongo (1.6.4)
17
+ bson (~> 1.6.4)
18
18
  rack (1.4.1)
19
19
  rack-test (0.6.1)
20
20
  rack (>= 1.0)
@@ -0,0 +1,57 @@
1
+ require 'uri'
2
+ require 'mongo'
3
+
4
+ module Low
5
+ module Mongo
6
+ # The `Util` module provides some handy static Mongo helper methods.
7
+ module Util
8
+ # Given a `string`, return all the mongdb URIs contained therein.
9
+ def self.extract_mongodb_uris(string)
10
+ URI.extract(string, 'mongodb')
11
+ end
12
+
13
+ def self.heroku_remote_mongo
14
+ # If a mongodb URI can be extracted from heroku config,
15
+ if uri = extract_mongodb_uris(`heroku config`).first
16
+
17
+ # build a Mongo::Remote with it.
18
+ Mongo::Remote.new(uri)
19
+ end
20
+ end
21
+
22
+ def self.sync_from_remote(local_database_or_mongo, remote)
23
+ # If a Mongo::Local is specified,
24
+ local = local_database_or_mongo.is_a?(Mongo::Local) ?
25
+
26
+ # use it,
27
+ local_database_or_mongo :
28
+
29
+ # otherwise, assume that it is a database name and build one.
30
+ Mongo::Local.new(local_database_or_mongo)
31
+
32
+ # Extract host and port from the remote URI,
33
+ match = remote.uri.match(URI.regexp('mongodb'))
34
+ remote_host = match[4]
35
+ remote_port = match[5]
36
+
37
+ # and copy the database.
38
+ local.connection.copy_database(
39
+ remote.database,
40
+ local.database,
41
+ "#{remote_host}:#{remote_port}",
42
+ remote.username,
43
+ remote.password
44
+ )
45
+ end
46
+
47
+ def self.sync_heroku(local_database_or_mongo)
48
+ # If there is a remote Heroku Mongo,
49
+ if remote = heroku_remote_mongo
50
+
51
+ # sync it to the specified database or Mongo.
52
+ sync_from_remote(local_database_or_mongo, remote)
53
+ end
54
+ end
55
+ end
56
+ end
57
+ end
data/lib/low/mongo.rb CHANGED
@@ -4,6 +4,8 @@ module Low
4
4
  # The `Mongo` module defines an interface for a Mongo helper
5
5
  # class. It also includes two basic classes, `Local` and `Remote`.
6
6
  module Mongo
7
+ autoload :Util, 'low/mongo/util'
8
+
7
9
  # Simple access to a Mongo::Collection instance.
8
10
  def [](collection)
9
11
  db[collection]
@@ -33,6 +35,12 @@ module Low
33
35
  def database
34
36
  end
35
37
 
38
+ def username
39
+ end
40
+
41
+ def password
42
+ end
43
+
36
44
  # Force a new connection the next time one is needed
37
45
  def reset_connection!
38
46
  @grid = nil
@@ -44,13 +52,15 @@ module Low
44
52
  class Local
45
53
  include Mongo
46
54
 
47
- attr_reader :host, :database
55
+ attr_reader :host, :database, :username, :password
48
56
 
49
57
  # Specify the database upon initialization and
50
58
  # assume that the host is localhost (unless told otherwise).
51
- def initialize(database, host = nil)
59
+ def initialize(database, opts = {})
52
60
  @database = database
53
- @host = host || 'localhost'
61
+ @host = opts[:host] || 'localhost'
62
+ @username = opts[:username]
63
+ @password = opts[:password]
54
64
  end
55
65
  end
56
66
 
@@ -70,9 +80,23 @@ module Low
70
80
  @connection ||= ::Mongo::Connection.from_uri(uri)
71
81
  end
72
82
 
73
- # The database can be extracted from the URI.
83
+ # The database can be extracted from the URI,
74
84
  def database
75
- uri.match(/.*\/(.*)$/)[1]
85
+ @uri.match(/.*\/(.*)$/)[1]
86
+ end
87
+
88
+ # as can the username,
89
+ def username
90
+ if match = @uri.match(/^.*:\/\/(.*?):.*/)
91
+ match[1]
92
+ end
93
+ end
94
+
95
+ # and password.
96
+ def password
97
+ if match = @uri.match(/^.*:\/\/.*?:(.*?)@.*/)
98
+ match[1]
99
+ end
76
100
  end
77
101
  end
78
102
  end
data/lib/low/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Low
2
- VERSION = '0.0.3'
2
+ VERSION = '0.0.4'
3
3
  end
@@ -0,0 +1,33 @@
1
+ require File.dirname(__FILE__) + '/../../spec_helper'
2
+
3
+ describe Low::Mongo::Util do
4
+ describe '.extract_mongodb_uris' do
5
+ it 'should return an array of mongodb URIs in the specified string' do
6
+ string = <<-BLOCK
7
+ GEM_PATH: vendor/bundle/ruby/1.9.1
8
+ LANG: en_US.UTF-8
9
+ MONGOLAB_URI: mongodb://heroku_app1:secret1@mongolab.com:29827/heroku_app1
10
+ MONGOHQ_URI: mongodb://heroku_app2:secret2@mongohq.com:29827/heroku_app2
11
+ RANDOM_URI: http://www.gilt.com
12
+ PATH: bin:vendor/bundle/ruby/1.9.1/bin:/usr/local/bin:/usr/bin:/bin
13
+ RACK_ENV: production
14
+ BLOCK
15
+
16
+ uris = Low::Mongo::Util.extract_mongodb_uris(string)
17
+ uris.length.should == 2
18
+ uris.should include('mongodb://heroku_app1:secret1@mongolab.com:29827/heroku_app1')
19
+ uris.should include('mongodb://heroku_app2:secret2@mongohq.com:29827/heroku_app2')
20
+ end
21
+ end
22
+
23
+ describe '.sync_from_remote' do
24
+ it 'should delegate to #copy_database on the local connection' do
25
+ local = Low::Mongo::Local.new('low_local')
26
+ remote = Low::Mongo::Remote.new('mongodb://khy:secret@mongo.com:1234/low_remote')
27
+ connection = mock(:connection)
28
+ local.should_receive(:connection).and_return(connection)
29
+ connection.should_receive(:copy_database).with('low_remote', 'low_local', 'mongo.com:1234', 'khy', 'secret')
30
+ Low::Mongo::Util.sync_from_remote(local, remote)
31
+ end
32
+ end
33
+ end
@@ -59,11 +59,35 @@ describe Low::Mongo::Local do
59
59
  mongo.host.should == 'localhost'
60
60
  end
61
61
 
62
- it 'should return the value specified at initialization' do
63
- mongo = Low::Mongo::Local.new('low_test', '0.0.0.0')
62
+ it 'should return the value specified by the named parameter at initialization' do
63
+ mongo = Low::Mongo::Local.new('low_test', host: '0.0.0.0')
64
64
  mongo.host.should == '0.0.0.0'
65
65
  end
66
66
  end
67
+
68
+ describe '#username' do
69
+ it 'should return nil if none is specified at initialization' do
70
+ mongo = Low::Mongo::Local.new('low_test')
71
+ mongo.username.should be_nil
72
+ end
73
+
74
+ it 'should return the value specified by the named parameter at initialization' do
75
+ mongo = Low::Mongo::Local.new('low_test', username: 'khy')
76
+ mongo.username.should == 'khy'
77
+ end
78
+ end
79
+
80
+ describe '#password' do
81
+ it 'should return nil if none is specified at initialization' do
82
+ mongo = Low::Mongo::Local.new('low_test')
83
+ mongo.username.should be_nil
84
+ end
85
+
86
+ it 'should return the value specified by the named parameter at initialization' do
87
+ mongo = Low::Mongo::Local.new('low_test', password: 'secret')
88
+ mongo.password.should == 'secret'
89
+ end
90
+ end
67
91
  end
68
92
 
69
93
  describe Low::Mongo::Remote do
@@ -81,6 +105,30 @@ describe Low::Mongo::Remote do
81
105
  end
82
106
  end
83
107
 
108
+ describe '#username' do
109
+ it 'should return nil if the URI does not specify a username' do
110
+ mongo = Low::Mongo::Remote.new('mongodb://mongo.com/low_remote')
111
+ mongo.username.should be_nil
112
+ end
113
+
114
+ it 'should return the appropriate value extracted from the URI' do
115
+ mongo = Low::Mongo::Remote.new('mongodb://khy:secret@mongo.com/low_remote')
116
+ mongo.username.should == 'khy'
117
+ end
118
+ end
119
+
120
+ describe '#password' do
121
+ it 'should return nil if the URI does not specify a password' do
122
+ mongo = Low::Mongo::Remote.new('mongodb://mongo.com/low_remote')
123
+ mongo.password.should be_nil
124
+ end
125
+
126
+ it 'should return the appropriate value extracted from the URI' do
127
+ mongo = Low::Mongo::Remote.new('mongodb://khy:secret@mongo.com/low_remote')
128
+ mongo.password.should == 'secret'
129
+ end
130
+ end
131
+
84
132
  describe '#connection' do
85
133
  it 'should pass uri to the ::Mongo::Connection.from_uri method' do
86
134
  connection = double('mongo connection')
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: low
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-08-29 00:00:00.000000000 Z
12
+ date: 2012-09-05 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rack
@@ -106,9 +106,11 @@ files:
106
106
  - Rakefile
107
107
  - lib/low.rb
108
108
  - lib/low/mongo.rb
109
+ - lib/low/mongo/util.rb
109
110
  - lib/low/subdomain_map.rb
110
111
  - lib/low/version.rb
111
112
  - low.gemspec
113
+ - spec/low/mongo/util_spec.rb
112
114
  - spec/low/mongo_spec.rb
113
115
  - spec/low/subdomain_map_spec.rb
114
116
  - spec/spec_helper.rb
@@ -137,6 +139,7 @@ signing_key:
137
139
  specification_version: 3
138
140
  summary: A low-level utility library.
139
141
  test_files:
142
+ - spec/low/mongo/util_spec.rb
140
143
  - spec/low/mongo_spec.rb
141
144
  - spec/low/subdomain_map_spec.rb
142
145
  - spec/spec_helper.rb