module BitPay::RestConnector

Public Instance Methods

delete(path:, token: nil) click to toggle source
# File lib/bitpay/rest_connector.rb, line 45
def delete(path:, token: nil)
  urlpath = '/' + path
  urlpath = urlpath + '?token=' + token if token
  request = Net::HTTP::Delete.new urlpath
  request['X-Signature'] = KeyUtils.sign(@uri.to_s + urlpath, @priv_key) 
  request['X-Identity'] = @pub_key
  process_request(request)
end
get(path:, token: nil, public: false) click to toggle source
# File lib/bitpay/rest_connector.rb, line 19
def get(path:, token: nil, public: false)
  urlpath = '/' + path
  token_prefix = if urlpath.include? '?' then '&token=' else '?token=' end
  urlpath = urlpath + token_prefix + token if token
  request = Net::HTTP::Get.new urlpath
  unless public
    request['X-Signature'] = KeyUtils.sign(@uri.to_s + urlpath, @priv_key) 
    request['X-Identity'] = @pub_key
  end
  process_request(request)
end
post(path:, token: nil, params:) click to toggle source
# File lib/bitpay/rest_connector.rb, line 31
def post(path:, token: nil, params:)
  urlpath = '/' + path
  request = Net::HTTP::Post.new urlpath
  params[:token] = token if token
  params[:guid]  = SecureRandom.uuid
  params[:id] = @client_id
  request.body = params.to_json
  if token
    request['X-Signature'] = KeyUtils.sign(@uri.to_s + urlpath + request.body, @priv_key)
    request['X-Identity'] = @pub_key
  end
  process_request(request)
end
send_request(verb, path, facade: 'merchant', params: {}, token: nil) click to toggle source
# File lib/bitpay/rest_connector.rb, line 7
def send_request(verb, path, facade: 'merchant', params: {}, token: nil)
  token ||= get_token(facade)
  case verb.upcase
  when "GET"
    return get(path: path, token: token)
  when "POST"
    return post(path: path, token: token, params: params)
  else
    raise(BitPayError, "Invalid HTTP verb: #{verb.upcase}")
  end
end

Private Instance Methods

get_token(facade) click to toggle source

Makes a request to /tokens for pairing

  Adds passed params as post parameters
  If empty params, retrieves server-generated pairing code
  If pairingCode key/value is passed, will pair client ID to this account
Returns response hash
# File lib/bitpay/rest_connector.rb, line 101
def get_token(facade)
  token = @tokens[facade] || refresh_tokens[facade] || raise(BitPayError, "Not authorized for facade: #{facade}")
end
process_request(request) click to toggle source
Processes HTTP Request and returns parsed response

Otherwise throws error

# File lib/bitpay/rest_connector.rb, line 59
def process_request(request)
  request['User-Agent'] = @user_agent
  request['Content-Type'] = 'application/json'
  request['X-BitPay-Plugin-Info'] = 'Rubylib' + VERSION

  begin
    response = @https.request request
  rescue => error
    raise BitPay::ConnectionError, "#{error.message}"
  end

  if response.kind_of? Net::HTTPSuccess
    return JSON.parse(response.body)
  elsif JSON.parse(response.body)["error"]
    raise(BitPayError, "#{response.code}: #{JSON.parse(response.body)['error']}")
  else
    raise BitPayError, "#{response.code}: #{JSON.parse(response.body)}"
  end

end
refresh_tokens() click to toggle source

Fetches the tokens hash from the server and updates @tokens

# File lib/bitpay/rest_connector.rb, line 83
def refresh_tokens
  response = get(path: 'tokens')["data"]
  token_array = response || {}
  tokens = {}
  token_array.each do |t|
    tokens[t.keys.first] = t.values.first
  end
  @tokens = tokens
  return tokens
end