class Coinbase::Exchange::NetHTTPClient

Net-HTTP adapter

Public Class Methods

new(api_key = '', api_secret = '', api_pass = '', options = {}) click to toggle source
Calls superclass method
# File lib/coinbase/exchange/adapters/net_http.rb, line 5
def initialize(api_key = '', api_secret = '', api_pass = '', options = {})
  super(api_key, api_secret, api_pass, options)
  @conn = Net::HTTP.new(@api_uri.host, @api_uri.port)
  @conn.use_ssl = true if @api_uri.scheme == 'https'
  @conn.cert_store = self.class.whitelisted_certificates
  @conn.ssl_version = :TLSv1
end

Private Instance Methods

http_verb(method, path, body = nil) { |net_http_response| ... } click to toggle source
# File lib/coinbase/exchange/adapters/net_http.rb, line 15
def http_verb(method, path, body = nil)
  case method
  when 'GET' then req = Net::HTTP::Get.new(path)
  when 'POST' then req = Net::HTTP::Post.new(path)
  when 'DELETE' then req = Net::HTTP::Delete.new(path)
  else fail
  end

  req.body = body

  req_ts = Time.now.utc.to_i.to_s
  signature = Base64.encode64(
    OpenSSL::HMAC.digest('sha256', Base64.decode64(@api_secret).strip,
                         "#{req_ts}#{method}#{path}#{body}")).strip
  req['Content-Type'] = 'application/json'
  req['CB-ACCESS-TIMESTAMP'] = req_ts
  req['CB-ACCESS-PASSPHRASE'] = @api_pass
  req['CB-ACCESS-KEY'] = @api_key
  req['CB-ACCESS-SIGN'] = signature

  resp = @conn.request(req)
  case resp.code
  when "200" then yield(NetHTTPResponse.new(resp))
  when "400" then fail BadRequestError, resp.body
  when "401" then fail NotAuthorizedError, resp.body
  when "403" then fail ForbiddenError, resp.body
  when "404" then fail NotFoundError, resp.body
  when "429" then fail RateLimitError, resp.body
  when "500" then fail InternalServerError, resp.body
  end
  resp.body
end