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.



15
16
17
18
19
# File 'lib/konduto-ruby.rb', line 15

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.



13
14
15
# File 'lib/konduto-ruby.rb', line 13

def api_key
  @api_key
end

#endpointObject

Returns the value of attribute endpoint.



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

def endpoint
  @endpoint
end

#request_bodyObject

Returns the value of attribute request_body.



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

def request_body
  @request_body
end

#response_bodyObject

Returns the value of attribute response_body.



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

def response_body
  @response_body
end

Instance Method Details

#analyze(order) ⇒ Object



78
79
80
81
82
83
84
85
86
87
# File 'lib/konduto-ruby.rb', line 78

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

#debugObject



107
108
109
# File 'lib/konduto-ruby.rb', line 107

def debug
  "API Key: #{@api_key}\nEndpoint: #{@endpoint}\n"
end

#get_order(order_id) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/konduto-ruby.rb', line 63

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



38
39
40
41
42
# File 'lib/konduto-ruby.rb', line 38

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

#proxyObject



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

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

#proxy=(proxy_url) ⇒ Object



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

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

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



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/konduto-ruby.rb', line 44

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



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/konduto-ruby.rb', line 89

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