Class: PiNetwork
- Inherits:
-
Object
- Object
- PiNetwork
- Defined in:
- lib/errors.rb,
lib/pinetwork.rb
Defined Under Namespace
Modules: Errors
Constant Summary collapse
- BASE_URL =
"https://api.minepi.com".freeze
- MAINNET_HOST =
"api.mainnet.minepi.com".freeze
- TESTNET_HOST =
"api.testnet.minepi.com".freeze
Instance Attribute Summary collapse
-
#account ⇒ Object
readonly
Returns the value of attribute account.
-
#api_key ⇒ Object
readonly
Returns the value of attribute api_key.
-
#base_url ⇒ Object
readonly
Returns the value of attribute base_url.
-
#client ⇒ Object
readonly
Returns the value of attribute client.
-
#from_address ⇒ Object
readonly
Returns the value of attribute from_address.
Instance Method Summary collapse
- #cancel_payment(payment_id) ⇒ Object
- #complete_payment(payment_id, txid) ⇒ Object
- #create_payment(payment_data) ⇒ Object
- #get_incomplete_server_payments ⇒ Object
- #get_payment(payment_id) ⇒ Object
-
#initialize(api_key:, wallet_private_key:, faraday: Faraday.new, options: {}) ⇒ PiNetwork
constructor
A new instance of PiNetwork.
- #submit_payment(payment_id) ⇒ Object
Constructor Details
#initialize(api_key:, wallet_private_key:, faraday: Faraday.new, options: {}) ⇒ PiNetwork
Returns a new instance of PiNetwork.
18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/pinetwork.rb', line 18 def initialize(api_key:, wallet_private_key:, faraday: Faraday.new, options: {}) validate_private_seed_format!(wallet_private_key) @api_key = api_key @account = load_account(wallet_private_key) @base_url = [:base_url] || BASE_URL @mainnet_host = [:mainnet_host] || MAINNET_HOST @testnet_host = [:testnet_host] || TESTNET_HOST @faraday = faraday @open_payments = {} @open_payments_mutex = Mutex.new end |
Instance Attribute Details
#account ⇒ Object (readonly)
Returns the value of attribute account.
10 11 12 |
# File 'lib/pinetwork.rb', line 10 def account @account end |
#api_key ⇒ Object (readonly)
Returns the value of attribute api_key.
8 9 10 |
# File 'lib/pinetwork.rb', line 8 def api_key @api_key end |
#base_url ⇒ Object (readonly)
Returns the value of attribute base_url.
11 12 13 |
# File 'lib/pinetwork.rb', line 11 def base_url @base_url end |
#client ⇒ Object (readonly)
Returns the value of attribute client.
9 10 11 |
# File 'lib/pinetwork.rb', line 9 def client @client end |
#from_address ⇒ Object (readonly)
Returns the value of attribute from_address.
12 13 14 |
# File 'lib/pinetwork.rb', line 12 def from_address @from_address end |
Instance Method Details
#cancel_payment(payment_id) ⇒ Object
112 113 114 115 116 117 118 119 120 121 122 123 124 |
# File 'lib/pinetwork.rb', line 112 def cancel_payment(payment_id) response = Faraday.post( base_url + "/v2/payments/#{payment_id}/cancel", {}.to_json, http_headers, ) @open_payments_mutex.synchronize do @open_payments.delete(payment_id) end handle_http_response(response, "An unknown error occurred while cancelling the payment") end |
#complete_payment(payment_id, txid) ⇒ Object
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/pinetwork.rb', line 96 def complete_payment(payment_id, txid) body = {"txid": txid} response = Faraday.post( base_url + "/v2/payments/#{payment_id}/complete", body.to_json, http_headers ) @open_payments_mutex.synchronize do @open_payments.delete(payment_id) end handle_http_response(response, "An unknown error occurred while completing the payment") end |
#create_payment(payment_data) ⇒ Object
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/pinetwork.rb', line 45 def create_payment(payment_data) validate_payment_data!(payment_data, {amount: true, memo: true, metadata: true, uid: true}) request_body = { payment: payment_data, } response = @faraday.post( base_url + "/v2/payments", request_body.to_json, http_headers, ) parsed_response = handle_http_response(response, "An unknown error occurred while creating a payment") identifier = parsed_response["identifier"] @open_payments_mutex.synchronize do @open_payments[identifier] = parsed_response end return identifier end |
#get_incomplete_server_payments ⇒ Object
126 127 128 129 130 131 132 133 134 135 |
# File 'lib/pinetwork.rb', line 126 def get_incomplete_server_payments response = Faraday.get( base_url + "/v2/payments/incomplete_server_payments", {}, http_headers, ) res = handle_http_response(response, "An unknown error occurred while fetching incomplete payments") res["incomplete_server_payments"] end |
#get_payment(payment_id) ⇒ Object
31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/pinetwork.rb', line 31 def get_payment(payment_id) response = Faraday.get( base_url + "/v2/payments/#{payment_id}", {}, http_headers, ) if response.status == 404 raise Errors::PaymentNotFoundError.new("Payment not found", payment_id) end handle_http_response(response, "An unknown error occurred while fetching the payment") end |
#submit_payment(payment_id) ⇒ Object
68 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 |
# File 'lib/pinetwork.rb', line 68 def submit_payment(payment_id) @open_payments_mutex.synchronize do payment = @open_payments[payment_id] if payment.nil? || payment["identifier"] != payment_id payment = get_payment(payment_id) txid = payment["transaction"]&.dig("txid") raise Errors::TxidAlreadyLinkedError.new("This payment already has a linked txid", payment_id, txid) if txid.present? end set_horizon_client(payment["network"]) @from_address = payment["from_address"] transaction_data = { amount: payment["amount"], identifier: payment["identifier"], recipient: payment["to_address"] } transaction = build_a2u_transaction(transaction_data) txid = submit_transaction(transaction) @open_payments.delete(payment_id) return txid end end |