pinetwork 0.1.1 → 0.1.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/errors.rb +32 -0
- data/lib/{pi_network.rb → pinetwork.rb} +16 -10
- metadata +4 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 1e55714fc1d6fbd13ff97e4d78e32d918a7926bc29e2e10cba93c8c9797ff9d5
|
4
|
+
data.tar.gz: f5dc2d60e6f0b835d677504e6ea70d0b78e21e48f3ad09a692287e5feacd7d71
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 68b9a5f54bab85dd97c1e358a8d7ae428e7c04b5029ebdcfe66bfb1057fc53c59126236528aaada6c8017a25050fc3bd251287eb4d132f8ebf8ece6cad467506
|
7
|
+
data.tar.gz: 469f3afd74061b371b27118172bc3f10955fd24e89f68c00fe26e2677e9ca3b09a69949e70babfd9d10679784c62619b44420cb50b40dc13fdf26cf1298abc05
|
data/lib/errors.rb
ADDED
@@ -0,0 +1,32 @@
|
|
1
|
+
class ::PiNetwork
|
2
|
+
module Errors
|
3
|
+
class APIRequestError < StandardError
|
4
|
+
attr_reader :response_body
|
5
|
+
attr_reader :response_status
|
6
|
+
def initialize(message, response_status, response_body)
|
7
|
+
super(message)
|
8
|
+
@response_status = response_status
|
9
|
+
@response_body = response_body
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
class PaymentNotFoundError < StandardError
|
14
|
+
attr_reader :payment_id
|
15
|
+
def initialize(message, payment_id)
|
16
|
+
super(message)
|
17
|
+
@payment_id = payment_id
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
class TxidAlreadyLinkedError < StandardError
|
22
|
+
attr_reader :payment_id
|
23
|
+
attr_reader :txid
|
24
|
+
|
25
|
+
def initialize(message, payment_id, txid)
|
26
|
+
super(message)
|
27
|
+
@payment_id = payment_id
|
28
|
+
@txid = txid
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
@@ -1,5 +1,4 @@
|
|
1
|
-
|
2
|
-
require "#{gem_dir}/lib/errors"
|
1
|
+
require_relative 'errors'
|
3
2
|
|
4
3
|
class PiNetwork
|
5
4
|
require 'faraday'
|
@@ -17,6 +16,8 @@ class PiNetwork
|
|
17
16
|
@api_key = api_key
|
18
17
|
@account = load_account(wallet_private_key)
|
19
18
|
@base_url = options[:base_url] || "https://api.minepi.com"
|
19
|
+
@mainnet_host = options[:mainnet_host] || "api.mainnet.minepi.com"
|
20
|
+
@testnet_host = options[:testnet_host] || "api.testnet.minepi.com"
|
20
21
|
|
21
22
|
@open_payments = {}
|
22
23
|
end
|
@@ -59,8 +60,10 @@ class PiNetwork
|
|
59
60
|
def submit_payment(payment_id)
|
60
61
|
payment = @open_payments[payment_id]
|
61
62
|
|
62
|
-
if payment.nil?
|
63
|
+
if payment.nil? || payment["identifier"] != payment_id
|
63
64
|
payment = get_payment(payment_id)
|
65
|
+
txid = payment["transaction"]&.dig("txid")
|
66
|
+
raise Errors::TxidAlreadyLinkedError.new("This payment already has a linked txid", payment_id, txid) if txid.present?
|
64
67
|
end
|
65
68
|
|
66
69
|
set_horizon_client(payment["network"])
|
@@ -80,25 +83,27 @@ class PiNetwork
|
|
80
83
|
return txid
|
81
84
|
end
|
82
85
|
|
83
|
-
def complete_payment(
|
86
|
+
def complete_payment(payment_id, txid)
|
84
87
|
body = {"txid": txid}
|
85
88
|
|
86
89
|
response = Faraday.post(
|
87
|
-
base_url + "/v2/payments/#{
|
90
|
+
base_url + "/v2/payments/#{payment_id}/complete",
|
88
91
|
body.to_json,
|
89
92
|
http_headers
|
90
93
|
)
|
91
94
|
|
95
|
+
@open_payments.delete(payment_id)
|
92
96
|
handle_http_response(response, "An unknown error occurred while completing the payment")
|
93
97
|
end
|
94
98
|
|
95
|
-
def cancel_payment(
|
99
|
+
def cancel_payment(payment_id)
|
96
100
|
response = Faraday.post(
|
97
|
-
base_url + "/v2/payments/#{
|
101
|
+
base_url + "/v2/payments/#{payment_id}/cancel",
|
98
102
|
{}.to_json,
|
99
103
|
http_headers,
|
100
104
|
)
|
101
105
|
|
106
|
+
@open_payments.delete(payment_id)
|
102
107
|
handle_http_response(response, "An unknown error occurred while cancelling the payment")
|
103
108
|
end
|
104
109
|
|
@@ -126,7 +131,7 @@ class PiNetwork
|
|
126
131
|
|
127
132
|
def handle_http_response(response, unknown_error_message = "An unknown error occurred while making an API request")
|
128
133
|
unless response.status == 200
|
129
|
-
error_message = JSON.parse(response.body).dig("error_message") rescue
|
134
|
+
error_message = JSON.parse(response.body).dig("error_message") rescue unknown_error_message
|
130
135
|
raise Errors::APIRequestError.new(error_message, response.status, response.body)
|
131
136
|
end
|
132
137
|
|
@@ -140,8 +145,9 @@ class PiNetwork
|
|
140
145
|
end
|
141
146
|
|
142
147
|
def set_horizon_client(network)
|
143
|
-
host = network
|
144
|
-
horizon =
|
148
|
+
host = (network.starts_with? "Pi Network") ? @mainnet_host : @testnet_host
|
149
|
+
horizon = "https://#{host}"
|
150
|
+
|
145
151
|
client = Stellar::Client.new(host: host, horizon: horizon)
|
146
152
|
Stellar::default_network = network
|
147
153
|
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: pinetwork
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Pi Core Team
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2023-
|
11
|
+
date: 2023-10-12 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: stellar-sdk
|
@@ -44,7 +44,8 @@ executables: []
|
|
44
44
|
extensions: []
|
45
45
|
extra_rdoc_files: []
|
46
46
|
files:
|
47
|
-
- lib/
|
47
|
+
- lib/errors.rb
|
48
|
+
- lib/pinetwork.rb
|
48
49
|
homepage: https://github.com/pi-apps/pi-ruby
|
49
50
|
licenses:
|
50
51
|
- PiOS
|