Class: Utils
- Inherits:
-
Object
- Object
- Utils
- Defined in:
- lib/utils.rb
Overview
General use utilities for SimBot
Class Method Summary collapse
-
.bchbtc_price ⇒ float
fetch bch/btc price from coin market cap.
-
.bchxrp_price ⇒ float
fetch bch/xrp price from coin market cap.
-
.binane_order_book ⇒ float
fetch binance order book.
-
.binane_usdtbtc_order_book ⇒ float
fetch binance order book.
-
.btcltc_price ⇒ float
fetch btc/ltc price from coin market cap.
-
.btcusd_price ⇒ float
fetch btc/usd price from coin market cap.
-
.btcxrp_price ⇒ float
fetch btc/xrp price from coin market cap.
-
.cmc_data ⇒ Array
Cmc data.
-
.eoseth_price ⇒ float
fetch eos/eth price from coin market cap.
- .find_and_replace(file, replce_string, data) ⇒ Object
-
.kill_process(pid) ⇒ FalseClass and TrueClass
Try and read the existing pid from the pid file and signal the process.
-
.ltcbch_price ⇒ float
fetch ltc/bch price from coin market cap.
-
.ltcbtc_price ⇒ float
fetch ltc/btc price from coin market cap.
-
.ltcxrp_price ⇒ float
fetch ltc/xrp price from coin market cap.
-
.quote(market) ⇒ FalseClass and Float
accept market paramter and return the coinmarket cap quote for said market.
- .rand_for_hour(min, max) ⇒ Object
- .read_from_spreadsheet(name) ⇒ Array
- .send_mail(data_file, template_file, email, password) ⇒ Object
- .write_to_spreadsheet(data, name) ⇒ NilClass
Class Method Details
.bchbtc_price ⇒ float
fetch bch/btc price from coin market cap
92 93 94 95 96 |
# File 'lib/utils.rb', line 92 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 ⇒ float
fetch bch/xrp price from coin market cap
84 85 86 87 88 |
# File 'lib/utils.rb', line 84 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 |
.binane_order_book ⇒ float
fetch binance order book
116 117 118 119 120 121 122 |
# File 'lib/utils.rb', line 116 def self.binane_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 |
.binane_usdtbtc_order_book ⇒ float
fetch binance order book
126 127 128 129 130 131 132 |
# File 'lib/utils.rb', line 126 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 ⇒ float
fetch btc/ltc price from coin market cap
76 77 78 79 80 |
# File 'lib/utils.rb', line 76 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 ⇒ float
fetch btc/usd price from coin market cap
36 37 38 39 40 |
# File 'lib/utils.rb', line 36 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 ⇒ float
fetch btc/xrp price from coin market cap
100 101 102 103 104 |
# File 'lib/utils.rb', line 100 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 ⇒ Array
Returns cmc data.
186 187 188 189 190 191 192 193 194 195 196 197 198 199 |
# File 'lib/utils.rb', line 186 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 ⇒ float
fetch eos/eth price from coin market cap
108 109 110 111 112 |
# File 'lib/utils.rb', line 108 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 |
.find_and_replace(file, replce_string, data) ⇒ Object
172 173 174 175 176 177 178 179 180 181 182 183 |
# File 'lib/utils.rb', line 172 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) ⇒ FalseClass and TrueClass
Try and read the existing pid from the pid file and signal the process. Returns true for a non blocking status.
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 |
# File 'lib/utils.rb', line 138 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 ⇒ float
fetch ltc/bch price from coin market cap
52 53 54 55 56 |
# File 'lib/utils.rb', line 52 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 ⇒ float
fetch ltc/btc price from coin market cap
60 61 62 63 64 |
# File 'lib/utils.rb', line 60 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 ⇒ float
fetch ltc/xrp price from coin market cap
44 45 46 47 48 |
# File 'lib/utils.rb', line 44 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 |
.quote(market) ⇒ FalseClass and Float
accept market paramter and return the coinmarket cap quote for said market
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/utils.rb', line 7 def self.quote(market) case market when 'bchxrp' bchxrp_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) ⇒ Object
67 68 69 70 71 72 73 |
# File 'lib/utils.rb', line 67 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) ⇒ Array
222 223 224 225 226 227 228 229 230 231 |
# File 'lib/utils.rb', line 222 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) ⇒ Object
160 161 162 163 164 165 166 167 168 169 170 |
# File 'lib/utils.rb', line 160 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], '[email protected]', 'COLLABORATION', find_and_replace(f, '{var}', row)) puts "Sent mail to #{row[2]}" end end end |
.write_to_spreadsheet(data, name) ⇒ NilClass
204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 |
# File 'lib/utils.rb', line 204 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 |