Class: CouchBum

Inherits:
Object
  • Object
show all
Defined in:
lib/couch_bum/couch_bum.rb

Overview

A CouchRest wrapper for the changes API.

See: github.com/couchrest/couchrest

Defined Under Namespace

Classes: BingeContext

Instance Method Summary collapse

Constructor Details

#initialize(database:, protocol: 'http', host: 'localhost', port: 5984, username: nil, password: nil) ⇒ CouchBum

Returns a new instance of CouchBum.



13
14
15
16
17
# File 'lib/couch_bum/couch_bum.rb', line 13

def initialize(database:, protocol: 'http', host: 'localhost', port: 5984, username: nil, password: nil)
  @connection_string = make_connection_string(protocol, username, password, host, port, database)

  CouchBum.logger ||= Logger.new(STDOUT)
end

Instance Method Details

#binge_changes(since: 0, limit: nil, include_docs: nil, &block) ⇒ Object

Attaches to the Changes API and streams the updates to passed block.

This is a blocking call that only stops when there are no more changes to pull or is explicitly terminated by calling choke within the passed block.



25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/couch_bum/couch_bum.rb', line 25

def binge_changes(since: 0, limit: nil, include_docs: nil, &block)
  catch(:choke) do
    logger.debug("Binging #{limit} changes from '#{since}'")
    params = stringify_params(limit: limit, include_docs: include_docs)
    params = "since=#{since}&#{params}" unless since.blank?

    changes = couch_rest(:get, "_changes?#{params}")
    context = BingeContext.new(changes)
    changes['results'].each do |change|
      context.current_seq = change['seq']
      context.instance_exec(change, &block)
    end
  end
end

#couch_rest(method, route, *args, **kwargs) ⇒ Object



40
41
42
43
44
45
46
# File 'lib/couch_bum/couch_bum.rb', line 40

def couch_rest(method, route, *args, **kwargs)
  url = expand_route(route)
  CouchRest.send(method, url, *args, **kwargs)
rescue CouchRest::Exception => e
  logger.error("Failed to communicate with CouchDB: Status: #{e.http_code} - #{e.http_body}")
  raise e
end