Class: CryptoMarket::Currencies
- Inherits:
-
Object
- Object
- CryptoMarket::Currencies
- Includes:
- CryptoMarket
- Defined in:
- lib/crypto_market/currencies.rb
Overview
Crypto Currencies is having many Crypto Coins
Constant Summary
Constants included from CryptoMarket
Instance Attribute Summary collapse
-
#coins ⇒ Object
Returns the value of attribute coins.
Instance Method Summary collapse
-
#coin_names_arrays ⇒ Object
Returns smaller Arrays with coin names and indexes.
-
#create_coin(coin) ⇒ Object
Instantiates a new Coin based on hash that gets passed in and set it’s attributes.
-
#create_coins_from_attributes ⇒ Object
Call the create_coin method for each Coin hash.
-
#find_by_number(input) ⇒ Object
Returns an instance of Coin for the specific Coin that user is looking for.
-
#initialize ⇒ Currencies
constructor
Store all the currencies in an Array.
-
#print_coin_names ⇒ Object
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.
-
#print_sorted_changes(input = nil) ⇒ Object
Prints out the change for all Coin objects with terminal-table gem.
-
#print_sorted_prices ⇒ Object
Prints out the price for all Coin objects with terminal-table gem.
-
#run ⇒ Object
Instantiates new coins from Coin class with attributes specified from coin_attribues method.
-
#sort_by_change(input) ⇒ Object
Return sorted Array based on user input (positive, negative or default).
-
#sort_by_price_usd ⇒ Object
Ranks the coins from based on their USD price.
Methods included from CryptoMarket
Constructor Details
#initialize ⇒ Currencies
Store all the currencies in an Array
7 8 9 10 |
# File 'lib/crypto_market/currencies.rb', line 7 def initialize @api_data = CryptoMarket::Api.fetch_coin_data @coins = [] end |
Instance Attribute Details
#coins ⇒ Object
Returns the value of attribute coins.
5 6 7 |
# File 'lib/crypto_market/currencies.rb', line 5 def coins @coins end |
Instance Method Details
#coin_names_arrays ⇒ Object
Returns smaller Arrays with coin names and indexes
84 85 86 87 88 |
# 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 |
#create_coin(coin) ⇒ Object
Instantiates a new Coin based on hash that gets passed in and set it’s attributes
18 19 20 21 22 23 24 25 26 27 28 29 |
# 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 |
#create_coins_from_attributes ⇒ Object
Call the create_coin method for each Coin hash
32 33 34 35 36 |
# File 'lib/crypto_market/currencies.rb', line 32 def create_coins_from_attributes @api_data.each do |coin| create_coin(coin) end end |
#find_by_number(input) ⇒ Object
Returns an instance of Coin for the specific Coin that user is looking for
39 40 41 |
# File 'lib/crypto_market/currencies.rb', line 39 def find_by_number(input) coins[input - 1] end |
#print_coin_names ⇒ Object
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
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 |
# 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 |
#print_sorted_changes(input = nil) ⇒ Object
Prints out the change for all Coin objects with terminal-table gem
73 74 75 76 77 78 79 80 81 |
# 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 |
#print_sorted_prices ⇒ Object
Prints out the price for all Coin objects with terminal-table gem
60 61 62 63 64 65 66 67 68 69 70 |
# 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 |
#run ⇒ Object
Instantiates new coins from Coin class with attributes specified from coin_attribues method
13 14 15 |
# File 'lib/crypto_market/currencies.rb', line 13 def run create_coins_from_attributes end |
#sort_by_change(input) ⇒ Object
Return sorted Array based on user input (positive, negative or default)
49 50 51 52 53 54 55 56 57 |
# 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 |
#sort_by_price_usd ⇒ Object
Ranks the coins from based on their USD price
44 45 46 |
# File 'lib/crypto_market/currencies.rb', line 44 def sort_by_price_usd coins.sort_by { |coin| coin.price_usd }.reverse end |