class CTFC::Data

Data class keep all the logic to send request, receive response, and everything between. Class Ctfc extend CTFC::Data, for easier work.

@note Instead of using CTFC::Data.new, you can also call Ctfc.new

Attributes

coins[RW]
count[R]
currency[RW]
data[R]
fiat[RW]
prices[R]
response[R]
table[R]
url[R]

Public Class Methods

new(currency = :eur, opts = {}) click to toggle source

@example Initialization example

@data = CTFC::Data.new :eur, save: true

@param [Symbol] currency Optional. Define fiat currency. @param [Hash] opts Optional. Additional options hash.

@option opts [Boolean] print Optional. Print terminal output. @option opts [Boolean] save Optional. Save ‘.csv` output. @option opts [Array] coins Optional. Define coins to scrap.

@return [Data] Data object to work with

# File lib/ctfc/base.rb, line 47
def initialize(currency = :eur, opts = {})
  @fiat  = currency.to_s.upcase
  @save  = opts[:save].nil?  ? true : opts[:save]
  @print = opts[:print].nil? ? true : opts[:print]
  @coins = opts[:coins].nil? ? COINS : Array(opts[:coins])
end

Public Instance Methods

get(currency = nil, opts = {}) click to toggle source

@example Get fiat prices for initialized config

@data.get

@example Get prices and change initialized config “on-the-fly”

@data.get :usd, save: false, coins: %w[BTC XMR ETH]

@param [Symbol || String] currency Optional. Change fiat currency and execute request. @param [Hash] opts Optional. Options hash to change config ‘on-the-fly’ - see initialize.

@return [Hash || false] Hash of coins and fiat values, or false if all requests fail

# File lib/ctfc/base.rb, line 68
def get(currency = nil, opts = {})
  @fiat  = currency.to_s.upcase unless currency.nil?
  @coins = opts[:coins]  unless opts[:coins].nil?
  @save  = opts[:save]   unless opts[:save].nil?
  @print = opts[:print]  unless opts[:print].nil?
  @count = 0
  @table = "ctfc_#{@fiat}.csv".downcase
  do_rest_request
end
price(coin) click to toggle source

Get fiat value from response hash with crypto prices

@example

@data.price(:btc)

@param [Symbol || String] coin Required. Coin name as symbol or string. @return [Float]

# File lib/ctfc/base.rb, line 88
def price(coin)
  @prices[coin.to_s.upcase]
end
print=(opt) click to toggle source

Change option to print prices in terminal

@return [true || false]

print?() click to toggle source

Check if crypto prices will be printed in terminal

@return [true || false]

save=(opt) click to toggle source

Change option to save ‘.csv’ table with prices

@return [true || false]

# File lib/ctfc/base.rb, line 115
def save=(opt)
  @save = opt.is_a?(TrueClass)
end
save?() click to toggle source

Check if crypto prices will be saved in ‘.csv` table

@return [true || false]

# File lib/ctfc/base.rb, line 97
def save?
  @save == true
end
success?() click to toggle source

Check if request was successful or not.

@return [true || false]

# File lib/ctfc/base.rb, line 133
def success?
  return false if @response.nil?

  @response.code == 200
end

Private Instance Methods

create_csv_headers() click to toggle source
# File lib/ctfc/base.rb, line 198
def create_csv_headers
  header_array = ['TIME']
  @coins.each { |coin| header_array << coin }
  CSV.open(@table, 'w') { |header| header << header_array }
end
do_rest_request() click to toggle source
# File lib/ctfc/base.rb, line 141
def do_rest_request
  prepare_uri
  process_data
  @prices
rescue StandardError
  if (@count += 1) >= MAX_RETRY
    puts @response.to_s.split(',')
    false
  else
    do_rest_request
  end
end
prepare_uri() click to toggle source
# File lib/ctfc/base.rb, line 169
def prepare_uri
  @prices = {}
  @data_array = []
  coin_uri = String.new ''
  @coins.collect { |coin| coin_uri << "fsyms=#{coin}&" }
  @url = URL + "#{coin_uri}tsyms=#{@fiat}"
end
print_fiat_values() click to toggle source
process_data() click to toggle source
# File lib/ctfc/base.rb, line 154
def process_data
  @response = RestClient.get @url
  @data = JSON.parse @response

  @data_array << Time.now.to_s
  @coins.each do |coin|
    value = @data['RAW'][coin.to_s.upcase][@fiat.to_s.upcase]['PRICE'].round(2)
    @prices[coin] = value
    @data_array << value
  end

  print_fiat_values
  save_csv_data
end
save_csv_data() click to toggle source
# File lib/ctfc/base.rb, line 191
def save_csv_data
  return unless save?

  create_csv_headers unless File.exist?(@table)
  CSV.open(@table, 'ab') { |column| column << @data_array }
end