Class: CTFC::Client
- Inherits:
-
Object
- Object
- CTFC::Client
- Defined in:
- lib/ctfc/client.rb
Overview
Client allow us to get data from our sources, and to manipulate with that data. While other classes are mostly used by each-other, Client is mostly used directly by user.
Direct Known Subclasses
Instance Attribute Summary collapse
-
#config ⇒ Object
readonly
Returns the value of attribute config.
-
#prices ⇒ Object
readonly
Returns the value of attribute prices.
-
#response ⇒ Object
readonly
Returns the value of attribute response.
Instance Method Summary collapse
-
#export=(opt) ⇒ Boolean
Change option to export all data in json file after request.
-
#export? ⇒ Boolean
Check if json output will be exported after request.
-
#get(source = nil) ⇒ Hash
Scrap data from source.
-
#initialize(fiat, coins, source = nil, opts = {}) ⇒ Client
constructor
Choose fiat currency, coins and source for new client.
-
#price(coin) ⇒ Float
Get fiat value from response hash with crypto prices.
-
#save=(opt) ⇒ Boolean
Change option to save prices in csv table after request.
-
#save? ⇒ Boolean
Check if csv output will be saved after request.
-
#source ⇒ Symbol
Source for data extraction.
-
#source=(param) ⇒ Symbol
Set source for data extraction.
-
#success? ⇒ Boolean
Check if request was successful.
Constructor Details
#initialize(fiat, coins, source = nil, opts = {}) ⇒ Client
Choose fiat currency, coins and source for new client.
31 32 33 34 35 36 37 38 39 |
# File 'lib/ctfc/client.rb', line 31 def initialize(fiat, coins, source = nil, opts = {}) @config = { fiat: fiat, coins: coins, source: source || opts[:source], save: [nil, true].include?(opts[:save]), export: opts[:export].is_a?(TrueClass) } end |
Instance Attribute Details
#config ⇒ Object (readonly)
Returns the value of attribute config.
13 14 15 |
# File 'lib/ctfc/client.rb', line 13 def config @config end |
#prices ⇒ Object (readonly)
Returns the value of attribute prices.
13 14 15 |
# File 'lib/ctfc/client.rb', line 13 def prices @prices end |
#response ⇒ Object (readonly)
Returns the value of attribute response.
13 14 15 |
# File 'lib/ctfc/client.rb', line 13 def response @response end |
Instance Method Details
#export=(opt) ⇒ Boolean
Change option to export all data in json file after request.
96 97 98 |
# File 'lib/ctfc/client.rb', line 96 def export=(opt) @config[:export] = opt.is_a?(TrueClass) end |
#export? ⇒ Boolean
Check if json output will be exported after request.
89 90 91 |
# File 'lib/ctfc/client.rb', line 89 def export? config[:export].is_a?(TrueClass) end |
#get(source = nil) ⇒ Hash
Scrap data from source.
48 49 50 51 52 53 54 55 56 |
# File 'lib/ctfc/client.rb', line 48 def get(source = nil) source ||= config[:source] send_api_request(source) if success? Export.to_csv(source, response) if save? Export.to_json(source, response) if export? end @prices = response[:prices] end |
#price(coin) ⇒ Float
Get fiat value from response hash with crypto prices
107 108 109 |
# File 'lib/ctfc/client.rb', line 107 def price(coin) prices[coin.to_s.upcase] end |
#save=(opt) ⇒ Boolean
Change option to save prices in csv table after request.
82 83 84 |
# File 'lib/ctfc/client.rb', line 82 def save=(opt) @config[:save] = opt.is_a?(TrueClass) end |
#save? ⇒ Boolean
Check if csv output will be saved after request.
75 76 77 |
# File 'lib/ctfc/client.rb', line 75 def save? config[:save].is_a?(TrueClass) end |
#source ⇒ Symbol
Source for data extraction.
61 62 63 |
# File 'lib/ctfc/client.rb', line 61 def source config[:source] end |
#source=(param) ⇒ Symbol
Set source for data extraction.
68 69 70 |
# File 'lib/ctfc/client.rb', line 68 def source=(param) @config[:source] = param end |
#success? ⇒ Boolean
Check if request was successful.
113 114 115 116 117 |
# File 'lib/ctfc/client.rb', line 113 def success? return false if response.nil? response[:success].is_a?(TrueClass) end |