Repository navigation
Expand file tree
/
Copy pathtransport.rb
More file actions
82 lines (65 loc) · 2.02 KB
/
Copy pathtransport.rb
File metadata and controls
82 lines (65 loc) · 2.02 KB
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# frozen_string_literal: true
# Autogenerated — do not edit manually
require "json"
require "net/http"
require "openssl"
require "uri"
module Paystack
class Transport
BASE_URL = "https://api.paystack.co"
def initialize(api_key)
@api_key = api_key
@uri = URI(BASE_URL)
end
def get(path, query: {})
request(Net::HTTP::Get, path, query: query)
end
def post(path, body: {}, query: {})
request(Net::HTTP::Post, path, body: body, query: query)
end
def put(path, body: {}, query: {})
request(Net::HTTP::Put, path, body: body, query: query)
end
def patch(path, body: {}, query: {})
request(Net::HTTP::Patch, path, body: body, query: query)
end
def delete(path, body: {}, query: {})
request(Net::HTTP::Delete, path, body: body, query: query)
end
private
def request(klass, path, query: {}, body: {})
full_path = query.empty? ? path : "#{path}?#{URI.encode_www_form(query.compact)}"
Net::HTTP.start(@uri.host, @uri.port, **ssl_options) do |http|
req = klass.new(full_path)
req["Authorization"] = "Bearer #{@api_key}"
req["Content-Type"] = "application/json"
req["Accept"] = "application/json"
req["User-Agent"] = "paystack-ruby/#{Paystack::VERSION}"
req.body = body.to_json unless body.empty?
Response.new(http.request(req))
end
end
def ssl_options
{
use_ssl: true,
min_version: OpenSSL::SSL::TLS1_2_VERSION,
verify_mode: OpenSSL::SSL::VERIFY_PEER,
open_timeout: 10,
read_timeout: 30
}
end
def masked_api_key
visible_prefix = 8
visible_suffix = 4
return "[key too short to mask]" if @api_key.nil? || @api_key.length <= visible_prefix + visible_suffix
"#{@api_key[0, visible_prefix]}#{"*" * 8}#{@api_key[-visible_suffix, visible_suffix]}"
end
public
def inspect
"#<#{self.class.name} api_key=#{masked_api_key.inspect}>"
end
def to_s
inspect
end
end
end