Module: BitPay::RestConnector

Included in:
SDK::Client
Defined in:
lib/bitpay/rest_connector.rb

Instance Method Summary collapse

Instance Method Details

#delete(path:, token: nil) ⇒ Object



45
46
47
48
49
50
51
52
# 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) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
# 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:) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
# 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) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
# 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