Class: KondutoRuby
- Inherits:
-
Object
- Object
- KondutoRuby
- Defined in:
- lib/konduto-ruby.rb
Instance Attribute Summary collapse
-
#api_key ⇒ Object
Returns the value of attribute api_key.
-
#endpoint ⇒ Object
Returns the value of attribute endpoint.
-
#request_body ⇒ Object
Returns the value of attribute request_body.
-
#response_body ⇒ Object
Returns the value of attribute response_body.
Instance Method Summary collapse
- #analyze(order) ⇒ Object
- #debug ⇒ Object
- #get_order(order_id) ⇒ Object
-
#initialize(api_key, endpoint = 'https://api.konduto.com/v1') ⇒ KondutoRuby
constructor
A new instance of KondutoRuby.
- #order_url(order_id = '') ⇒ Object
- #proxy ⇒ Object
- #proxy=(proxy_url) ⇒ Object
- #send_request(http_method, request_body = '') ⇒ Object
- #update_order_status(order, new_status, comments) ⇒ Object
Constructor Details
#initialize(api_key, endpoint = 'https://api.konduto.com/v1') ⇒ KondutoRuby
Returns a new instance of KondutoRuby.
13 14 15 16 17 |
# File 'lib/konduto-ruby.rb', line 13 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 |
Instance Attribute Details
#api_key ⇒ Object
Returns the value of attribute api_key.
11 12 13 |
# File 'lib/konduto-ruby.rb', line 11 def api_key @api_key end |
#endpoint ⇒ Object
Returns the value of attribute endpoint.
10 11 12 |
# File 'lib/konduto-ruby.rb', line 10 def endpoint @endpoint end |
#request_body ⇒ Object
Returns the value of attribute request_body.
10 11 12 |
# File 'lib/konduto-ruby.rb', line 10 def request_body @request_body end |
#response_body ⇒ Object
Returns the value of attribute response_body.
10 11 12 |
# File 'lib/konduto-ruby.rb', line 10 def response_body @response_body end |
Instance Method Details
#analyze(order) ⇒ Object
76 77 78 79 80 81 82 83 84 85 |
# File 'lib/konduto-ruby.rb', line 76 def analyze order post = Net::HTTP::Post.new(order_url) response = send_request(post, order.to_json) if response.kind_of? Net::HTTPSuccess return KondutoOrder.new JSON.parse(response.entity)['order'] else raise (JSON.parse(response.body)['message']).to_s end end |
#debug ⇒ Object
105 106 107 |
# File 'lib/konduto-ruby.rb', line 105 def debug "API Key: #{@api_key}\nEndpoint: #{@endpoint}\n" end |
#get_order(order_id) ⇒ Object
61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/konduto-ruby.rb', line 61 def get_order order_id get = Net::HTTP::Get.new(order_url(order_id)) response = send_request(get) if response.kind_of? Net::HTTPSuccess order = KondutoOrder.new JSON.parse(response.entity)['order'] order.id = order_id unless order.id return order else raise (JSON.parse(response.body)['message']).to_s end end |
#order_url(order_id = '') ⇒ Object
36 37 38 39 40 |
# File 'lib/konduto-ruby.rb', line 36 def order_url order_id='' url = "#{@endpoint}/orders" url += "/#{order_id}" unless order_id == '' URI.parse(url) end |
#proxy ⇒ Object
27 28 29 30 |
# File 'lib/konduto-ruby.rb', line 27 def proxy return @proxy if @proxy URI.parse(ENV['http_proxy']) rescue nil end |
#proxy=(proxy_url) ⇒ Object
32 33 34 |
# File 'lib/konduto-ruby.rb', line 32 def proxy= proxy_url @proxy = URI.parse(proxy_url) end |
#send_request(http_method, request_body = '') ⇒ Object
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/konduto-ruby.rb', line 42 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 if proxy http = Net::HTTP::Proxy(proxy.host, proxy.port).new(@endpoint.host, @endpoint.port) else http = Net::HTTP.new(@endpoint.host, @endpoint.port) end http.use_ssl = true response = http.request(http_method) response end |
#update_order_status(order, new_status, comments) ⇒ Object
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/konduto-ruby.rb', line 87 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.kind_of? 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'] return order else raise (JSON.parse(response.body)['message']).to_s end end |