Permalink
Browse files

Move code for changes listener to a Rake task

  • Loading branch information...
1 parent 41dc723 commit ae1f5649042a98e33b53c2bc2b0f8816bbc0cf5c @karmi committed Sep 22, 2010
Showing with 82 additions and 72 deletions.
  1. +2 −0 Gemfile
  2. +80 −6 Rakefile
  3. +0 −66 changes.rb
View
@@ -5,3 +5,5 @@ gem 'couchrest'
gem 'faker'
gem 'rack'
gem 'activesupport'
+gem 'yajl-ruby'
+gem 'term-ansicolor'
View
@@ -7,21 +7,25 @@ require 'couchrest'
require 'faker'
require 'rack/mime'
require 'active_support/json'
+require 'active_support/core_ext/hash'
+require 'yajl/http_stream'
+require 'term/ansicolor'
class String
- def parameterize
- self.gsub(/[^a-z0-9\-_]+/i, '-').downcase
- end
+ include Term::ANSIColor
+ def parameterize; self.gsub(/[^a-z0-9\-_]+/i, '-').downcase; end
end
class Array
- def random_slice
- shuffle.slice( Kernel.rand(self.size), Kernel.rand(self.size)+1)
- end
+ def random_slice; shuffle.slice( Kernel.rand(self.size), Kernel.rand(self.size)+1); end
end
+# Store connection to CouchDB in this constant
+#
Database = CouchRest.database!('http://127.0.0.1:5984/addressbook')
+
+
desc "Create COUNT documents in adressbook"
task :populate do
count = ENV['COUNT'] || 1
@@ -74,6 +78,8 @@ task :populate do
end
+
+
desc "Upload database logic from ./couchdb/_design/person"
task :views do
require 'couch_docs/design_directory' # Note: Gem version blows up on RestClient version incompatibility with CouchRest
@@ -87,6 +93,8 @@ task :views do
p response
end
+
+
desc "Upload assets from ./couchdb/_design/assets"
task :assets do
assets_folder = Pathname.new('couchdb/_design/assets')
@@ -112,3 +120,69 @@ task :assets do
p doc['_attachments'].keys
Database.save_doc(doc)
end
+
+desc "Display changes in database from the continuous feed"
+task :changes do
+
+ STDOUT.sync = true
+ trap('INT') { puts "\n\n*** Exiting..."; exit(0) }
+
+ module CouchDB
+
+ # Simple listener for CouchDB's _changes feed
+ # <http://guide.couchdb.org/draft/notifications.html>
+ #
+ class Changes
+
+ attr_reader :server, :database, :last_seq
+
+ def initialize(database)
+ raise ArgumentError, "Please pass a database name" unless database
+ @server = 'http://localhost:5984'
+ @database = database
+ @last_seq = 0
+ end
+
+ def url
+ URI.parse("#{server}/#{database}/_changes?feed=continuous&since=#{last_seq}")
+ end
+
+ def listen
+ puts "Listening for changes in '#{database}' since #{last_seq}..."
+ puts '-'*80
+
+ Yajl::HttpStream.get(url, :symbolize_keys => true) do |response|
+ break unless response[:id] && response[:seq]
+ @last_seq = response[:seq]
+
+ pid = Process.fork do
+ exit if response[:id].to_s.include?('_design')
+ current = Yajl::Parser.parse RestClient.get("#{server}/#{database}/#{response[:id]}?revs=true").to_s, :symbolize_keys => true
+ revision = current[:_revisions][:start].to_i > 1 ? "rev=#{current[:_revisions][:start].to_i-1}-#{current[:_revisions][:ids][1]}" : ''
+ previous = Yajl::Parser.parse RestClient.get("#{server}/#{database}/#{response[:id]}?#{revision}").to_s, :symbolize_keys => true
+ previous.delete_if { |key, value| key.to_s =~ /^_rev.*/ }
+ current. delete_if { |key, value| key.to_s =~ /^_rev.*/ }
+ puts "[#{Time.now.strftime('%H:%M:%S')}] Document ID #{response[:id].bold} received updates:"
+ puts "- #{previous.diff(current).inspect}".white.on_red, "+ #{current.diff(previous).inspect}".white.on_green, '-' * 80
+ exit
+ end
+ Process.detach(pid)
+
+ end
+
+ ensure
+ listen unless $!.class == SystemExit
+ end
+
+ end
+ end
+
+ # ---> Run the _changes listener
+ begin
+ CouchDB::Changes.new(ENV['DATABASE']).listen
+ rescue ArgumentError => e
+ puts "[!] #{e.message}"
+ exit(1)
+ end
+
+end
View
@@ -1,66 +0,0 @@
-#!/usr/bin/env ruby
-
-require 'rubygems'
-require 'uri'
-require 'yajl/http_stream'
-require 'rest_client'
-require 'term/ansicolor'
-require 'active_support/core_ext/hash'
-
-class String; include Term::ANSIColor; end
-
-STDOUT.sync = true
-trap('INT') { puts "\n\n*** Exiting..."; exit(0) }
-
-module CouchDB
- class Changes
-
- attr_reader :server, :database, :last_seq
-
- def initialize(database)
- raise ArgumentError, "Please pass a database name" unless database
- @server = 'http://localhost:5984'
- @database = database
- @last_seq = 0
- end
-
- def url
- URI.parse("#{server}/#{database}/_changes?feed=continuous&since=#{last_seq}")
- end
-
- def listen
- puts "Listening for changes in '#{database}'..."
- puts '-'*80
-
- Yajl::HttpStream.get(url, :symbolize_keys => true) do |response|
- break unless response[:id] && response[:seq]
- @last_seq = response[:seq]
-
- pid = Process.fork do
- exit if response[:id].to_s.include?('_design')
- current = Yajl::Parser.parse RestClient.get("#{server}/#{database}/#{response[:id]}?revs=true").to_s, :symbolize_keys => true
- revision = current[:_revisions][:start].to_i > 1 ? "rev=#{current[:_revisions][:start].to_i-1}-#{current[:_revisions][:ids][1]}" : ''
- previous = Yajl::Parser.parse RestClient.get("#{server}/#{database}/#{response[:id]}?#{revision}").to_s, :symbolize_keys => true
- previous.delete_if { |key, value| key.to_s =~ /^_rev.*/ }
- current. delete_if { |key, value| key.to_s =~ /^_rev.*/ }
- puts "[#{Time.now.strftime('%H:%M:%S')}] Document ID #{response[:id].bold} received updates:"
- puts "- #{previous.diff(current).inspect}".white.on_red, "+ #{current.diff(previous).inspect}".white.on_green, '-' * 80
- exit
- end
- Process.detach(pid)
-
- end
-
- ensure
- listen unless $!.class == SystemExit
- end
-
- end
-end
-
-begin
- CouchDB::Changes.new(ARGV.pop).listen
-rescue ArgumentError => e
- puts "[!] #{e.message}"
- exit(1)
-end

0 comments on commit ae1f564

Please sign in to comment.