Module: CtRegisterMicroservice::HTTPService

Defined in:
lib/ct_register_microservice/http_service.rb,
lib/ct_register_microservice/http_service/endpoint.rb,
lib/ct_register_microservice/http_service/response.rb

Defined Under Namespace

Classes: Endpoint, Response

Class Method Summary collapse

Class Method Details

.make_request(options, credentials = {}) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/ct_register_microservice/http_service.rb', line 10

def self.make_request(options, credentials = {})
  http_method = options.http_method&.to_sym || :get
  url = CtRegisterMicroservice.config.ct_url
  headers = options.headers || {}
  headers['Content-Type'] = 'application/json' if options.http_method == 'post' || options.http_method == 'put'
  endpoint = Endpoint.new(options, credentials).get
  body = options.body

  case http_method
  when :get
    response = HTTParty.get(url+endpoint, headers: headers)
  when :delete
    response = HTTParty.delete(url+endpoint, headers: headers)
  when :post
    response = HTTParty.post(url+endpoint, {
      headers: headers,
      body: body
    })
  when :put
    response = HTTParty.put(url+endpoint, {
      headers: headers,
      body: body
    })
  when :patch
    response = HTTParty.patch(url+endpoint, {
      headers: headers,
      body: body
    })
  end

  CtRegisterMicroservice::HTTPService::Response.new(response.code.to_i, response.body, response.headers)
end