How to post directly to steemit using rudy

in #steem7 years ago

You can post DIRECTLY to the Steem blockchain using Ruby in just a few lines of code!
2018-03-23 09.31.23.png
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={})
@options = opts
end

def post!
raise "Post is missing title, body, or tags" unless is_valid_post?
account.post!(post_options)
end

def post_options
{
title: @options[:title],
body: @options[:body],
tags: @options[:tags],
percent_steem_dollars: percent_steem_dollars,
max_accepted_payout: max_accepted_payout,
allow_votes: allow_votes,
allow_curation_rewards: allow_curation_rewards
#self_vote: self_vote
}
end

def account
@account ||= Steem.new(account_name: ENV['ACCOUNT_NAME'], wif: ENV['WIF'])
end

private

def percent_steem_dollars
@options.fetch(:percent_steem_dollars, 10000)
end

def max_accepted_payout
@options.fetch(:max_accepted_payout, "1000000.000 SBD")
end

def allow_votes
@options.fetch(:allow_votes, true)
end

def allow_curation_rewards
@options.fetch(:allow_curation_rewards, true)
end

def self_vote
@options.fetch(:self_vote, true)
end

def is_valid_post?
[:title, :body, :tags].all? { |opt| !!@options[opt].blank? }
end
end
How it works
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
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.
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:

def account
@account ||= Steem.new(account_name: ENV['ACCOUNT_NAME'], wif: ENV['WIF'])
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.

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

Sort:  

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

Upvote this notification to help all Steemit users. Learn why here!

Coin Marketplace

STEEM 0.19
TRX 0.15
JST 0.029
BTC 63843.45
ETH 2624.02
USDT 1.00
SBD 2.76