You can post DIRECTLY to the Steem blockchain using Ruby in just a few lines of code!
You can post DIRECTLY to the Steem blockchain using Ruby in just a few lines of code!
More than five tags are now possible
I was planning on posting part 3 of my Steem API Basics series, but I got so damn excited writing this code that I had to share it right away!
A recent project with @steemsmarter required me to automate the posting of our daily reports. Using the BADASS code created by https://steemit.com/@inertia with his awesome Radiator gem for Ruby, I came up with the following simple class to make it easy for anyone's Ruby app to post directly to the Steem blockchain itself. (view the gist here)
require 'radiator'
class SteemPostBroadcaster
attr_accessor :options
def initialize(opts={})
end
@options = opts
def post!
raise "Post is missing title, body, or tags" unless is_valid_post?
account.post!(post_options)
end
def post_options
{
percent_steem_dollars: percent_steem_dollars,
max_accepted_payout: max_accepted_payout,
allow_votes: allow_votes,
allow_curation_rewards: allow_curation_rewards
}
end
title: @options[:title],
body: @options[:body],
tags: @options[:tags],
#self_vote: self_vote
def account
end
@account ||= Steem.new(account_name: ENV['ACCOUNT_NAME'], wif: ENV['WIF'])
private
def percent_steem_dollars
end
@options.fetch(:percent_steem_dollars, 10000)
def max_accepted_payout
end
@options.fetch(:max_accepted_payout, "1000000.000 SBD")
def allow_votes
end
@options.fetch(:allow_votes, true)
def allow_curation_rewards
end
@options.fetch(:allow_curation_rewards, true)
def self_vote
end
@options.fetch(:self_vote, true)
def is_valid_post?
end
end
How it works
[:title, :body, :tags].all? { |opt| !!@options[opt].blank? }
The Radiator gem by @inertia has some serious functionality baked in for easily interacting with the Steem blockchain! Let's take a look at how to use the Radiator::Chain class to publish posts.
Set up your ENV variables
You need to have the following two variables defined in your .env file or in your remote providers's configuration settings (aka Heroku Settings -> Config Variables):
ACCOUNT_NAME: name of Steem account you will be posting under
Connecting your account to the Steem blockchain
SteemPostBroadcaster uses the Radiator::Chain class (source code) to set up a up a Steem object for a given account:
WIF: Steem account POSTING permission private key (NOT your Owner key!) -- see this great article by @rgeddes about Getting Your Posting Key for more details.
def account
end
Now we can use account to directly interact with the Steem blockchain! Steem objects use Radiator::Chain to post, vote, comment, and even transfer funds on the blockchain. For this post, we're focusing on the .post! method.
@account ||= Steem.new(account_name: ENV['ACCOUNT_NAME'], wif: ENV['WIF'])
Create your post options hash
The most basic post options hash is very easy to build. SteemPostBroadcaster will throw an error if these three parameters are not defined:
opts = {
title: "My Post Title",
body: "My Post Body",
tags: ["tag-1","tag-2"]
}
Here's a further breakdown of these variables:
title (string)
The title of the post. The title is automatically dasherized as the post permlink.
body (string)
The full markdown and/or HTML body of the post.
tags (array of strings)
Array of tags for the post. The first tag will automatically be used as the category.
Post away!
All we have left to do now is feed our options hash opts into a new instance of SteemPostBroadcaster and tell it to post!
THIS IS THE MAGIC PIECE OF CODE
SteemPostBroadcaster.new(opts).post!
And that's it! Our post will almost instantly be pushed to the Steem blockchain! If it works, you will get a response with information on the transaction block in which your post was published, eg:
<Hashie::Mash id=1 result=#<Hashie::Mash block_num=20907858 expired=false id="65d4a6709f6d4c69fd0cbdf9a6fbdcf43a
Congratulations @bustami1983! You have completed some achievement on Steemit and have been rewarded with new badge(s) :
Award for the number of posts published
Click on any badge to view your own Board of Honor on SteemitBoard.
For more information about SteemitBoard, click here
If you no longer want to receive notifications, reply to this comment with the word
STOP