The gem can be found on Rubygems.org or on Github.com. Check out the examples folder for more examples on how this project could be used.
Installing the gems
gem install httparty
gem install CryptoPriceFinder
This gem uses the Ruby gem “httparty” to make the request to the API. “gem install CryptoPriceFinder” will install the gem on your system.
Saving the price in a text file.
require 'CryptoPriceFinder'
time = Time.new
time_save = time.strftime("%m/%d/%Y\s%I:%M%p")
monero = CryptoPriceFinder::monero(6.1)
File.open("save.txt", 'a') { |file| file.write("#{time_save}\s#{monero}\n") }
# you could create a cron job that runs every day
# at a certain time to track the changes of your crypto.
This will save the price of Monero in a text file named save.txt. You could create a cronjob to run this script at a certain time to see what the average price is.
Find Hodl-ing Percentage
require 'CryptoPriceFinder'
xmr = 6.6
usd_xmr = CryptoPriceFinder::monero(xmr)
btc = 0.103
usd_btc = CryptoPriceFinder::bitcoin(btc)
xlm = 6014
usd_xlm = CryptoPriceFinder::stellar(xlm)
total = usd_xmr + usd_btc + usd_xlm
xlm_percent = usd_xlm.to_f / total * 100.0
xmr_percent = usd_xmr.to_f / total * 100.0
btc_percent = usd_btc.to_f / total * 100.0
puts "XLM Hodling:\s#{xlm_percent.round(3)}%"
puts "XMR Hodling:\s#{xmr_percent.round(3)}%"
puts "BTC Hodling:\s#{btc_percent.round(3)}%"
This script will get the prices of XLM, XMR and BTC, add the values up and determine the percentage between all the different cryptocurrencies.
Parsing the text file.
total = 0
count = 0
File.readlines("save.txt").each do |line|
price = line.split("\s")[2]
total += price.to_i
count += 1
end
puts "Count:\s#{count}\n"
puts "total:\s#{total}\n"
new_avg = total / count.round(4)
# adds comma to the numbers.
puts "AVG:\s#{new_avg.to_s.gsub(/\B(?=(...)*\b)/, ',')}"
This script will parse the “save the price” script and perform averaging calculations based on information in the “save.txt”. In one of the examples in the “examples” folder includes a file with the name “crontab.rb”. This file gives instructions on how to set up a cron job to automatically run the script to save the current price in a file. I have it set to run every day at 1:00 PM. But this could be changed easily. Cronjobs only work for Linux based systems.
Check current price with past price.
require 'CryptoPriceFinder'
# gets the last line of save.txt
check = File.readlines("save.txt").last
date = check.split("\s")[0]
time = check.split("\s")[1]
price = check.split("\s")[2]
xmr = 6.6
usd_xmr = CryptoPriceFinder::monero(xmr)
message = "
The price is greater than the price on #{date} at #{time}
The price on #{date} is #{price}
The current price is: #{usd_xmr}
"
if usd_xmr.to_i >= price.to_i
puts message
else
puts "The price is not greater :("
puts "Past Price: #{price}"
puts "Current Price: #{xmr_price}"
end
This example will get the current price of Monero and read and parse the “save.txt” file which contains data about past price. The code will determine if the current price is greater or equal to the past price. If the current price is equal or greater than the past price it will print out information.
Add comma’s and round up the price
require 'CryptoPriceFinder'
# get the price and round by 2
xmr = CryptoPriceFinder::monero(6.7).round(2)
# add a comma to the price "xmr" variable
# needs to be a string
puts xmr.to_s.comma
The “comma” method will add commas to the price of the cryptocurrency but the price has to be a string. If you use the “.round(2)” method, the code will round up the price of the cryptocurrency by two decimals.
Leave a Reply