-
Notifications
You must be signed in to change notification settings - Fork 11
/
payge.rb
68 lines (58 loc) · 1.47 KB
/
payge.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
require 'uri'
require 'net/http'
require 'json'
class Payge
attr_reader :secret_key, :major_version, :minor_version
def initialize(secret_key, major_version, minor_version)
@secret_key = secret_key
@major_version = major_version
@minor_version = minor_version
end
def list
uri = URI.parse("#{base_url}/api/#{@major_version}/payments")
request(uri) do |http|
http.get(uri.path, header)
end
end
def get(id)
uri = URI.parse("#{base_url}/api/#{@major_version}/payments/#{id}")
request(uri) do |http|
http.get(uri.path, header)
end
end
def create(params = {})
uri = URI.parse("#{base_url}/api/#{@major_version}/payments")
request(uri) do |http|
http.post(uri.path, params.to_json, header)
end
end
def update(id, params = {})
uri = URI.parse("#{base_url}/api/#{@major_version}/payments/#{id}")
request(uri) do |http|
http.patch(uri.path, params.to_json, header)
end
end
private
def request(uri, &block)
response = Net::HTTP.start(uri.host, uri.port, use_ssl: true) do |http|
block.call(http)
end
case response
when Net::HTTPSuccess
JSON.parse(response.body)
else
response.value
end
end
def base_url
'https://api.coiney.io'
end
def header
{
'Authorization' => "Bearer #{@secret_key}",
'X-CoineyPayge-Version' => @minor_version,
'Accept' => 'application/json',
'Content-Type' => 'application/json'
}
end
end