Class: CTFC::Data

Inherits:
Object
  • Object
show all
Includes:
CONFIG
Defined in:
lib/ctfc/base.rb

Overview

Note:

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

Ctfc

Constant Summary

Constants included from CONFIG

CONFIG::COINS, CONFIG::MAX_RETRY, CONFIG::URL

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(currency = :eur, opts = {}) ⇒ Object

Returns Data object to work with.

Examples:

Initialization example


@data = CTFC::Data.new :eur, save: true

Parameters:

  • currency (Symbol) (defaults to: :eur)

    Optional. Define fiat currency.

  • opts (Hash) (defaults to: {})

    Optional. Additional options hash.

Options Hash (opts):

  • print (Boolean)

    Optional. Print terminal output.

  • save (Boolean)

    Optional. Save ‘.csv` output.

  • coins (Array)

    Optional. Define coins to scrap.



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

#coinsObject

Returns the value of attribute coins.



22
23
24
# File 'lib/ctfc/base.rb', line 22

def coins
  @coins
end

#countObject (readonly)

Returns the value of attribute count.



21
22
23
# File 'lib/ctfc/base.rb', line 21

def count
  @count
end

#dataObject (readonly)

Returns the value of attribute data.



21
22
23
# File 'lib/ctfc/base.rb', line 21

def data
  @data
end

#fiatObject Also known as: currency

Returns the value of attribute fiat.



22
23
24
# File 'lib/ctfc/base.rb', line 22

def fiat
  @fiat
end

#pricesObject (readonly)

Returns the value of attribute prices.



21
22
23
# File 'lib/ctfc/base.rb', line 21

def prices
  @prices
end

#responseObject (readonly)

Returns the value of attribute response.



21
22
23
# File 'lib/ctfc/base.rb', line 21

def response
  @response
end

#tableObject (readonly)

Returns the value of attribute table.



21
22
23
# File 'lib/ctfc/base.rb', line 21

def table
  @table
end

#urlObject (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

Examples:

Get fiat prices for previous config


@data.get

Get prices and change previous config “on-the-fly”


@data.get :usd, save: false, coins: %w[BTC XMR ETH]

Parameters:

  • currency (Symbol || String) (defaults to: nil)

    Optional. Change fiat currency and execute request.

  • opts (Hash) (defaults to: {})

    Optional. Options hash to change config ‘on-the-fly’ - see #initialize.



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

Examples:


@data.price(:btc)

Parameters:

  • coin (Symbol || String)

    Required. Coin name as symbol or string.

Returns:

  • (Float)


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

Returns:

  • (true || false)


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

Returns:

  • (true || false)


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

Returns:

  • (true || false)


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

Returns:

  • (true || false)


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.

Returns:

  • (true || false)


124
125
126
127
128
# File 'lib/ctfc/base.rb', line 124

def success?
  return false if @response.nil?

  @response.code == 200
end