summaryrefslogtreecommitdiff
path: root/lib/tapicero
diff options
context:
space:
mode:
Diffstat (limited to 'lib/tapicero')
-rw-r--r--lib/tapicero/config.rb99
-rw-r--r--lib/tapicero/couch_changes.rb89
-rw-r--r--lib/tapicero/couch_stream.rb28
-rw-r--r--lib/tapicero/json_stream.rb31
-rw-r--r--lib/tapicero/version.rb2
5 files changed, 1 insertions, 248 deletions
diff --git a/lib/tapicero/config.rb b/lib/tapicero/config.rb
deleted file mode 100644
index 7d7d0e1..0000000
--- a/lib/tapicero/config.rb
+++ /dev/null
@@ -1,99 +0,0 @@
-require 'yaml'
-
-module Tapicero
- module Config
- extend self
-
- attr_accessor :users_db_name
- attr_accessor :db_prefix
- attr_accessor :couch_connection
- attr_accessor :security
- attr_accessor :seq_file
- attr_accessor :log_file
- attr_accessor :log_level
-
- def self.load(base_dir, *configs)
- loaded = configs.collect do |file_path|
- file = find_file(base_dir, file_path)
- load_config(file)
- end
- init_logger
- log_loaded_configs(loaded.compact)
- end
-
- def couch_host(conf = nil)
- conf ||= couch_connection
- userinfo = [conf[:username], conf[:password]].compact.join(':')
- userinfo += '@' unless userinfo.empty?
- "#{conf[:protocol]}://#{userinfo}#{conf[:host]}:#{conf[:port]}"
- end
-
- def couch_host_without_password
- couch_host couch_connection.merge({:password => nil})
- end
-
- private
-
- def init_logger
- if log_file
- require 'logger'
- Tapicero.logger = Logger.new(log_file)
- else
- require 'syslog/logger'
- Tapicero.logger = Syslog::Logger.new('tapicero')
- end
- Tapicero.logger.level = Logger.const_get(log_level.upcase)
- end
-
- def load_config(file_path)
- return unless file_path
- load_settings YAML.load(File.read(file_path))
- return file_path
- rescue NoMethodError => exc
- init_logger
- Tapicero.logger.fatal "Error in file #{file_path}"
- Tapicero.logger.fatal exc
- 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
-
- def log_loaded_configs(files)
- files.each do |file|
- Tapicero.logger.info "Loaded config from #{file} ."
- end
- end
- end
-end
diff --git a/lib/tapicero/couch_changes.rb b/lib/tapicero/couch_changes.rb
deleted file mode 100644
index fddebec..0000000
--- a/lib/tapicero/couch_changes.rb
+++ /dev/null
@@ -1,89 +0,0 @@
-require 'couchrest'
-require 'fileutils'
-
-module Tapicero
- class CouchChanges
-
- attr_accessor :db
-
- def initialize(db, seq_filename)
- @db = db
- @seq_filename = seq_filename
- read_seq(seq_filename)
- end
-
- def created(hash = {}, &block)
- if block_given?
- @created = block
- else
- @created && @created.call(hash)
- end
- end
-
- def deleted(hash = {}, &block)
- if block_given?
- @deleted = block
- else
- @deleted && @deleted.call(hash)
- end
- end
-
- def listen
- Tapicero.logger.info "listening..."
- Tapicero.logger.debug "Starting at sequence #{since}"
- result = db.changes :feed => :continuous, :since => since, :heartbeat => 1000 do |hash|
- callbacks(hash)
- store_seq(hash["seq"])
- end
- Tapicero.logger.info "couch stream ended unexpectedly."
- Tapicero.logger.debug result.inspect
- end
-
- protected
-
- def since
- @since ||= 0 # fetch_last_seq
- end
-
- def callbacks(hash)
- #changed callback
- # let's not track design document changes
- return if hash['id'].start_with? '_design/'
- return unless changes = hash["changes"]
- return deleted(hash) if hash["deleted"]
- created(hash) if changes[0]["rev"].start_with?('1-')
- #updated callback
- end
-
- def read_seq(seq_filename)
- Tapicero.logger.debug "Looking up sequence here: #{seq_filename}"
- FileUtils.touch(seq_filename)
- unless File.writable?(seq_filename)
- raise StandardError.new("Can't access sequence file")
- end
- @since = File.read(seq_filename)
- if @since == ''
- @since = nil
- Tapicero.logger.debug "Found no sequence in the file."
- else
- Tapicero.logger.debug "Found sequence: #{@since}"
- end
- rescue Errno::ENOENT => e
- Tapicero.logger.warn "No sequence file found. Starting from scratch"
- end
-
- def store_seq(seq)
- File.write(@seq_filename, seq.to_json)
- end
-
- #
- # UNUSED: this is useful for only following new sequences.
- #
- def fetch_last_seq
- hash = db.changes :limit => 1, :descending => true
- Tapicero.logger.info "starting at seq: " + hash["last_seq"]
- return hash["last_seq"]
- end
-
- end
-end
diff --git a/lib/tapicero/couch_stream.rb b/lib/tapicero/couch_stream.rb
deleted file mode 100644
index b63a9fd..0000000
--- a/lib/tapicero/couch_stream.rb
+++ /dev/null
@@ -1,28 +0,0 @@
-#
-# UNUSED: we currently use CouchRest's streamer instead. Still keeping this
-# around because it's a simple alternative that works.
-#
-
-module Tapicero
- class CouchStream
- def initialize(database_url)
- @database_url = database_url
- end
-
- def get(path, options)
- url = url_for(path, options)
- # puts url
- Tapicero::JsonStream.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
diff --git a/lib/tapicero/json_stream.rb b/lib/tapicero/json_stream.rb
deleted file mode 100644
index 64b160f..0000000
--- a/lib/tapicero/json_stream.rb
+++ /dev/null
@@ -1,31 +0,0 @@
-require 'net/http'
-require 'uri'
-require 'yajl'
-
-# UNUSED: We're currently using couchrest instead as that is what we use all
-# over the place. It internally uses curl to fetch the stream.
-#
-# Since Yajl HTTP Stream will go a way in version 2.0 here's a simple substitude.
-#
-module Tapicero
- class JsonStream
-
- def self.get(url, options, &block)
- uri = URI(url)
- parser = Yajl::Parser.new(options)
- parser.on_parse_complete = block
- Net::HTTP.start(uri.host, uri.port) do |http|
- request = Net::HTTP::Get.new uri.request_uri
-
-
- http.request request do |response|
- response.read_body do |chunk|
- parser << chunk
- end
- end
- end
-
- end
- end
-end
-
diff --git a/lib/tapicero/version.rb b/lib/tapicero/version.rb
index ec18e78..3688f62 100644
--- a/lib/tapicero/version.rb
+++ b/lib/tapicero/version.rb
@@ -1,4 +1,4 @@
module Tapicero
- VERSION = "0.1.0"
+ VERSION = "0.2.0"
REQUIRE_PATHS = ['lib']
end