diff options
author | Azul <azul@leap.se> | 2012-09-03 12:34:15 +0200 |
---|---|---|
committer | Azul <azul@leap.se> | 2012-09-03 12:34:15 +0200 |
commit | 10714081c8fbd2e8729950bbb7cee1b76b5a2646 (patch) | |
tree | 65a4396d8d075fcf91eaf9aceb271f01f28447c6 | |
parent | 902c6999fe363dfd08ba366fa586a9c1cddead13 (diff) |
refactored the test script
CouchStream stores the server and database config and wraps the HttpStream calls
CouchChanges sets options to retrieve specifice change feeds
-rw-r--r-- | test.rb | 55 |
1 files changed, 42 insertions, 13 deletions
@@ -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 |