class Utils

General use utilities for SimBot

Public Class Methods

bchbtc_price() click to toggle source

fetch bch/btc price from coin market cap @return [float] rate

# File lib/utils.rb, line 117
def self.bchbtc_price
  response = RestClient.get('https://api.coinmarketcap.com/v2/ticker/1831/?convert=BTC')
  hash = JSON.parse (response.body)
  hash['data']['quotes']['BTC']['price'].to_f.round(4)
end
bchxrp_price() click to toggle source

fetch bch/xrp price from coin market cap @return [float] rate

# File lib/utils.rb, line 109
def self.bchxrp_price
  response = RestClient.get('https://api.coinmarketcap.com/v2/ticker/1831/?convert=XRP')
  hash = JSON.parse(response.body)
  hash['data']['quotes']['XRP']['price'].to_f.round(4)
end
binance_order_book() click to toggle source

fetch binance order book @return [float] rate

# File lib/utils.rb, line 149
def self.binance_order_book
  response = RestClient.get('https://api.binance.com/api/v1/depth?symbol=BCCBTC&limit=500')
  hash = JSON.parse(response.body)
  bids =  hash['bids']
  asks =  hash['asks']
  { bids: bids, asks: asks }
end
binane_usdtbtc_order_book() click to toggle source

fetch binance order book @return [float] rate

# File lib/utils.rb, line 159
def self.binane_usdtbtc_order_book
  response = RestClient.get('https://api.binance.com/api/v1/depth?symbol=BTCUSDT&limit=500')
  hash = JSON.parse(response.body)
  bids =  hash['bids']
  asks =  hash['asks']
  { bids: bids, asks: asks }
end
btcltc_price() click to toggle source

fetch btc/ltc price from coin market cap @return [float] rate

# File lib/utils.rb, line 101
def self.btcltc_price
  response = RestClient.get('https://api.coinmarketcap.com/v2/ticker/1/?convert=LTC')
  hash = JSON.parse(response.body)
  hash['data']['quotes']['LTC']['price'].to_f.round(4)
end
btcusd_price() click to toggle source

fetch btc/usd price from coin market cap @return [float] rate

# File lib/utils.rb, line 61
def self.btcusd_price
  response = RestClient.get('https://api.coinmarketcap.com/v2/ticker/1/?convert=USD')
  hash = JSON.parse(response.body)
  hash['data']['quotes']['USD']['price'].to_f.round(4)
end
btcxrp_price() click to toggle source

fetch btc/xrp price from coin market cap @return [float] rate

# File lib/utils.rb, line 125
def self.btcxrp_price
  response = RestClient.get('https://api.coinmarketcap.com/v2/ticker/1/?convert=XRP')
  hash = JSON.parse (response.body)
  hash['data']['quotes']['XRP']['price'].to_f.round(4)
end
cmc_data() click to toggle source

@return [Array] cmc data

# File lib/utils.rb, line 219
def self.cmc_data
  data = []
  (0...16).each do |start|
    response = RestClient.get("https://api.coinmarketcap.com/v2/ticker/?start=#{start * 100 + 1}")
    hash = JSON.parse (response.body)
    hash['data'].each do |key, coin|
      row = [coin['name'], coin['symbol'],
             coin['quotes']['USD']['market_cap'].to_f,
             coin['quotes']['USD']['volume_24h'].to_f]
      data.push(row)
    end
  end
  data
end
eoseth_price() click to toggle source

fetch eos/eth price from coin market cap @return [float] rate

# File lib/utils.rb, line 133
def self.eoseth_price
  response = RestClient.get('https://api.coinmarketcap.com/v2/ticker/1765/?convert=ETH')
  hash = JSON.parse (response.body)
  return hash['data']['quotes']['ETH']['price'].to_f.round(4)
end
ethusd_price() click to toggle source

fetch eos/eth price from coin market cap @return [float] rate

# File lib/utils.rb, line 141
def self.ethusd_price
  response = RestClient.get('https://api.coinmarketcap.com/v2/ticker/1027/?convert=USD')
  hash = JSON.parse (response.body)
  return hash['data']['quotes']['USD']['price'].to_f.round(4)
end
find_and_replace(file, replce_string, data) click to toggle source
# File lib/utils.rb, line 205
def self.find_and_replace(file, replce_string, data)
  i = 0
  result = ''
  file.each_line do |line|
    if line.include? "{var}"
      line = line.gsub(replce_string, data[i])
      i += 1
    end
    result += line
  end
  result
end
kill_process(pid) click to toggle source

Try and read the existing pid from the pid file and signal the process. Returns true for a non blocking status. @param [Integer] pid @return [FalseClass and TrueClass]

# File lib/utils.rb, line 171
def self.kill_process(pid)
  opid = File.read("#{pid}.pid").strip.to_i
  Process.kill "HUP", opid
  File.delete("#{pid}.pid")
  File.delete("#{pid}.outfile")
  File.delete("#{pid}.errfile")
  puts "Stopped process #{pid}"
  true
rescue Errno::ENOENT
  $stdout.puts "#{pid} did not exist: Errno::ENOENT"
  true
rescue Errno::ESRCH
  $stdout.puts "The process #{opid} did not exist: Errno::ESRCH"
  true
rescue Errno::EPERM
  $stderr.puts "Lack of privileges to manage the process #{opid}: Errno::EPERM"
  false
rescue ::Exception => e
  $stderr.puts "While signaling the PID, unexpected #{e.class}: #{e}"
  false
end
ltcbch_price() click to toggle source

fetch ltc/bch price from coin market cap @return [float] rate

# File lib/utils.rb, line 77
def self.ltcbch_price
  response = RestClient.get('https://api.coinmarketcap.com/v2/ticker/2/?convert=BCH')
  hash = JSON.parse(response.body)
  hash['data']['quotes']['BCH']['price'].to_f.round(4)
end
ltcbtc_price() click to toggle source

fetch ltc/btc price from coin market cap @return [float] rate

# File lib/utils.rb, line 85
def self.ltcbtc_price
  response = RestClient.get('https://api.coinmarketcap.com/v2/ticker/2/?convert=BTC')
  hash = JSON.parse(response.body)
  hash['data']['quotes']['BTC']['price'].to_f.round(4)
end
ltcxrp_price() click to toggle source

fetch ltc/xrp price from coin market cap @return [float] rate

# File lib/utils.rb, line 69
def self.ltcxrp_price
  response = RestClient.get('https://api.coinmarketcap.com/v2/ticker/2/?convert=XRP')
  hash = JSON.parse(response.body)
  hash['data']['quotes']['XRP']['price'].to_f.round(4)
end
new_sheet(name) click to toggle source
# File lib/utils.rb, line 253
def self.new_sheet(name)
  book = Spreadsheet::Workbook.new
  book.write(name)
  puts "#{name} Created"
end
quote(market) click to toggle source

accept market paramter and return the coinmarket cap quote for said market @param [String] market @return [FalseClass and Float]

# File lib/utils.rb, line 7
def self.quote(market)
  case market
  when 'bchxrp'
    bchxrp_price
  when 'tusdzar'
    tusdzar_price
  when 'xrpzar'
    xrpusd_price*tusdzar_price
  when 'ethzar'
    ethusd_price*tusdzar_price
  when 'btczar'
    btcusd_price*tusdzar_price
  when 'bchbtc'
    bchbtc_price
  when 'btcxrp'
    btcxrp_price
  when 'eoseth'
    eoseth_price
  when 'btcltc'
    btcltc_price
  when 'ltcbtc'
    ltcbtc_price
  when 'ltcbch'
    ltcbch_price
  when 'ltcxrp'
    ltcxrp_price
  when 'btcusd'
    btcusd_price
  when 'btcbch'
    1/bchbtc_price
  else
    false
  end
end
rand_for_hour(min, max) click to toggle source
# File lib/utils.rb, line 92
def self.rand_for_hour(min, max)
  if @prev_time != Time.now.hour
    @rand_val = rand(min...max)
    @prev_time = Time.now.hour
  end
  @rand_val
end
read_from_spreadsheet(name) click to toggle source

@param [String] name @return [Array]

# File lib/utils.rb, line 261
def self.read_from_spreadsheet(name)
  data = []
  book = Spreadsheet.open(name)
  sheet1 = book.worksheet(0) # can use an index or worksheet name
  sheet1.each do |row|
    break if row[0].nil? # if first cell empty
    data.push(row)
  end
  data
end
send_mail(data_file, template_file, email, password) click to toggle source
# File lib/utils.rb, line 193
def self.send_mail(data_file, template_file, email, password)
  m = Mailer.new(email, password)
  data = read_from_spreadsheet(data_file)
  data.delete_at(0)
  data.each do |row|
    File.open(template_file, "r") do |f|
      m.send(row[2], row[3], 'jon@ovex.io', 'COLLABORATION', find_and_replace(f, '{var}', row))
      puts "Sent mail to #{row[2]}"
    end
  end
end
tusdzar_price() click to toggle source

fetch btc/usd price from coin market cap @return [float] rate

# File lib/utils.rb, line 44
def self.tusdzar_price
  response = RestClient.get('https://www.freeforexapi.com/api/live?pairs=USDZAR')
  hash = JSON.parse(response.body)
  hash['rates']['USDZAR']['rate'].to_f.round(2)
end
write_to_spreadsheet(data, name) click to toggle source

@param [Array] data @param [String] name @return [NilClass]

# File lib/utils.rb, line 237
def self.write_to_spreadsheet(data, name)
  book = Spreadsheet::Workbook.new
  sheet = book.create_worksheet(name: 'coin_data')
  rc = 1
  data.each do |row|
    col = 0
    row.each do |cell|
      sheet[rc, col] = cell
      col += 1
    end
    rc += 1
  end
  book.write(name)
  puts "Data written to #{name}"
end
xrpusd_price() click to toggle source

fetch btc/usd price from coin market cap @return [float] rate

# File lib/utils.rb, line 53
def self.xrpusd_price
  response = RestClient.get('https://api.coinmarketcap.com/v2/ticker/52/?convert=USD')
  hash = JSON.parse(response.body)
  hash['data']['quotes']['USD']['price'].to_f.round(4)
end