class BiilabsClient

Attributes

config[R]

Public Class Methods

new() click to toggle source
# File lib/biilabs-client.rb, line 7
def initialize
  config_path = 'config/biilabs-client.yml'
  env = ENV['RAILS_ENV'] || 'development'
  string = File.open(config_path, 'rb') { |f| f.read }
  fail "#{config_path} not existed nor not readable" if string.nil?
  @config = YAML.load(string)[env]
  fail "#{config_path} incorrect or environment not exist" if @config.nil?
end

Public Instance Methods

get_tangle(tangle_id) click to toggle source
# File lib/biilabs-client.rb, line 30
def get_tangle(tangle_id)
  path = "/transaction/#{tangle_id}"
  resp = connection.get(path)
  response_handler(resp)
end
get_tangle_by_tag(tag) click to toggle source
# File lib/biilabs-client.rb, line 36
def get_tangle_by_tag(tag)
  tag_id = (tag.to_trytes.value + "9" * 27)[0..26]
  path = "/tag/#{tag_id}"
  resp = connection.get(path)
  response_handler(resp)
end
post_tangle(tag, message) click to toggle source
# File lib/biilabs-client.rb, line 16
def post_tangle(tag, message)
  path = '/transaction'
  body = {
    tag: tag.to_trytes.value,
    message: message
  }
  resp = connection.post do |req|
    req.url path
    req.headers['Content-Type'] = 'application/json'
    req.body = body.to_json
  end
  response_handler(resp)
end

Private Instance Methods

connection() click to toggle source
# File lib/biilabs-client.rb, line 45
def connection
  @connection ||= Faraday.new(url: config['endpoint']) do |faraday|
    faraday.request  :url_encoded             # form-encode POST params
    # faraday.response :logger                  # log requests and responses to $stdout
    faraday.adapter  Faraday.default_adapter  # make requests with Net::HTTP
  end
end
response_handler(resp) click to toggle source
# File lib/biilabs-client.rb, line 53
def response_handler(resp)
  if resp.status == 200
    JSON.load(resp.body)
  else
    { status: resp.status, body: resp.body }
  end
end