Class: Coinbase::Client
- Inherits:
-
Object
show all
- Includes:
- Util
- Defined in:
- lib/coinbase/client.rb
Overview
Base class to connect to coinbase
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
#initialize ⇒ Client
Returns a new instance of Client.
Class Method Details
.configuration ⇒ Object
71
72
73
|
# File 'lib/coinbase/client.rb', line 71
def self.configuration
@configuration ||= HashWithIndifferentAccess.new
end
|
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
|
26
27
28
29
30
31
32
33
34
35
36
37
38
|
# File 'lib/coinbase/client.rb', line 26
def (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
40
41
42
43
44
45
|
# File 'lib/coinbase/client.rb', line 40
def http_request(method, path, body: nil)
= (method, path, body)
request_params = { 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}"
secret = Base64.decode64(@api_secret)
hash = OpenSSL::HMAC.digest('sha256', secret, message)
Base64.strict_encode64(hash)
end
|