summaryrefslogtreecommitdiff
path: root/lib/tapicero/couch_changes.rb
blob: abb451d0ec81ca052bf09f153aab8aa94c3ccc2f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
module Tapicero
  class CouchChanges
    def initialize(stream)
      @stream = stream
    end

    def created(hash = {}, &block)
      if block_given?
        @created = block
      else
        @created && @created.call(hash)
      end
    end

    def last_seq
      @stream.get "_changes", :limit => 1, :descending => true do |hash|
        return hash[:last_seq]
      end
    end

    def listen
      @stream.get "_changes", :feed => :continuous, :since => last_seq do |hash|
        callbacks(hash)
      end
    end

    def callbacks(hash)
      #changed
      return if hash[:deleted]
      return unless changes = hash[:changes]
      return created(hash) if changes[0][:rev].start_with?('1-')
      #updated
    end
  end
end