Module: Conify::HTTPForTests

Included in:
ApiTest
Defined in:
lib/conify/http.rb

Instance Method Summary collapse

Instance Method Details

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



21
22
23
# File 'lib/conify/http.rb', line 21

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

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



8
9
10
11
# File 'lib/conify/http.rb', line 8

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



13
14
15
# File 'lib/conify/http.rb', line 13

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

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



17
18
19
# File 'lib/conify/http.rb', line 17

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

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



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/conify/http.rb', line 25

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

  begin
    args = [{ accept: 'application/json' }]

    if payload
      args.first[:content_type] = 'application/json'
      args.unshift(payload.to_json)
    end

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

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

  [code, body]
end