Parent

Files

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|
    first = f.gets # discard header
    while line = f.gets 
      row = parse_line(line)
      block.call row unless row.nil? # last line "}]" discarded
      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 52
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 45
def parse_line line
  return nil unless line
  if /(\{.*\}),?/.match(line.chomp)
    MultiJson.decode($1)
  end
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.