Module: Chemlab::Support::API

Included in:
ApiFabricator
Defined in:
lib/chemlab/support/api.rb

Constant Summary collapse

HTTP_STATUS_OK =
200
HTTP_STATUS_CREATED =
201
HTTP_STATUS_NO_CONTENT =
204
HTTP_STATUS_ACCEPTED =
202
HTTP_STATUS_SERVER_ERROR =
500

Instance Method Summary collapse

Instance Method Details

#delete(url) ⇒ Object



42
43
44
45
46
47
48
49
# File 'lib/chemlab/support/api.rb', line 42

def delete(url)
  RestClient::Request.execute(
    method: :delete,
    url: url,
    verify_ssl: false)
rescue RestClient::ExceptionWithResponse => e
  return_response_or_raise(e)
end

#get(url, raw_response: false) ⇒ Object



22
23
24
25
26
27
28
29
30
# File 'lib/chemlab/support/api.rb', line 22

def get(url, raw_response: false)
  RestClient::Request.execute(
    method: :get,
    url: url,
    verify_ssl: false,
    raw_response: raw_response)
rescue RestClient::ExceptionWithResponse => e
  return_response_or_raise(e)
end

#head(url) ⇒ Object



51
52
53
54
55
56
57
58
# File 'lib/chemlab/support/api.rb', line 51

def head(url)
  RestClient::Request.execute(
    method: :head,
    url: url,
    verify_ssl: false)
rescue RestClient::ExceptionWithResponse => e
  return_response_or_raise(e)
end

#parse_body(response) ⇒ Object



60
61
62
# File 'lib/chemlab/support/api.rb', line 60

def parse_body(response)
  JSON.parse(response.body, symbolize_names: true)
end

#post(url, payload) ⇒ Object



12
13
14
15
16
17
18
19
20
# File 'lib/chemlab/support/api.rb', line 12

def post(url, payload)
  RestClient::Request.execute(
    method: :post,
    url: url,
    payload: payload,
    verify_ssl: false)
rescue RestClient::ExceptionWithResponse => e
  return_response_or_raise(e)
end

#put(url, payload) ⇒ Object



32
33
34
35
36
37
38
39
40
# File 'lib/chemlab/support/api.rb', line 32

def put(url, payload)
  RestClient::Request.execute(
    method: :put,
    url: url,
    payload: payload,
    verify_ssl: false)
rescue RestClient::ExceptionWithResponse => e
  return_response_or_raise(e)
end

#return_response_or_raise(error) ⇒ Object



64
65
66
67
68
# File 'lib/chemlab/support/api.rb', line 64

def return_response_or_raise(error)
  raise error unless error.respond_to?(:response) && error.response

  error.response
end