class CouchRest::Streamer
Attributes
default_curl_opts[RW]
Public Class Methods
new()
click to toggle source
# File lib/couchrest/helper/streamer.rb, line 6 def initialize self.default_curl_opts = "--silent --no-buffer --tcp-nodelay -H \"Content-Type: application/json\"" end
Public Instance Methods
get(url, &block)
click to toggle source
# File lib/couchrest/helper/streamer.rb, line 14 def get(url, &block) open_pipe("curl #{default_curl_opts} \"#{url}\"", &block) end
post(url, params = {}, &block)
click to toggle source
# File lib/couchrest/helper/streamer.rb, line 18 def post(url, params = {}, &block) open_pipe("curl #{default_curl_opts} -d \"#{escape_quotes(MultiJson.encode(params))}\" \"#{url}\"", &block) end
view(*args)
click to toggle source
# File lib/couchrest/helper/streamer.rb, line 10 def view(*args) raise "CouchRest::Streamer#view is deprecated. Please use Database#view with block." end
Protected Instance Methods
escape_quotes(data)
click to toggle source
# File lib/couchrest/helper/streamer.rb, line 24 def escape_quotes(data) data.gsub(/"/, '\"') end
open_pipe(cmd, &block)
click to toggle source
# File lib/couchrest/helper/streamer.rb, line 28 def open_pipe(cmd, &block) first = nil prev = nil IO.popen(cmd) do |f| while line = f.gets row = parse_line(line) if row.nil? first ||= line # save the header for later if we can't parse it. else block.call row end prev = line end end raise RestClient::ServerBrokeConnection if $? && $?.exitstatus != 0 parse_first(first, prev) end
parse_first(first, last)
click to toggle source
# File lib/couchrest/helper/streamer.rb, line 55 def parse_first first, last return nil unless first header = MultiJson.decode(last ? first + last : first) header.delete("rows") header end
parse_line(line)
click to toggle source
# File lib/couchrest/helper/streamer.rb, line 48 def parse_line line return nil unless line if /(\{.*\}),?/.match(line.chomp) MultiJson.decode($1) end end