Ruby Programming Tutorial - Lesson 43 - EasySteem Ruby Api :) Talking to Steem blockchain

in ruby •  7 years ago  (edited)

Radiator

Is a ruby gem written by @inertia its an API to talk to steemit blockchain via ruby.
We are going to further Easify this radiator Gem in here.. so that we can do something like this

Screenshot from 2018-04-02 03-53-50.png

Which should return us bilal-haider's 10 followers
like so
Screenshot from 2018-04-02 03-54-58.png

Create a Module which makes it easy to use Radiator gem :) <3

Creating a directory called Analyzer

cd to this directory and install radiator and paint gems

gem install radiator
Screenshot from 2018-04-02 01-46-34.png

and also install paint gem, for give our text colors in terminal

gem install paint
Screenshot from 2018-04-02 02-48-01.png

You will see that, after creating an EasySteem module, how easily we can talk to steem blockchain

puts EasySteem.reblog_stream

will start a steam for us, where it will show us who is sharing whom posts at the moment :)
like so
Screenshot from 2018-04-02 03-36-52.png

Lets start by analyzing what operations steem blockchain provides us :p

  • we can post
  • we can vote
  • we can transfer
  • we can claim rewards
  • we can flag
  • we can comment
  • we can watch streams :)

We don't have a lot of methods in this module, but we can extend it in upcoming articles. This way we can create an Easy Api to interact with steem blockchain

Our module so far has only 7 methods in it :p thanks to my laziness
You don't need to understand this code, if you don't want to.. you should be able to use it that's what matters :)

require 'radiator'
require 'json'
require 'paint'

module EasySteem
    def EasySteem.supply
        api = Radiator::Api.new
        api.get_dynamic_global_properties do |properties|
          properties.virtual_supply
        end
    end

    def EasySteem.followers(*options)
        api = Radiator::FollowApi.new
        if options[2] == nil
            _start = 0
        elsif 
            _start = options[2]
        end
        api.get_followers(options[0], _start, 'blog', options[1]) do |followers|
          followers.map(&:follower)
        end
    end

    def EasySteem.voting_stream
        stream = Radiator::Stream.new

        stream.operations(:vote) do |op|
          print "#{op.voter} voted for #{op.author}"
          puts " (weight: #{op.weight / 100.0}%)"
        end
    end

    def EasySteem.comment_stream
        stream = Radiator::Stream.new

        stream.operations(:comment) do |op|
            if op.parent_author != ""
                print "#{op.author} commented on #{op.parent_author}\'s Post. "
                puts "Permlink: #{op.parent_permlink}"
            elsif
                print "#{op.author} commented on a Post. \n"
                puts "Permlink: #{op.permlink}" 
            end
        end
    end
    def EasySteem.follow_stream
        stream = Radiator::Stream.new

        stream.operations(:custom_json) do |op|
            if op.id == "follow"
                _myhash = JSON.parse(op.json)
                if _myhash[0] == "follow"
                    _info = _myhash[1]
                    puts "#{Paint[_info['follower'], :red]} has followed #{Paint[_info['following'], :green]}"
                end
            end
        end
    end

    def EasySteem.reblog_stream
        stream = Radiator::Stream.new

        stream.operations(:custom_json) do |op|
            if op.id == "follow"
                _myhash = JSON.parse(op.json)
                # p _myhash
                if _myhash[0] == "reblog"
                    _info = _myhash[1]
                    puts "#{Paint[_info['account'], :green]} has shared #{Paint[_info['author'], :cyan]}\'s Post"
                end
            end
        end
    end

    def EasySteem.stream
        stream = Radiator::Stream.new
        stream.operations do |op|
            puts op
        end
    end
end

Once you have gems installed an you also have EasySteem module. you are ready to use it like so

require_relative 'easysteem'

puts EasySteem.supply

Which returns virtual Steem supply ..

get reblogs stream

require_relative 'easysteem'

puts EasySteem.reblog_stream

which outputs
Screenshot from 2018-04-02 03-47-57.png

Get followers of a person

require_relative 'easysteem'

## returns bilal-haider's 10 followers
puts EasySteem.followers "bilal-haider", 10

## returns bilal-haider's 10 followers starting from "a-0-a"
puts EasySteem.followers "bilal-haider", 10, "a-0-a"

Screenshot from 2018-04-02 03-51-57.png

Get virtual Steem Supply

require_relative 'easysteem'

puts EasySteem.supply

Screenshot from 2018-04-02 03-59-53.png

Get following stream.. see who is following whom at real time

require_relative 'easysteem'

puts EasySteem.follow_stream

Screenshot from 2018-04-02 04-02-16.png

require_relative 'easysteem'

puts EasySteem.follow_stream
puts EasySteem.supply
puts EasySteem.followers "bilal-haider", 100
puts EasySteem.stream
puts EasySteem.voting_stream
puts EasySteem.comment_stream
puts EasySteem.follow_stream
puts EasySteem.reblog_stream

These are few other Methods which you can call using EasySteem
I hope you liked it. ;)

Screenshot from 2018-04-02 04-03-39.png

Authors get paid when people like you upvote their post.
If you enjoyed what you read here, create your account today and start earning FREE STEEM!
Sort Order:  

Once again brother good stuff. Like I said before, you're helping people so stay at it. Just wanted to stop by and offer words of encouragement and to give you a up vote. stay at it and keep doing such a great job. I hope you had a great day. I'll talk at you later.

Thanks for stopping by :) appreciate that.

Lovely lesson

glad you loved it :)

Yea.....keep booming

Good post 👍👍👍

Thanks :) I am glad you liked it :)

ava getup.png

You got a 1.13% upvote from @getup


Want to promote your posts too? Send at least 0.005 STEEM DOLLAR or STEEM (max 0.025) to @getup with the post link as the memo and receive a upvote! More profits? 100% Payout! Delegate some SteemPower to @getup
1 SP, 5 SP, 10 SP, custom amount

► ► For Resteem to over 1400 follower + Upvote from @getup send 0.026 SBD with the post link as the memo.

U5ds6MjTfaESpQgTn2mRPWnvno7KYVF.gif

Ok @bilal-haider i got captivated by this code and what it can do. I understand just a little about the "if then else statements" I was once familiar years ago. Your sharing got me interested. I might buy me a book and explore further on this particular language. Thanks a bunch and enjoy developing...