Class: Coinbase::Advanced::APIBase

Inherits:
Object
  • Object
show all
Defined in:
lib/coinbase/advanced/api_base.rb

Overview

APIBase serves primarily to store state required for authentication (REST / Websocket) In addition it should house any contexts / configurations that are shared between communication protocols

Direct Known Subclasses

REST::RESTBase

Constant Summary collapse

JSON_API_KEY =
"name"
JSON_API_SECRET =
"privateKey"

Instance Method Summary collapse

Constructor Details

#initialize(config = Coinbase::Advanced.config) ⇒ APIBase

Returns a new instance of APIBase.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/coinbase/advanced/api_base.rb', line 11

def initialize(config = Coinbase::Advanced.config)
  @config = config

  @key_file = config&.key_file
  @api_key = config&.api_key
  @api_secret = config&.api_secret

  @base_url = config&.base_url
  @api_prefix = config&.api_prefix
  @timeout = config&.timeout

  @verbose = config&.verbose

  @authenticated = authenticate
  @config.log("Authentication failed. You may only access public endpoints.", :warn) unless @authenticated
end

Instance Method Details

#authenticate ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
# File 'lib/coinbase/advanced/api_base.rb', line 28

def authenticate
  if @key_file
    if @api_key && @api_secret
      @config.log "Ambiguous authentication scheme: use either api_key + api_secret OR key_file.", :warn
      @config.log "Defaulting to key_file credentials", :warn
    end
    parse_key_file
  end

  @api_key && @api_secret
end

#parse_key_file ⇒ Object



40
41
42
43
44
45
46
# File 'lib/coinbase/advanced/api_base.rb', line 40

def parse_key_file
  js = JSON.parse(File.read(@key_file))
  @api_key = js[JSON_API_KEY]
  @api_secret = js[JSON_API_SECRET]
rescue StandardError => e
  @config.log "Failed to parse JSON key file: #{e.message}", :error
end