Class: CTFC::Client

Inherits:
Object
  • Object
show all
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

Ctfc

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(fiat, coins, source = nil, opts = {}) ⇒ Client

Choose fiat currency, coins and source for new client.

Examples:

Initialize new *EUR* client

client = CTFC::Client.new :eur, %w[BTC XMR LTC ETH]

Parameters:

  • currency (Symbol)

    *Required*. Set fiat currency.

  • coins (Array)

    *Required*. Set crypto coins.

  • source (Symbol) (defaults to: nil)

    Optional. Source for data extraction.

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

    Options hash for additional configuration.

Options Hash (opts):

  • source (Symbol)

    Set source to extract data.

  • save (Boolean)

    Set option to save prices in csv table.

  • export (Boolean)

    Set option to export all data in json file.



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

#configObject (readonly)

Returns the value of attribute config.



13
14
15
# File 'lib/ctfc/client.rb', line 13

def config
  @config
end

#pricesObject (readonly)

Returns the value of attribute prices.



13
14
15
# File 'lib/ctfc/client.rb', line 13

def prices
  @prices
end

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

Returns:

  • (Boolean)


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.

Returns:

  • (Boolean)


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.

Examples:

client.get :cryptocompare

Parameters:

  • source (Symbol) (defaults to: nil)

    Source to send api request

Returns:

  • (Hash)

    Hash of fiat values for scrapped coins



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

Examples:

client.price(:btc)

Parameters:

  • coin (Symbol)

    *Required*. Coin name as symbol.

Returns:

  • (Float)


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.

Returns:

  • (Boolean)


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.

Returns:

  • (Boolean)


75
76
77
# File 'lib/ctfc/client.rb', line 75

def save?
  config[:save].is_a?(TrueClass)
end

#sourceSymbol

Source for data extraction.

Returns:

  • (Symbol)


61
62
63
# File 'lib/ctfc/client.rb', line 61

def source
  config[:source]
end

#source=(param) ⇒ Symbol

Set source for data extraction.

Returns:

  • (Symbol)


68
69
70
# File 'lib/ctfc/client.rb', line 68

def source=(param)
  @config[:source] = param
end

#success?Boolean

Check if request was successful.

Returns:

  • (Boolean)


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