added streamer class

This commit is contained in:
Chris Anderson 2008-09-07 15:28:20 -07:00
parent 4630fcd944
commit 9e7738fd74
4 changed files with 55 additions and 1 deletions

View file

@ -6,5 +6,6 @@ require File.dirname(__FILE__) + '/couch_rest'
require File.dirname(__FILE__) + '/database'
require File.dirname(__FILE__) + '/pager'
require File.dirname(__FILE__) + '/file_manager'
require File.dirname(__FILE__) + '/streamer'

View file

@ -3,7 +3,8 @@ require "base64"
class CouchRest
class Database
attr_accessor :server, :host, :name
attr_reader :server, :host, :name, :root
def initialize server, name
@name = name
@server = server

29
lib/streamer.rb Normal file
View file

@ -0,0 +1,29 @@
class CouchRest
class Streamer
attr_accessor :db
def initialize db
@db = db
end
def view name, params = nil
urlst = /^_/.match(name) ? "#{@db.root}/#{name}" : "#{@db.root}/_view/#{name}"
url = CouchRest.paramify_url urlst, params
IO.popen("curl --silent #{url}") do |view|
view.gets # discard header
while row = parse_line(view.gets)
yield row
end
end
end
private
def parse_line line
return nil unless line
if /(\{.*\}),?/.match(line.chomp)
JSON.parse($1)
end
end
end
end

23
spec/streamer_spec.rb Normal file
View file

@ -0,0 +1,23 @@
require File.dirname(__FILE__) + '/spec_helper'
describe CouchRest::Streamer do
before(:all) do
@cr = CouchRest.new(COUCHHOST)
@db = @cr.database(TESTDB)
@db.delete! rescue nil
@db = @cr.create_db(TESTDB) rescue nil
@streamer = CouchRest::Streamer.new(@db)
@docs = (1..1000).collect{|i| {:integer => i, :string => i.to_s}}
@db.bulk_save(@docs)
end
it "should yield each row in a view" do
count = 0
sum = 0
@streamer.view("_all_docs") do |row|
count += 1
end
count.should == 1000
end
end