Class: BitPay::Client
- Inherits:
-
Object
- Object
- BitPay::Client
- Defined in:
- lib/bitpay/client.rb
Overview
This class is used to instantiate a BitPay Client object. It is expected to be thread safe.
Instance Method Summary collapse
- #create_invoice(id:, price:, currency:, facade: 'pos', params: {}) ⇒ Object
- #get_public_invoice(id:) ⇒ Object
- #initialize(opts = {}) ⇒ Client constructor
- #pair_pos_client(claimCode) ⇒ Object
-
#send_request(verb, path, facade: 'merchant', params: {}, token: nil) ⇒ Object
Generates REST request to api endpoint.
Constructor Details
#initialize(opts = {}) ⇒ Client
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/bitpay/client.rb', line 21 def initialize(opts={}) @pem = opts[:pem] || ENV['BITPAY_PEM'] || KeyUtils.retrieve_or_generate_pem @key = KeyUtils.create_key @pem @priv_key = KeyUtils.get_private_key @key @pub_key = KeyUtils.get_public_key @key @client_id = KeyUtils.generate_sin_from_pem @pem @uri = URI.parse opts[:api_uri] || API_URI @user_agent = opts[:user_agent] || USER_AGENT @https = Net::HTTP.new @uri.host, @uri.port @https.use_ssl = true @https.ca_file = CA_FILE # Option to disable certificate validation in extraordinary circumstance. NOT recommended for production use @https.verify_mode = opts[:insecure] == true ? OpenSSL::SSL::VERIFY_NONE : OpenSSL::SSL::VERIFY_PEER # Option to enable http request debugging @https.set_debug_output($stdout) if opts[:debug] == true # Load all the available tokens into @tokens load_tokens end |
Instance Method Details
#create_invoice(id:, price:, currency:, facade: 'pos', params: {}) ⇒ Object
56 57 58 59 60 |
# File 'lib/bitpay/client.rb', line 56 def create_invoice(id:, price:, currency:, facade: 'pos', params:{}) params.merge!({price: price, currency: currency}) response = send_request("POST", "invoices", facade: facade, params: params) response["data"] end |
#get_public_invoice(id:) ⇒ Object
62 63 64 65 66 |
# File 'lib/bitpay/client.rb', line 62 def get_public_invoice(id:) request = Net::HTTP::Get.new("/invoices/#{id}") response = @https.request request (JSON.parse response.body)["data"] end |
#pair_pos_client(claimCode) ⇒ Object
43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/bitpay/client.rb', line 43 def pair_pos_client(claimCode) response = set_pos_token(claimCode) case response.code when "200" get_token 'pos' when "500" raise BitPayError, JSON.parse(response.body)["error"] else raise BitPayError, "#{response.code}: #{JSON.parse(response.body)}" end response end |
#send_request(verb, path, facade: 'merchant', params: {}, token: nil) ⇒ Object
Generates REST request to api endpoint
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/bitpay/client.rb', line 69 def send_request(verb, path, facade: 'merchant', params: {}, token: nil) token ||= @tokens[facade] || raise(BitPayError, "No token for specified facade: #{facade}") # Verb-specific logic case verb.upcase when "GET" urlpath = '/' + path + '?nonce=' + KeyUtils.nonce + '&token=' + token request = Net::HTTP::Get.new urlpath request['X-Signature'] = KeyUtils.sign(@uri.to_s + urlpath, @priv_key) when "PUT" when "POST" # Requires a GUID urlpath = '/' + path request = Net::HTTP::Post.new urlpath params[:token] = token params[:nonce] = KeyUtils.nonce params[:guid] = SecureRandom.uuid params[:id] = @client_id request.body = params.to_json request['X-Signature'] = KeyUtils.sign(@uri.to_s + urlpath + request.body, @priv_key) when "DELETE" raise(BitPayError, "Invalid HTTP verb: #{verb.upcase}") end # Build request headers and submit request['User-Agent'] = @user_agent request['Content-Type'] = 'application/json' request['X-BitPay-Plugin-Info'] = 'Rubylib' + VERSION request['X-Identity'] = @pub_key response = @https.request request JSON.parse response.body end |