pinetwork 0.1.2 → 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} +14 -12
- 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
|
@@ -61,7 +62,7 @@ class PiNetwork
|
|
61
62
|
|
62
63
|
if payment.nil? || payment["identifier"] != payment_id
|
63
64
|
payment = get_payment(payment_id)
|
64
|
-
txid = payment["transaction"]
|
65
|
+
txid = payment["transaction"]&.dig("txid")
|
65
66
|
raise Errors::TxidAlreadyLinkedError.new("This payment already has a linked txid", payment_id, txid) if txid.present?
|
66
67
|
end
|
67
68
|
|
@@ -82,27 +83,27 @@ class PiNetwork
|
|
82
83
|
return txid
|
83
84
|
end
|
84
85
|
|
85
|
-
def complete_payment(
|
86
|
+
def complete_payment(payment_id, txid)
|
86
87
|
body = {"txid": txid}
|
87
88
|
|
88
89
|
response = Faraday.post(
|
89
|
-
base_url + "/v2/payments/#{
|
90
|
+
base_url + "/v2/payments/#{payment_id}/complete",
|
90
91
|
body.to_json,
|
91
92
|
http_headers
|
92
93
|
)
|
93
94
|
|
94
|
-
@open_payments.delete(
|
95
|
+
@open_payments.delete(payment_id)
|
95
96
|
handle_http_response(response, "An unknown error occurred while completing the payment")
|
96
97
|
end
|
97
98
|
|
98
|
-
def cancel_payment(
|
99
|
+
def cancel_payment(payment_id)
|
99
100
|
response = Faraday.post(
|
100
|
-
base_url + "/v2/payments/#{
|
101
|
+
base_url + "/v2/payments/#{payment_id}/cancel",
|
101
102
|
{}.to_json,
|
102
103
|
http_headers,
|
103
104
|
)
|
104
105
|
|
105
|
-
@open_payments.delete(
|
106
|
+
@open_payments.delete(payment_id)
|
106
107
|
handle_http_response(response, "An unknown error occurred while cancelling the payment")
|
107
108
|
end
|
108
109
|
|
@@ -130,7 +131,7 @@ class PiNetwork
|
|
130
131
|
|
131
132
|
def handle_http_response(response, unknown_error_message = "An unknown error occurred while making an API request")
|
132
133
|
unless response.status == 200
|
133
|
-
error_message = JSON.parse(response.body).dig("error_message") rescue
|
134
|
+
error_message = JSON.parse(response.body).dig("error_message") rescue unknown_error_message
|
134
135
|
raise Errors::APIRequestError.new(error_message, response.status, response.body)
|
135
136
|
end
|
136
137
|
|
@@ -144,8 +145,9 @@ class PiNetwork
|
|
144
145
|
end
|
145
146
|
|
146
147
|
def set_horizon_client(network)
|
147
|
-
host = network
|
148
|
-
horizon =
|
148
|
+
host = (network.starts_with? "Pi Network") ? @mainnet_host : @testnet_host
|
149
|
+
horizon = "https://#{host}"
|
150
|
+
|
149
151
|
client = Stellar::Client.new(host: host, horizon: horizon)
|
150
152
|
Stellar::default_network = network
|
151
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
|