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.



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

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.



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

def api_key
  @api_key
end

#endpointObject

Returns the value of attribute endpoint.



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

def endpoint
  @endpoint
end

#request_bodyObject

Returns the value of attribute request_body.



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

def request_body
  @request_body
end

#response_bodyObject

Returns the value of attribute response_body.



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

def response_body
  @response_body
end

Instance Method Details

#analyze(order) ⇒ Object



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

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



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

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



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

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

#proxyObject



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

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

#proxy=(proxy_url) ⇒ Object



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

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

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



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

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



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

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