Class: ApiCaller

Inherits:
Object
  • Object
show all
Defined in:
lib/api_caller.rb

Class Method Summary collapse

Class Method Details

.delete(uri_string:, logger: nil) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/api_caller.rb', line 30

def self.delete(uri_string:, logger: nil)
  response = nil

  uri = escaped_uri(uri_string)
  begin
    Net::HTTP.start(uri.host, uri.port, {use_ssl: uri.scheme == 'https', open_timeout: 1}) do |http|
      request = Net::HTTP::Delete.new(uri)
      response = http.request(request)
    end
  rescue Exception => e
    logger.error("[API FAIL] - URI: [#{uri_string}] - Message: #{e.message}\n #{e.backtrace.join("\n ")}") if logger
  end

  response.is_a?(Net::HTTPSuccess) ? response.body&.force_encoding(Encoding::UTF_8) : nil
end

.get(uri_string:, logger: nil) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/api_caller.rb', line 5

def self.get(uri_string:, logger: nil)
  response = nil

  uri = escaped_uri(uri_string)

  begin
    Net::HTTP.start(uri.host, uri.port, {use_ssl: uri.scheme == 'https', open_timeout: 1}) do |http|
      request = Net::HTTP::Get.new(uri)
      response = http.request(request)
    end
  rescue Exception => e
    logger.error("[API FAIL] - URI: [#{uri_string}] - Message: #{e.message}\n #{e.backtrace.join("\n ")}") if logger
  end

  response.is_a?(Net::HTTPSuccess) ? response.body&.force_encoding(Encoding::UTF_8) : nil
end

.post(uri_string:, params:, content_type: nil, logger: nil) ⇒ Object



22
23
24
25
26
27
28
# File 'lib/api_caller.rb', line 22

def self.post(uri_string:, params:, content_type: nil, logger: nil)
  if content_type
    body_params_post(uri_string: uri_string, params: params, content_type: content_type, logger: logger)
  else
    url_params_post(uri_string: uri_string, params: params, logger: logger)
  end
end