Class: Chain::TransactionFeed

Inherits:
ResponseObject show all
Defined in:
lib/chain/transaction_feed.rb

Defined Under Namespace

Classes: ClientModule, Query

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from ResponseObject

#[], #[]=, #to_h, #to_json

Constructor Details

#initialize(raw_attribs, base_conn) ⇒ TransactionFeed

Returns a new instance of TransactionFeed.



30
31
32
33
34
35
36
# File 'lib/chain/transaction_feed.rb', line 30

def initialize(raw_attribs, base_conn)
  super(raw_attribs)

  # The consume/ack cycle should run on its own thread, so make a copy of
  # the base connection so this feed has an exclusive HTTP connection.
  @conn = Connection.new(base_conn.opts)
end

Instance Attribute Details

#afterString (readonly)

Returns:

  • (String)


28
# File 'lib/chain/transaction_feed.rb', line 28

attrib :after

#aliasString (readonly)

User specified, unique identifier.

Returns:

  • (String)


20
# File 'lib/chain/transaction_feed.rb', line 20

attrib :alias

#filterString (readonly)

Returns:

  • (String)


24
# File 'lib/chain/transaction_feed.rb', line 24

attrib :filter

#idString (readonly)

Unique transaction feed identifier.

Returns:

  • (String)


15
# File 'lib/chain/transaction_feed.rb', line 15

attrib :id

Instance Method Details

#ackObject



69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/chain/transaction_feed.rb', line 69

def ack
  raise 'ack must be called at most once per cycle in a consume loop' unless @next_after

  @conn.request(
    'update-transaction-feed',
    id: id,
    after: @next_after,
    previous_after: after,
  )

  self.after = @next_after
  @next_after = nil
end

#consume(timeout: 24*60*60) {|Transaction| ... } ⇒ void

This method returns an undefined value.

Parameters:

  • timeout (Fixnum) (defaults to: 24*60*60)

    value in seconds

Yields:

  • (Transaction)

    block process individual transactions

Yield Parameters:



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/chain/transaction_feed.rb', line 42

def consume(timeout: 24*60*60)
  query = {
    filter: filter,
    after: after,
    timeout: (timeout * 1000).to_i, # milliseconds
    ascending_with_long_poll: true
  }

  longpoll = Connection.new(@conn.opts.merge(read_timeout: timeout))

  loop do
    page = longpoll.request('list-transactions', query)
    query = page['next']

    page['items'].each do |raw_tx|
      tx = Transaction.new(raw_tx)

      # Memoize the cursor value for this transaction in case the user
      # decides to ack. The format of the cursor value is specified in the
      # core/query package.
      @next_after = "#{tx.block_height}:#{tx.position}-#{MAX_BLOCK_HEIGHT}"

      yield tx
    end
  end
end