create account

How to Tail the Golos Blockchain in Ruby by inertia

View this thread on: hive.blogpeakd.comecency.com
· @inertia ·
$0.19
How to Tail the Golos Blockchain in Ruby
Ever want to just watch the blockchain from a terminal, as if you're tailing a log file?  Here's how to do that in ruby.

As always, we use [Radiator](https://steemit.com/steem/@inertia/radiator-steem-ruby-api-client) with `bundler`.  You can get `bundler` with this command:

```bash
$ gem install bundler
```

I've tested it on various versions of ruby.  The oldest one I got it to work was:

`ruby 2.0.0p645 (2015-04-13 revision 50299) [x86_64-darwin14.4.0]`

First, make a project folder:

```bash
$ mkdir radiator
$ cd radiator
```

Create a file named `Gemfile` containing:

```ruby
source 'https://rubygems.org'
gem 'radiator', github: 'inertia186/radiator'
gem 'awesome_print'
```

Then run the command:

```bash
$ bundle install
```

Create a file named `tail_blockchain.rb` containing:

```ruby
require 'rubygems'
require 'bundler/setup'

Bundler.require

stream = Radiator::Stream.new(url: 'http://golos.steem.ws/')
last_op = nil
pattern = ARGV.first

stream.operations do |operation|
  if !!pattern
    next unless operation.inspect =~ /#{pattern}/i
  end
  op_key = operation.keys.first
  op_value = operation.values.first
  log = []

  case op_key
  when :comment
    (v = op_value.title).empty? or log << "title: #{v}"
    (v = op_value.parent_author).empty? or log << "parent_author: #{v}"
    (v = op_value.parent_permlink).empty? or log << "parent_permlink: #{v}"
    log << "author: #{op_value.author}"
    log << "permlink: #{op_value.permlink}"
  when :vote
    log << "weight: #{op_value.weight / 100} %"
    log << "voter: #{op_value.voter}"
    log << "author: #{op_value.author}, permlink: #{op_value.permlink}"
  when :custom_json
    log << "id: #{op_value.id}"
    log << "json: #{op_value.json}"
  when :transfer
    log << "from: #{op_value.from}"
    log << "to: #{op_value.to}"
    log << "amount: #{op_value.amount}"
    (v = op_value.memo).empty? or log << "memo: #{v}"
  else log << op_value.to_json
  end

  ap op_key if last_op != op_key
  ap log.join(', ')
  last_op = op_key
end
```

Then run it:

```bash
$ ruby tail_blockchain.rb
```

You can also pass along a regex pattern to match on:

```bash
$ ruby tail_blockchain.rb ned
```

Note, the `comment` operation (which also represents posts) does not output the body.  If the operation, including the body, contains the optional regex, it will display that line without the body so it doesn't flood the display.

![ruby](https://www.steemimg.com/images/2016/08/24/1024px-Ruby_logo.svgdcc20.png)

See my previous Ruby How To posts in: [#radiator](https://golos.io/created/radiator) [#ruby](https://golos.io/created/ruby)
👍  , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , and 8 others
properties (23)
authorinertia
permlinkhow-to-tail-the-golos-blockchain-in-ruby
categoryradiator
json_metadata"{"tags": ["ruby", "golos", "howto"], "permlink": "how-to-tail-the-golos-blockchain-in-ruby"}"
created2016-10-24 17:58:24
last_update2016-10-24 17:58:24
depth0
children0
last_payout2016-11-24 18:38:21
cashout_time1969-12-31 23:59:59
total_payout_value0.152 HBD
curator_payout_value0.039 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length2,610
author_reputation346,568,901,399,561
root_title"How to Tail the Golos Blockchain in Ruby"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id1,610,860
net_rshares2,438,896,757,456
author_curate_reward""
vote details (72)