Class: Coinbase::Client

Inherits:
Object
  • Object
show all
Includes:
Util
Defined in:
lib/coinbase/client.rb

Overview

Base class to connect to coinbase

Direct Known Subclasses

Accounts, Conversions, Fees, Orders, Transactions, Transfers

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Util

#base_uri, #build_query_params, #format_response, #pagination_params, #send_request

Constructor Details

#initializeClient

Returns a new instance of Client.



8
9
10
11
12
13
14
15
# File 'lib/coinbase/client.rb', line 8

def initialize
  @api_key = Coinbase::Client.configuration[:api_key]
  @api_secret = Coinbase::Client.configuration[:api_secret]
  @passphrase = Coinbase::Client.configuration[:passphrase]
  [@api_key, @api_secret, @passphrase].each do |opt|
    raise 'Missing coinbase credentials' if opt.blank?
  end
end

Class Method Details

.configurationObject



71
72
73
# File 'lib/coinbase/client.rb', line 71

def self.configuration
  @configuration ||= HashWithIndifferentAccess.new
end

.configure {|configuration| ... } ⇒ Object

Yields:



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

def self.configure
  yield(configuration)
end

Instance Method Details

#delete(path) ⇒ Object



65
66
67
68
69
# File 'lib/coinbase/client.rb', line 65

def delete(path)
  http_request(:DELETE, path) do |request_params|
    HTTParty.delete("#{base_uri}#{path}", request_params)
  end
end

#get(path) ⇒ Object



47
48
49
50
51
# File 'lib/coinbase/client.rb', line 47

def get(path)
  http_request(:GET, path) do |request_params|
    HTTParty.get("#{base_uri}#{path}", request_params)
  end
end

#headers(method, path, body) ⇒ Object



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

def headers(method, path, body)
  timestamp = Time.now.to_i.to_s
  sign = signature(method, timestamp, request_path: path, body: body)
  {
    'Content-Type' => 'application/json',
    'Accept' => 'application/json',
    'CB-ACCESS-KEY' => @api_key,
    'CB-ACCESS-SIGN' => sign,
    'CB-ACCESS-TIMESTAMP' => timestamp,
    'CB-ACCESS-PASSPHRASE' => @passphrase,
    'User-Agent' => 'request'
  }
end

#http_request(method, path, body: nil) {|request_params| ... } ⇒ Object

Yields:

  • (request_params)


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

def http_request(method, path, body: nil)
  request_headers = headers(method, path, body)
  request_params = { headers: request_headers }
  request_params.merge!(body: body.to_json) if body.present?
  yield(request_params) if block_given?
end

#post(path, body: nil) ⇒ Object



53
54
55
56
57
# File 'lib/coinbase/client.rb', line 53

def post(path, body: nil)
  http_request(:POST, path, body: body) do |request_params|
    HTTParty.post("#{base_uri}#{path}", request_params)
  end
end

#put(path, body: nil) ⇒ Object



59
60
61
62
63
# File 'lib/coinbase/client.rb', line 59

def put(path, body: nil)
  http_request(:PUT, path, body: body) do |request_params|
    HTTParty.put("#{base_uri}#{path}", request_params)
  end
end

#signature(method, timestamp, request_path: '', body: nil) ⇒ Object



17
18
19
20
21
22
23
24
# File 'lib/coinbase/client.rb', line 17

def signature(method, timestamp, request_path: '', body: nil)
  body = body.to_json if body.is_a?(Hash)
  message = "#{timestamp}#{method}#{request_path}#{body}"
  # create a sha256 hmac with the secret
  secret = Base64.decode64(@api_secret)
  hash = OpenSSL::HMAC.digest('sha256', secret, message)
  Base64.strict_encode64(hash)
end