Class: CTFC::Data
Overview
Instead of using CTFC::Data.new, recommended way is to 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 = {}) ⇒ Object
-
#initialize(currency = :eur, opts = {}) ⇒ Object
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 = {}) ⇒ Object
Returns Data object to work with.
40 41 42 43 44 45 |
# File 'lib/ctfc/base.rb', line 40 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.
22 23 24 |
# File 'lib/ctfc/base.rb', line 22 def coins @coins end |
#count ⇒ Object (readonly)
Returns the value of attribute count.
21 22 23 |
# File 'lib/ctfc/base.rb', line 21 def count @count end |
#data ⇒ Object (readonly)
Returns the value of attribute data.
21 22 23 |
# File 'lib/ctfc/base.rb', line 21 def data @data end |
#fiat ⇒ Object Also known as: currency
Returns the value of attribute fiat.
22 23 24 |
# File 'lib/ctfc/base.rb', line 22 def fiat @fiat end |
#prices ⇒ Object (readonly)
Returns the value of attribute prices.
21 22 23 |
# File 'lib/ctfc/base.rb', line 21 def prices @prices end |
#response ⇒ Object (readonly)
Returns the value of attribute response.
21 22 23 |
# File 'lib/ctfc/base.rb', line 21 def response @response end |
#table ⇒ Object (readonly)
Returns the value of attribute table.
21 22 23 |
# File 'lib/ctfc/base.rb', line 21 def table @table end |
#url ⇒ Object (readonly)
Returns the value of attribute url.
21 22 23 |
# File 'lib/ctfc/base.rb', line 21 def url @url end |
Instance Method Details
#get(currency = nil, opts = {}) ⇒ Object
59 60 61 62 63 64 65 66 67 |
# File 'lib/ctfc/base.rb', line 59 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
79 80 81 |
# File 'lib/ctfc/base.rb', line 79 def price(coin) @prices[coin.to_s.upcase] end |
#print=(opt) ⇒ true || false
Change option to print prices in terminal
115 116 117 |
# File 'lib/ctfc/base.rb', line 115 def print=(opt) @print = opt.is_a?(TrueClass) end |
#print? ⇒ true || false
Check if crypto prices will be printed in terminal
97 98 99 |
# File 'lib/ctfc/base.rb', line 97 def print? @print == true end |
#save=(opt) ⇒ true || false
Change option to save ‘.csv’ table with prices
106 107 108 |
# File 'lib/ctfc/base.rb', line 106 def save=(opt) @save = opt.is_a?(TrueClass) end |
#save? ⇒ true || false
Check if crypto prices will be saved in ‘.csv` table
88 89 90 |
# File 'lib/ctfc/base.rb', line 88 def save? @save == true end |
#success? ⇒ true || false
Check if request was successful or not.
124 125 126 127 128 |
# File 'lib/ctfc/base.rb', line 124 def success? return false if @response.nil? @response.code == 200 end |