Class: KondutoRuby

Inherits:
Object
  • Object
show all
Defined in:
lib/konduto-ruby.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_key, endpoint = 'https://api.konduto.com/v1') ⇒ KondutoRuby

Returns a new instance of KondutoRuby.



9
10
11
12
13
# File 'lib/konduto-ruby.rb', line 9

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_keyObject

Returns the value of attribute api_key.



7
8
9
# File 'lib/konduto-ruby.rb', line 7

def api_key
  @api_key
end

#endpointObject

Returns the value of attribute endpoint.



6
7
8
# File 'lib/konduto-ruby.rb', line 6

def endpoint
  @endpoint
end

#request_bodyObject

Returns the value of attribute request_body.



6
7
8
# File 'lib/konduto-ruby.rb', line 6

def request_body
  @request_body
end

#response_bodyObject

Returns the value of attribute response_body.



6
7
8
# File 'lib/konduto-ruby.rb', line 6

def response_body
  @response_body
end

Instance Method Details

#analyze(order) ⇒ Object



72
73
74
75
76
77
78
79
80
81
# File 'lib/konduto-ruby.rb', line 72

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

#get_order(order_id) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/konduto-ruby.rb', line 57

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



32
33
34
35
36
# File 'lib/konduto-ruby.rb', line 32

def order_url order_id=''
  url = "#{@endpoint}/orders"
  url += "/#{order_id}" unless order_id == ''
  URI.parse(url)
end

#proxyObject



23
24
25
26
# File 'lib/konduto-ruby.rb', line 23

def proxy
  return @proxy if @proxy
  URI.parse(ENV['http_proxy']) rescue nil
end

#proxy=(proxy_url) ⇒ Object



28
29
30
# File 'lib/konduto-ruby.rb', line 28

def proxy= proxy_url
  @proxy = URI.parse(proxy_url)
end

#send_request(http_method, request_body = '') ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/konduto-ruby.rb', line 38

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



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/konduto-ruby.rb', line 83

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