Class: CTFC::Data

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

Overview

Note:

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

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 = {}) ⇒ Data

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.



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

#coinsObject

Returns the value of attribute coins.



30
31
32
# File 'lib/ctfc/base.rb', line 30

def coins
  @coins
end

#countObject (readonly)

Returns the value of attribute count.



29
30
31
# File 'lib/ctfc/base.rb', line 29

def count
  @count
end

#dataObject (readonly)

Returns the value of attribute data.



29
30
31
# File 'lib/ctfc/base.rb', line 29

def data
  @data
end

#fiatObject Also known as: currency

Returns the value of attribute fiat.



30
31
32
# File 'lib/ctfc/base.rb', line 30

def fiat
  @fiat
end

#pricesObject (readonly)

Returns the value of attribute prices.



29
30
31
# File 'lib/ctfc/base.rb', line 29

def prices
  @prices
end

#responseObject (readonly)

Returns the value of attribute response.



29
30
31
# File 'lib/ctfc/base.rb', line 29

def response
  @response
end

#tableObject (readonly)

Returns the value of attribute table.



29
30
31
# File 'lib/ctfc/base.rb', line 29

def table
  @table
end

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

Examples:

Get fiat prices for initialized config


@data.get

Get prices and change initialized 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.

Returns:

  • (Hash || false)

    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

Examples:


@data.price(:btc)

Parameters:

  • coin (Symbol || String)

    Required. Coin name as symbol or string.

Returns:

  • (Float)


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

Returns:

  • (true || false)


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

Returns:

  • (true || false)


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

Returns:

  • (true || false)


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

Returns:

  • (true || false)


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.

Returns:

  • (true || false)


133
134
135
136
137
# File 'lib/ctfc/base.rb', line 133

def success?
  return false if @response.nil?

  @response.code == 200
end