forked from jonathan0211/konduto-ruby
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathkonduto-ruby.rb
More file actions
116 lines (96 loc) · 3.06 KB
/
Copy pathkonduto-ruby.rb
File metadata and controls
116 lines (96 loc) · 3.06 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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# frozen_string_literal: true
require 'json'
require 'date'
require_relative 'konduto-ruby/konduto_base'
require_relative 'konduto-ruby/konduto_travel_leg'
Dir["#{__dir__}/konduto-ruby/*.rb"].sort.each { |file| require file }
class KondutoRuby
require 'uri'
require 'base64'
require 'net/http'
attr_accessor :request_body, :response_body, :endpoint
attr_reader :api_key
VERSION = '2.2.0'
def initialize(api_key, endpoint = 'https://api.konduto.com/v1')
@endpoint = URI.parse(endpoint)
@http_client = Net::HTTP.new(@endpoint.host, @endpoint.port)
@api_key = api_key
end
def api_key=(api_key)
if api_key.length == 21
@api_key = api_key
else
raise ArgumentError, "Invalid key: #{api_key}! API key length must be of 21"
end
end
def proxy
return @proxy if @proxy
begin
URI.parse(ENV['http_proxy'])
rescue StandardError
nil
end
end
def proxy=(proxy_url)
@proxy = URI.parse(proxy_url)
end
def order_url(order_id = '')
url = "#{@endpoint}/orders"
url += "/#{order_id}" unless order_id == ''
URI.parse(url)
end
def send_request(http_method, request_body = '')
headers = {
'Authorization' => "Basic #{Base64.encode64(@api_key)}",
'Content-Type' => 'application/json',
'Referer' => @endpoint.path
}
http_method.initialize_http_header headers
http_method.body = request_body
http = if proxy
Net::HTTP::Proxy(proxy.host, proxy.port).new(@endpoint.host, @endpoint.port)
else
Net::HTTP.new(@endpoint.host, @endpoint.port)
end
http.use_ssl = true
http.request(http_method)
end
def get_order(order_id)
get = Net::HTTP::Get.new(order_url(order_id))
response = send_request(get)
if response.is_a? Net::HTTPSuccess
order = KondutoOrder.new JSON.parse(response.entity)['order']
order.id = order_id unless order.id
order
else
raise JSON.parse(response.body)['message'].to_s
end
end
def analyze(order)
post = Net::HTTP::Post.new(order_url)
response = send_request(post, order.to_json)
if response.is_a? Net::HTTPSuccess
KondutoOrder.new JSON.parse(response.entity)['order']
else
raise JSON.parse(response.body)['message'].to_s
end
end
def update_order_status(order, new_status, comments)
raise ArgumentError("Illegal status #{new_status}") unless KondutoOrderStatus.allowed_status.include? new_status
raise ArgumentError("Commets can't be nil") unless comments
put = Net::HTTP::Put.new(order_url(order.id))
body = { status: new_status.downcase, comments: comments }.to_json
response = send_request(put, body)
if response.is_a? Net::HTTPSuccess
resposta = JSON.parse(response.entity)['order']
raise 'Update order status can\'t be done' if resposta['old_status'].nil? && resposta['new_status'].nil?
order.status = resposta['new_status']
order
else
raise JSON.parse(response.body)['message'].to_s
end
end
def debug
"API Key: #{@api_key}\nEndpoint: #{@endpoint}\n"
end
end