lib/utils.rb in SimBot-0.1.14 vs lib/utils.rb in SimBot-0.1.15
- old
+ new
@@ -1,6 +1,8 @@
+# General use utilities for SimBot
class Utils
+ require 'spreadsheet'
# accept market paramter and return the coinmarket cap quote for said market
# @param [String] market
# @return [FalseClass and Float]
def self.quote(market)
case market
@@ -14,21 +16,31 @@
eoseth_price
when 'btcltc'
btcltc_price
when 'ltcbtc'
ltcbtc_price
- when 'bchxrp'
- bchxrp_price
when 'ltcbch'
ltcbch_price
when 'ltcxrp'
ltcxrp_price
+ when 'btcusd'
+ btcusd_price
+ when 'btcbch'
+ 1/bchbtc_price
else
false
end
end
+ # fetch btc/usd price from coin market cap
+ # @return [float] rate
+ 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
+
# fetch ltc/xrp price from coin market cap
# @return [float] rate
def self.ltcxrp_price
response = RestClient.get('https://api.coinmarketcap.com/v2/ticker/2/?convert=XRP')
hash = JSON.parse(response.body)
@@ -41,18 +53,10 @@
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
- # fetch bch/xrp price from coin market cap
- # @return [float] rate
- 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
-
# fetch ltc/btc price from coin market cap
# @return [float] rate
def self.ltcbtc_price
response = RestClient.get('https://api.coinmarketcap.com/v2/ticker/2/?convert=BTC')
hash = JSON.parse(response.body)
@@ -121,8 +125,43 @@
$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
+
+ # @return [Array] cmc data
+ 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
+
+ # @param [Array] data
+ # @param [String] name
+ # @return [NilClass]
+ 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
end