Class: CTFC::Data
Overview
Instead of using CTFC::Data.new, you can also call Ctfc.new
Data class keep all the logic to send request, receive response, and everything between. Class Ctfc extend CTFC::Data, for easier work.
Direct Known Subclasses
Constant Summary
Constants included from CONFIG
CONFIG::COINS, CONFIG::MAX_RETRY, CONFIG::URL
Instance Attribute Summary collapse
-
#coins ⇒ Object
Returns the value of attribute coins.
-
#count ⇒ Object
readonly
Returns the value of attribute count.
-
#data ⇒ Object
readonly
Returns the value of attribute data.
-
#fiat ⇒ Object
(also: #currency)
Returns the value of attribute fiat.
-
#prices ⇒ Object
readonly
Returns the value of attribute prices.
-
#response ⇒ Object
readonly
Returns the value of attribute response.
-
#table ⇒ Object
readonly
Returns the value of attribute table.
-
#url ⇒ Object
readonly
Returns the value of attribute url.
Instance Method Summary collapse
-
#get(currency = nil, opts = {}) ⇒ Hash || false
Hash of coins and fiat values, or false if all requests fail.
-
#initialize(currency = :eur, opts = {}) ⇒ Data
constructor
Data object to work with.
-
#price(coin) ⇒ Float
Get fiat value from response hash with crypto prices.
-
#print=(opt) ⇒ true || false
Change option to print prices in terminal.
-
#print? ⇒ true || false
Check if crypto prices will be printed in terminal.
-
#save=(opt) ⇒ true || false
Change option to save ‘.csv’ table with prices.
-
#save? ⇒ true || false
Check if crypto prices will be saved in ‘.csv` table.
-
#success? ⇒ true || false
Check if request was successful or not.
Constructor Details
#initialize(currency = :eur, opts = {}) ⇒ Data
Returns Data object to work with.
47 48 49 50 51 52 |
# 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 |
Instance Attribute Details
#coins ⇒ Object
Returns the value of attribute coins.
30 31 32 |
# File 'lib/ctfc/base.rb', line 30 def coins @coins end |
#count ⇒ Object (readonly)
Returns the value of attribute count.
29 30 31 |
# File 'lib/ctfc/base.rb', line 29 def count @count end |
#data ⇒ Object (readonly)
Returns the value of attribute data.
29 30 31 |
# File 'lib/ctfc/base.rb', line 29 def data @data end |
#fiat ⇒ Object Also known as: currency
Returns the value of attribute fiat.
30 31 32 |
# File 'lib/ctfc/base.rb', line 30 def fiat @fiat end |
#prices ⇒ Object (readonly)
Returns the value of attribute prices.
29 30 31 |
# File 'lib/ctfc/base.rb', line 29 def prices @prices end |
#response ⇒ Object (readonly)
Returns the value of attribute response.
29 30 31 |
# File 'lib/ctfc/base.rb', line 29 def response @response end |
#table ⇒ Object (readonly)
Returns the value of attribute table.
29 30 31 |
# File 'lib/ctfc/base.rb', line 29 def table @table end |
#url ⇒ Object (readonly)
Returns the value of attribute url.
29 30 31 |
# File 'lib/ctfc/base.rb', line 29 def url @url end |
Instance Method Details
#get(currency = nil, opts = {}) ⇒ Hash || false
Returns Hash of coins and fiat values, or false if all requests fail.
68 69 70 71 72 73 74 75 76 |
# 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) ⇒ Float
Get fiat value from response hash with crypto prices
88 89 90 |
# File 'lib/ctfc/base.rb', line 88 def price(coin) @prices[coin.to_s.upcase] end |
#print=(opt) ⇒ true || false
Change option to print prices in terminal
124 125 126 |
# File 'lib/ctfc/base.rb', line 124 def print=(opt) @print = opt.is_a?(TrueClass) end |
#print? ⇒ true || false
Check if crypto prices will be printed in terminal
106 107 108 |
# File 'lib/ctfc/base.rb', line 106 def print? @print == true end |
#save=(opt) ⇒ true || false
Change option to save ‘.csv’ table with prices
115 116 117 |
# File 'lib/ctfc/base.rb', line 115 def save=(opt) @save = opt.is_a?(TrueClass) end |
#save? ⇒ true || false
Check if crypto prices will be saved in ‘.csv` table
97 98 99 |
# File 'lib/ctfc/base.rb', line 97 def save? @save == true end |
#success? ⇒ true || false
Check if request was successful or not.
133 134 135 136 137 |
# File 'lib/ctfc/base.rb', line 133 def success? return false if @response.nil? @response.code == 200 end |