Class: ServiceClient::Base

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

Overview

This class provides a lightweight and flexible way to make HTTP requests in Ruby projects.

This class can be used to make GET, POST, PUT, and DELETE requests to RESTful APIs. It simplifies the HTTP request process with a simple and intuitive API, configurable base URL, and default headers. It also provides a consistent and structured way to receive responses.

Class Method Summary collapse

Class Method Details

.base_url(url = nil) ⇒ Object

Sets the base URL to be used for all requests.

Parameters:

  • (defaults to: nil)

    the base URL to be used



25
26
27
# File 'lib/service_client/base.rb', line 25

def base_url(url = nil)
  @base_url = url
end

.default_headers(headers = nil) ⇒ Object

Sets the default headers to be sent with all requests.

Parameters:

  • (defaults to: nil)

    the default headers to be sent



32
33
34
# File 'lib/service_client/base.rb', line 32

def default_headers(headers = nil)
  @default_headers = headers
end

.delete(url = nil, headers: nil) ⇒ Object

Makes a DELETE request to the specified URL.

Parameters:

  • (defaults to: nil)

    the URL to make the request to

  • (defaults to: nil)

    additional headers to send with the request



66
67
68
# File 'lib/service_client/base.rb', line 66

def delete(url = nil, headers: nil)
  request(:delete, url, headers: headers)
end

.get(url = nil, headers: nil) ⇒ Object

Makes a GET request to the specified URL.

Parameters:

  • (defaults to: nil)

    the URL to make the request to

  • (defaults to: nil)

    additional headers to send with the request



49
50
51
# File 'lib/service_client/base.rb', line 49

def get(url = nil, headers: nil)
  request(:get, url, headers: headers)
end

.post(url = nil, headers: nil, body: nil) ⇒ Object

Makes a POST request to the specified URL.

Parameters:

  • (defaults to: nil)

    the URL to make the request to

  • (defaults to: nil)

    additional headers to send with the request

  • (defaults to: nil)

    the request body to send with the request



41
42
43
# File 'lib/service_client/base.rb', line 41

def post(url = nil, headers: nil, body: nil)
  request(:post, url, headers: headers, body: body)
end

.put(url = nil, headers: nil, body: nil) ⇒ Object

Makes a PUT request to the specified URL.

Parameters:

  • (defaults to: nil)

    the URL to make the request to

  • (defaults to: nil)

    additional headers to send with the request

  • (defaults to: nil)

    the request body to send with the request



58
59
60
# File 'lib/service_client/base.rb', line 58

def put(url = nil, headers: nil, body: nil)
  request(:put, url, headers: headers, body: body)
end