class CryptoMarket::Currencies

Crypto Currencies is having many Crypto Coins

Attributes

coins[RW]

Public Class Methods

new() click to toggle source

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

coin_names_arrays() click to toggle source

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
create_coin(coin) click to toggle source

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
create_coins_from_attributes() click to toggle source

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
find_by_number(input) click to toggle source

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
print_coin_names() click to toggle source

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) click to toggle source

Prints out the change for all Coin objects with terminal-table gem

print_sorted_prices() click to toggle source

Prints out the price for all Coin objects with terminal-table gem

run() click to toggle source

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
sort_by_change(input) click to toggle source

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
sort_by_price_usd() click to toggle source

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