Hive Developers logo

Hive Developer Portal

Get Follower And Following List

Get the followers of a user/author & the authors that user is following.

Full, runnable src of Get Follower And Following List can be downloaded as part of: tutorials/ruby (or download just this tutorial: devportal-master-tutorials-ruby-19_get_follower_and_following_list.zip).

This tutorial will take you through the process of requesting either the follower or following list for an account on the blockchain.

Intro

In radiator, we can request follow results using condenser_api.get_following or condenser_api.get_follows methods. These methods take the following arguments:

Also see:

Steps

  1. Configure connection Configuration of radiator to communicate with the Hive blockchain
  2. Input variables Collecting the required inputs via command line arguments
  3. Get followers/following Get the followers or accounts being followed
  4. Display Return the array of results to the console

1. Configure connection

get_follow.rb

In the first few lines we initialize the configured library and packages (libraries are described in Gemfile):

require 'rubygems'
require 'bundler/setup'

Bundler.require

api = Radiator::Api.new

Above, we have radiator pointing to the production network. To specify a different full node, e.g.:

api = Radiator::Api.new(url: 'https://api.hive.blog')

2. Input variables

Capture the arguments from the command line.

type = 'blog' # use 'ignore' to get mutes
account = ARGV[0]
what = ARGV[1] || 'following'
limit = (ARGV[2] || '-1').to_i
result = []
count = -1

3. Get followers/following

Depending on the arguments passed, we call the corresponding method and the element name of what we are requesting:

method = "get_#{what}"
elem = what.sub(/s/, '').to_sym

The name of the elem value stored corresponds with the result elements we’re interested in. For method calls on get_following, we want the following elements. For method calls on get_followers, we want follower elements.

4. Display

Iterate multiple calls to capture all of the results.

until count >= result.size
  count = result.size
  response = api.send(method, account, result.last, type, [limit, 1000].max)
  abort response.error.message if !!response.error
  result += response.result.map(&elem)
  result = result.uniq
end

Final code:

require 'rubygems'
require 'bundler/setup'

Bundler.require

api = Radiator::Api.new

if ARGV.size < 1
  puts "Usage:"
  puts "ruby #{__FILE__} <account> [following|followers] [limit]"
  exit
end

type = 'blog' # use 'ignore' to get mutes
account = ARGV[0]
what = ARGV[1] || 'following'
limit = (ARGV[2] || '-1').to_i
result = []
count = -1

method = "get_#{what}"
elem = what.sub(/s/, '').to_sym

until count >= result.size
  count = result.size
  response = api.send(method, account, result.last, type, [limit, 1000].max)
  abort response.error.message if !!response.error
  result += response.result.map(&elem)
  result = result.uniq
end

puts result[0..limit]
puts "#{account} #{what}: #{result.size}"

To Run

First, set up your workstation using the steps provided in Getting Started. Then you can create and execute the script (or clone from this repository):

git clone https://gitlab.syncad.com/hive/devportal.git
cd devportal/tutorials/ruby/19_get_follower_and_following_list
bundle install
ruby get_follow.rb