class Cwallet::Wallet::NetHTTPClient

Public Class Methods

new(base_uri, options = {}) click to toggle source
# File lib/cwallet/wallet/adapters/net_http.rb, line 4
def initialize(base_uri, options = {})
  @conn = Net::HTTP.new(base_uri.host, base_uri.port)
  @conn.use_ssl = true if base_uri.scheme == 'https'
end

Private Instance Methods

debug() click to toggle source
# File lib/cwallet/wallet/adapters/net_http.rb, line 11
def debug
  raise 'Need to be implemented'
end
http_verb(method, path, body = nil, headers = {}) { |out| ... } click to toggle source
# File lib/cwallet/wallet/adapters/net_http.rb, line 15
def http_verb(method, path, body = nil, headers = {})
  case method
  when 'GET' then req = Net::HTTP::Get.new(path)
  when 'PUT' then req = Net::HTTP::Put.new(path)
  when 'POST' then req = Net::HTTP::Post.new(path)
  when 'DELETE' then req = Net::HTTP::Delete.new(path)
  else raise
  end

  req.body = body

  req['Content-Type'] = 'application/json'
  req['User-Agent'] = "cwallet/ruby/#{Cwallet::Wallet::VERSION}"
  auth_headers(method, path, body).each do |key, val|
    req[key] = val
  end
  headers.each do |key, val|
    req[key] = val
  end

  if debug
    $stdout.puts("=====REQUEST:\n#{req.method} #{req.path}\nHEADERS: #{req.to_hash}\nBODY: #{req.body}\n=====")
  end

  resp = @conn.request(req)
  out = NetHTTPResponse.new(resp)

  if debug
    $stdout.puts("=====RESPONSE:\nSTATUS: #{out.status}\nBODY: #{out.body}\n=====")
  end

  Cwallet::Wallet::check_response_status(out)
  yield(out)
  out.data
end