From c52eed9d8c4bdcc767bd7eec7c0b073250ae948f Mon Sep 17 00:00:00 2001 From: Azul Date: Mon, 3 Sep 2012 12:40:12 +0200 Subject: moved CouchChanges and CouchStream into their own files --- lib/couch_changes.rb | 17 +++++++++++++++++ lib/couch_stream.rb | 20 ++++++++++++++++++++ test.rb | 42 +++--------------------------------------- 3 files changed, 40 insertions(+), 39 deletions(-) create mode 100644 lib/couch_changes.rb create mode 100644 lib/couch_stream.rb diff --git a/lib/couch_changes.rb b/lib/couch_changes.rb new file mode 100644 index 0000000..59209a4 --- /dev/null +++ b/lib/couch_changes.rb @@ -0,0 +1,17 @@ +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 diff --git a/lib/couch_stream.rb b/lib/couch_stream.rb new file mode 100644 index 0000000..e3f66cc --- /dev/null +++ b/lib/couch_stream.rb @@ -0,0 +1,20 @@ +class CouchStream + def initialize(server, db) + @server = server + @db = db + 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 + + def url_for(path, options) + url = @server + @db + '/' + path + url += '?' if options.any? + url += options.map {|k,v| "#{k}=#{v}"}.join('&') + end +end diff --git a/test.rb b/test.rb index 9ab73fc..c19db2f 100644 --- a/test.rb +++ b/test.rb @@ -1,46 +1,10 @@ #!/usr/bin/ruby require 'rubygems' -require "yajl/http_stream" +require 'yajl/http_stream' +require 'lib/couch_stream' +require 'lib/couch_changes' -class CouchStream - def initialize(server, db) - @server = server - @db = db - 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 - - def url_for(path, options) - url = @server + @db + '/' + path - url += '?' if options.any? - url += options.map {|k,v| "#{k}=#{v}"}.join('&') - end -end - -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 def main # TODO: read the connection from a config -- cgit v1.2.3