Class: CryptoTracker::Crypto
- Inherits:
-
Object
- Object
- CryptoTracker::Crypto
- Defined in:
- lib/crypto_tracker/crypto.rb
Instance Attribute Summary collapse
-
#coin ⇒ Object
readonly
Returns the value of attribute coin.
-
#currency ⇒ Object
readonly
Returns the value of attribute currency.
Instance Method Summary collapse
- #current_price ⇒ Object
-
#initialize(coin, currency) ⇒ Crypto
constructor
A new instance of Crypto.
- #price_history(days) ⇒ Object
Constructor Details
#initialize(coin, currency) ⇒ Crypto
Returns a new instance of Crypto.
7 8 9 10 11 12 |
# File 'lib/crypto_tracker/crypto.rb', line 7 def initialize(coin, currency) @coin = coin.downcase @currency = currency.downcase validate_inputs end |
Instance Attribute Details
#coin ⇒ Object (readonly)
Returns the value of attribute coin.
5 6 7 |
# File 'lib/crypto_tracker/crypto.rb', line 5 def coin @coin end |
#currency ⇒ Object (readonly)
Returns the value of attribute currency.
5 6 7 |
# File 'lib/crypto_tracker/crypto.rb', line 5 def currency @currency end |
Instance Method Details
#current_price ⇒ Object
14 15 16 17 18 19 20 21 |
# File 'lib/crypto_tracker/crypto.rb', line 14 def current_price response = Client.get("coins/#{coin}") price = response.dig('market_data', 'current_price', currency) return { error: 'Preço não disponível' } unless price { coin: coin, price: price, currency: currency, timestamp: Time.now } end |
#price_history(days) ⇒ Object
23 24 25 26 27 28 29 30 31 |
# File 'lib/crypto_tracker/crypto.rb', line 23 def price_history(days) response = Client.get("coins/#{coin}/market_chart", { vs_currency: currency, days: days }) return { error: 'Histórico não disponível' } unless response['prices'] response['prices'].map do |price_data| { timestamp: Time.at(price_data[0] / 1000), price: price_data[1] } end end |