class CryptoMarket::Currencies
Crypto Currencies
is having many Crypto Coins
Attributes
Public Class Methods
Store all the currencies in an Array
# File lib/crypto_market/currencies.rb, line 7 def initialize @api_data = CryptoMarket::Api.fetch_coin_data @coins = [] end
Public Instance Methods
Returns smaller Arrays with coin names and indexes
# File lib/crypto_market/currencies.rb, line 84 def coin_names_arrays coins.map.with_index do |coin, index| "#{index + 1} #{coin.name}" end.each_slice(coins.size / 6) end
Instantiates a new Coin based on hash that gets passed in and set it's attributes
# File lib/crypto_market/currencies.rb, line 18 def create_coin(coin) name = coin['name'] price_usd = coin['price_usd'] price_btc = coin['price_btc'] market_cap_usd = coin['market_cap_usd'] percent_change_24h = coin['percent_change_24h'] last_updated_unix = coin['last_updated'] CryptoMarket::Coin.new(name, price_usd, price_btc, market_cap_usd, percent_change_24h, last_updated_unix).tap do |new_coin| coins << new_coin end end
Call the create_coin
method for each Coin hash
# File lib/crypto_market/currencies.rb, line 32 def create_coins_from_attributes @api_data.each do |coin| create_coin(coin) end end
Returns an instance of Coin for the specific Coin that user is looking for
# File lib/crypto_market/currencies.rb, line 39 def find_by_number(input) coins[input - 1] end
To be able to print out coin names in batches In order for next/break to work I need to keep interactions in this method
# File lib/crypto_market/currencies.rb, line 92 def print_coin_names coin_names_arrays.each do |coin| coin.each do |name| index = name.slice!(/\d+\s/).strip table = terminal_table do |t| t.add_row [index, name] t.style = { all_separators: true, width: 60 } end puts table end table = terminal_table do |t| t.title = 'Select a number' t.add_row [1, 'To load more coins'] t.add_row [2, 'To select a coin'] t.style = { all_separators: true, width: 60 } end puts table input = nil until input == 1 || input == 2 input = gets.strip.to_i puts 'Enter a correct number from the list' unless input == 1 || input == 2 end if input == 1 next elsif input == 2 break end end end
Prints out the change for all Coin objects with terminal-table gem
# File lib/crypto_market/currencies.rb, line 73 def print_sorted_changes(input = nil) sort_by_change(input).each do |coin| table = terminal_table do |t| t.add_row [coin.name, "#{coin.percent_change_24h}%"] t.style = { all_separators: true, width: 60 } end puts table end end
Prints out the price for all Coin objects with terminal-table gem
# File lib/crypto_market/currencies.rb, line 60 def print_sorted_prices sort_by_price_usd.each do |coin| unless coin.price_usd == 0 table = terminal_table do |t| t.add_row [coin.name, "$#{coin.price_usd}"] t.style = { all_separators: true, width: 60 } end puts table end end end
Instantiates new coins from Coin class with attributes specified from coin_attribues method
# File lib/crypto_market/currencies.rb, line 13 def run create_coins_from_attributes end
Return sorted Array based on user input (positive, negative or default)
# File lib/crypto_market/currencies.rb, line 49 def sort_by_change(input) if input == 'positive' coins.select { |coin| coin.percent_change_24h.to_f > 0 }.sort_by { |coin| coin.percent_change_24h.to_f }.reverse elsif input == 'negative' coins.select { |coin| coin.percent_change_24h.to_f < 0 }.sort_by { |coin| coin.percent_change_24h.to_f } else coins.sort_by { |coin| coin.percent_change_24h.to_f }.reverse end end
Ranks the coins from based on their USD price
# File lib/crypto_market/currencies.rb, line 44 def sort_by_price_usd coins.sort_by { |coin| coin.price_usd }.reverse end