Ruby Steem tutorial 2 - Get Steem account information

In this tutorial, we will focus on how to get Steem account information via radiator. By using this gem, we can easily get Steem users’ profile, balances, followers etc. If you haven’t setup your development environments, please check my first tutorial.

Get basic account information

First, we need to use Api class defined in radiator:

1
2
3
4
5
6
7
require 'radiator'
user = ’yuxi’
api = Radiator::Api.new
account = api.find_account(user)
puts account.name
puts "Voting power: %.2f" % (account.voting_power/100.to_f)

User’s reputation needs a bit calculation:

1
puts "Reputation: %.2f" % (Math::log10(account.reputation.to_f)*9-56)

Radiator supports alternative nodes in case of the official node becoming unresponsive. Just define a list of alternatives and pass it to Api class as follows:

1
2
3
4
5
6
7
8
options = {
ur: 'https://api.steemit.com',
failover_urls: [
'https://api.steemitstage.com'
]
}
api = Radiator::Api.new(options)

Parse json metadata

In Steem blockchain, user’s profile is stored in json_metadata. To parse json string, we need to install json gem:

1
gem install json

Now we can parse json_metadata

1
2
3
4
5
6
7
8
9
require 'radiator'
require 'json'
api = Radiator::Api.new
j=JSON.parse(account.json_metadata)
puts j["profile"]["about"]
puts j["profile"]["profile_image"]
puts j["profile"]["website"]

Read account balances

With the same account object we have just obtained, STEEM / SBD balances can be accessed:

1
2
puts "SBD: %.2f" % (account.sbd_balance.to_f)
puts "STEEM: %.2f" % (account.balance.to_f)

Get follower / following information

To access user’s follower / following information, we need to use FollowApi class, which makes call to steem API.

1
2
3
4
5
6
7
8
9
10
11
followapi = Radiator::FollowApi.new
response = followapi.get_followers(user, 0, 'blog', 10)
followers = response.result.map(&:follower)
puts 'followers'
puts followers
response = followapi.get_following(user, 0, 'blog', 10)
following = response.result.map(&:following)
puts "\nfollowing"
puts following

Summary

In this tutorial, we use radiator to get Steem user information, including user’s profile, balances, followers/following. There are much more the radiator API can offer, I would leave it to you to explore.

Share