Hive Developers logo

Hive Developer Portal

Submit Post

This example will broadcast a new post to the blockchain using the values provided.

Full, runnable src of Submit Post can be downloaded as part of: tutorials/ruby (or download just this tutorial: devportal-master-tutorials-ruby-10_submit_post.zip).

A post is differentiated from a comment by whether or not a parent_author exists. When there is no parent_author, then it’s a post, otherwise it’s a comment.

Also see:

Script

submit_a_new_post.rb

You should change wif to the posting key that matches your author. This script will pass along the values as a comment operation:

Final code:

require 'rubygems'
require 'bundler/setup'

Bundler.require

options = {
  url: 'https://testnet.openhive.network',
  wif: '5JrvPrQeBBvCRdjv29iDvkwn3EQYZ9jqfAHzrCyUvfbEbRkrYFC'
}
tx = Radiator::Transaction.new(options)

tags = %w(tag1 tag2 tag3)
metadata = {
  tags: tags
}

tx.operations << {
  type: :comment,
  author: 'social',
  permlink: 'test-post',
  parent_author: '',
  parent_permlink: tags[0],
  title: 'Test Post',
  body: 'Body',
  json_metadata: metadata.to_json
}

response = tx.process(true)

if !!response.error
  puts response.error.message
else
  puts JSON.pretty_generate response
end

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):

You can launch a local testnet, with port 8090 mapped locally to the docker container:

docker run -d -p 8090:8090 inertia/tintoy:latest

For details on running a local testnet, see: Setting Up a Testnet

git clone https://gitlab.syncad.com/hive/devportal.git
cd devportal/tutorials/ruby/10_submit_post
bundle install
ruby submit_a_new_post.rb

Example Output

{
  "jsonrpc": "2.0",
  "result": {
    "id": "768f7f64cee94413da0017ef79f592bb4da86baf",
    "block_num": 22867626,
    "trx_num": 43,
    "expired": false
  },
  "id": 1
}

The response we get after broadcasting the transaction gives us the transaction id (768f7f6...), block number (22867626), and the transaction number of that block (43).

Error Handling

We’re checking the result for error in case the remote node has an issue to raise. Normally, it will be nil, but if it’s populated, output error.message and exit.