From 10714081c8fbd2e8729950bbb7cee1b76b5a2646 Mon Sep 17 00:00:00 2001 From: Azul Date: Mon, 3 Sep 2012 12:34:15 +0200 Subject: refactored the test script CouchStream stores the server and database config and wraps the HttpStream calls CouchChanges sets options to retrieve specifice change feeds --- test.rb | 55 ++++++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 42 insertions(+), 13 deletions(-) diff --git a/test.rb b/test.rb index 477be2e..9ab73fc 100644 --- a/test.rb +++ b/test.rb @@ -3,23 +3,52 @@ require 'rubygems' require "yajl/http_stream" -def main - url = "http://localhost:5984/" - db = "salticidae_certs" - feed_type = "continuous" - since = "0" - queue = "changes" - - # descending = true for last changes first. limit = 1 - Yajl::HttpStream.get("#{url}#{db}/_changes?limit=1&descending=true", :symbolize_keys => true) do |hash| - since = hash[:last_seq] +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 - Yajl::HttpStream.get("#{url}#{db}/_changes?feed=#{feed_type}&since=#{since}", :symbolize_keys => true) do |hash| - if hash[:id] - p hash + 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 + couch = CouchStream.new("http://localhost:5984/", "salticidae_certs") + changes = CouchChanges.new(couch) + changes.follow do |hash| + p hash + end +end + main -- cgit v1.2.3