Module: Xplenty::Kensa::HTTP

Included in:
DeprovisionCheck, PlanChangeCheck, SsoCheck
Defined in:
lib/xplenty/kensa/http.rb

Instance Method Summary collapse

Instance Method Details

#delete(credentials, path, payload = nil) ⇒ Object



20
21
22
# File 'lib/xplenty/kensa/http.rb', line 20

def delete(credentials, path, payload=nil)
  request(:delete, credentials, path, payload)
end

#get(path, params = {}) ⇒ Object



7
8
9
10
# File 'lib/xplenty/kensa/http.rb', line 7

def get(path, params={})
  path = "#{path}?" + params.map { |k, v| "#{k}=#{v}" }.join("&") unless params.empty?
  request(:get, [], path)
end

#post(credentials, path, payload = nil) ⇒ Object



12
13
14
# File 'lib/xplenty/kensa/http.rb', line 12

def post(credentials, path, payload=nil)
  request(:post, credentials, path, payload)
end

#put(credentials, path, payload = nil) ⇒ Object



16
17
18
# File 'lib/xplenty/kensa/http.rb', line 16

def put(credentials, path, payload=nil)
  request(:put, credentials, path, payload)
end

#request(meth, credentials, path, payload = nil) ⇒ Object



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
# File 'lib/xplenty/kensa/http.rb', line 24

def request(meth, credentials, path, payload=nil)
  code = nil
  body = nil

  begin
    args = [
      (OkJson.encode(payload) if payload),
      {
        :accept => "application/json",
        :content_type => "application/json"
      }
    ].compact

    user, pass = credentials
    body = RestClient::Resource.new(url, user, pass)[path].send(
      meth,
      *args
    ).to_s

    code = 200
  rescue RestClient::ExceptionWithResponse => boom
    code = boom.http_code
    body = boom.http_body
  rescue Errno::ECONNREFUSED
    code = -1
    body = nil
  end

  [code, body]
end