From 57140b80f00aab43918a3ec3276062823971dec7 Mon Sep 17 00:00:00 2001 From: Azul Date: Tue, 10 Sep 2013 09:44:38 +0200 Subject: bringing over couch_changes from leap_ca including tests --- lib/tapicero/config.rb | 69 +++++++++++++++++++++++++++++++++++++++++++ lib/tapicero/couch_changes.rb | 19 ++++++++++++ lib/tapicero/couch_stream.rb | 25 ++++++++++++++++ 3 files changed, 113 insertions(+) create mode 100644 lib/tapicero/config.rb create mode 100644 lib/tapicero/couch_changes.rb create mode 100644 lib/tapicero/couch_stream.rb (limited to 'lib/tapicero') diff --git a/lib/tapicero/config.rb b/lib/tapicero/config.rb new file mode 100644 index 0000000..cb22792 --- /dev/null +++ b/lib/tapicero/config.rb @@ -0,0 +1,69 @@ +require 'yaml' + +module Tapicero + module Config + extend self + + attr_accessor :users_db_name + attr_accessor :db_prefix + attr_accessor :couch_connection + + def self.load(base_dir, *configs) + configs.each do |file_path| + load_config find_file(base_dir, file_path) + end + end + + # TODO: enable username and password + def couch_host + couch_connection[:protocol] + '://' + + couch_connection[:host] + ':' + + couch_connection[:port] + '/' + end + + private + + def load_config(file_path) + return unless file_path + puts " * Loading configuration #{file_path}" + load_settings YAML.load(File.read(file_path)) + rescue NoMethodError => exc + STDERR.puts "ERROR in file #{file_path}" + exit(1) + end + + def load_settings(hash) + return unless hash + hash.each do |key, value| + apply_setting(key, value) + end + end + + def apply_setting(key, value) + if value.is_a? Hash + value = symbolize_keys(value) + end + self.send("#{key}=", value) + rescue NoMethodError => exc + STDERR.puts "'#{key}' is not a valid option" + raise exc + end + + def self.symbolize_keys(hsh) + newhsh = {} + hsh.keys.each do |key| + newhsh[key.to_sym] = hsh[key] + end + newhsh + end + + def self.find_file(base_dir, file_path) + return nil unless file_path + if defined? CWD + return File.expand_path(file_path, CWD) if File.exists?(File.expand_path(file_path, CWD)) + end + return File.expand_path(file_path, base_dir) if File.exists?(File.expand_path(file_path, base_dir)) + return nil + end + end +end diff --git a/lib/tapicero/couch_changes.rb b/lib/tapicero/couch_changes.rb new file mode 100644 index 0000000..2e668dc --- /dev/null +++ b/lib/tapicero/couch_changes.rb @@ -0,0 +1,19 @@ +module Tapicero + class CouchChanges + def initialize(stream) + @stream = stream + end + + def last_seq + @stream.get "_changes", :limit => 1, :descending => true do |hash| + return hash[:last_seq] + end + end + + def follow + @stream.get "_changes", :feed => :continuous, :since => last_seq do |hash| + yield(hash) + end + end + end +end diff --git a/lib/tapicero/couch_stream.rb b/lib/tapicero/couch_stream.rb new file mode 100644 index 0000000..96c752a --- /dev/null +++ b/lib/tapicero/couch_stream.rb @@ -0,0 +1,25 @@ +require 'yajl/http_stream' + +module Tapicero + class CouchStream + def initialize(database_url) + @database_url = database_url + end + + def get(path, options) + url = url_for(path, options) + # puts url + Yajl::HttpStream.get(url, :symbolize_keys => true) do |hash| + yield(hash) + end + end + + protected + + def url_for(path, options = {}) + url = [@database_url, path].join('/') + url += '?' if options.any? + url += options.map {|k,v| "#{k}=#{v}"}.join('&') + end + end +end -- cgit v1.2.3