summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAzul <azul@riseup.net>2012-09-11 12:35:41 +0200
committerAzul <azul@riseup.net>2012-09-11 12:35:41 +0200
commitc331d70638c35d807128e39a9958cab5ba4eeb25 (patch)
tree329667af3f1624fbe49dd6260dd5c032f64fd7ba
parenta52f357906ed5f54b7a3b64e54be79db83c58e7c (diff)
added config class
-rw-r--r--config.yml2
-rw-r--r--leap_ca.rb8
-rw-r--r--lib/config.rb18
-rw-r--r--lib/couch_stream.rb9
-rw-r--r--test/unit/config.yml2
-rw-r--r--test/unit/config_test.rb14
-rw-r--r--test/unit/couch_stream_test.rb5
7 files changed, 47 insertions, 11 deletions
diff --git a/config.yml b/config.yml
new file mode 100644
index 0000000..c78104d
--- /dev/null
+++ b/config.yml
@@ -0,0 +1,2 @@
+database: salticidae_certs
+server: http://localhost:5984
diff --git a/leap_ca.rb b/leap_ca.rb
index a2a1d37..26f3b03 100644
--- a/leap_ca.rb
+++ b/leap_ca.rb
@@ -2,15 +2,15 @@
require 'rubygems'
require 'yajl/http_stream'
+require 'lib/config'
require 'lib/couch_stream'
require 'lib/couch_changes'
-# TODO: read the connection from a config
-SERVER = "http://localhost:5984"
-DATABASE = "salticidae_certs"
def main
- couch = CouchStream.new(SERVER, DATABASE)
+ config = LeapCA::Config.new(File.expand_path("../config.yml", __FILE__))
+ p "Tracking #{config.database} on #{config.server}"
+ couch = CouchStream.new(config)
changes = CouchChanges.new(couch)
changes.follow do |hash|
p hash
diff --git a/lib/config.rb b/lib/config.rb
new file mode 100644
index 0000000..dded140
--- /dev/null
+++ b/lib/config.rb
@@ -0,0 +1,18 @@
+require 'yaml'
+
+module LeapCA
+ class Config
+ def initialize(filename)
+ file = File.new(filename, 'r')
+ @hash = YAML::load(file)
+ end
+
+ def server
+ @hash['server']
+ end
+
+ def database
+ @hash['database']
+ end
+ end
+end
diff --git a/lib/couch_stream.rb b/lib/couch_stream.rb
index bed296a..081688a 100644
--- a/lib/couch_stream.rb
+++ b/lib/couch_stream.rb
@@ -1,7 +1,6 @@
class CouchStream
- def initialize(server, db)
- @server = server
- @db = db
+ def initialize(config)
+ @config = config
end
def get(path, options)
@@ -11,11 +10,11 @@ class CouchStream
yield(hash)
end
end
-
+
protected
def url_for(path, options = {})
- url = @server + @db + '/' + path
+ url = [@config.server, @config.database, path].join('/')
url += '?' if options.any?
url += options.map {|k,v| "#{k}=#{v}"}.join('&')
end
diff --git a/test/unit/config.yml b/test/unit/config.yml
new file mode 100644
index 0000000..725e5af
--- /dev/null
+++ b/test/unit/config.yml
@@ -0,0 +1,2 @@
+database: test-db
+server: test-server
diff --git a/test/unit/config_test.rb b/test/unit/config_test.rb
new file mode 100644
index 0000000..545bedd
--- /dev/null
+++ b/test/unit/config_test.rb
@@ -0,0 +1,14 @@
+require 'test_helper'
+require 'lib/config'
+
+class ConfigTest < MiniTest::Unit::TestCase
+
+ def setup
+ @config = LeapCA::Config.new(File.expand_path("../config.yml", __FILE__))
+ end
+
+ def test_initial_content
+ assert_equal 'test-server', @config.server
+ assert_equal 'test-db', @config.database
+ end
+end
diff --git a/test/unit/couch_stream_test.rb b/test/unit/couch_stream_test.rb
index 5826ad3..438049b 100644
--- a/test/unit/couch_stream_test.rb
+++ b/test/unit/couch_stream_test.rb
@@ -10,7 +10,8 @@ end
class CouchStreamTest < MiniTest::Unit::TestCase
def setup
- @stream = CouchStream.new("http://server/", "database")
+ @config = stub(:server => "http://server", :database => "database")
+ @stream = CouchStream.new(@config)
@url = "http://server/database/_changes?c=d&a=b"
@path = "_changes"
@options = {:a => :b, :c => :d}
@@ -30,5 +31,5 @@ class CouchStreamTest < MiniTest::Unit::TestCase
assert_equal "http://server/database/", @stream.send(:url_for, "")
assert_equal @url, @stream.send(:url_for, @path, @options)
end
-
+
end