Class: ActiveGenie::Providers::BaseProvider

Inherits:
Object
  • Object
show all
Defined in:
lib/active_genie/providers/base_provider.rb

Defined Under Namespace

Classes: ProviderUnknownError

Constant Summary collapse

DEFAULT_HEADERS =
{
  'Content-Type': 'application/json',
  Accept: 'application/json',
  'User-Agent': "ActiveGenie/#{ActiveGenie::VERSION}"
}.freeze
DEFAULT_TIMEOUT =

seconds

60
DEFAULT_OPEN_TIMEOUT =

seconds

10
DEFAULT_MAX_RETRIES =
3
DEFAULT_RETRY_DELAY =

seconds

1

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ BaseProvider

Returns a new instance of BaseProvider.



22
23
24
# File 'lib/active_genie/providers/base_provider.rb', line 22

def initialize(config)
  @config = config
end

Instance Method Details

#delete(endpoint, headers: {}, params: {}) ⇒ Hash?

Make a DELETE request to the specified endpoint

Parameters:

  • endpoint (String)

    The API endpoint to call

  • headers (Hash) (defaults to: {})

    Additional headers to include in the request

  • params (Hash) (defaults to: {})

    Query parameters for the request

Returns:

  • (Hash, nil)

    The parsed JSON response or nil if empty



73
74
75
76
77
78
# File 'lib/active_genie/providers/base_provider.rb', line 73

def delete(endpoint, headers: {}, params: {})
  uri = build_uri(endpoint, params)
  request = Net::HTTP::Delete.new(uri)
  apply_headers(request, headers)
  execute_request(uri, request)
end

#get(endpoint, params: {}, headers: {}) ⇒ Hash?

Make a GET request to the specified endpoint

Parameters:

  • endpoint (String)

    The API endpoint to call

  • headers (Hash) (defaults to: {})

    Additional headers to include in the request

  • params (Hash) (defaults to: {})

    Query parameters for the request

Returns:

  • (Hash, nil)

    The parsed JSON response or nil if empty



32
33
34
35
36
37
# File 'lib/active_genie/providers/base_provider.rb', line 32

def get(endpoint, params: {}, headers: {})
  uri = build_uri(endpoint, params)
  request = Net::HTTP::Get.new(uri)
  apply_headers(request, headers)
  execute_request(uri, request)
end

#post(endpoint, payload, params: {}, headers: {}) ⇒ Hash?

Make a POST request to the specified endpoint

Parameters:

  • endpoint (String)

    The API endpoint to call

  • payload (Hash)

    The request body to send

  • headers (Hash) (defaults to: {})

    Additional headers to include in the request

Returns:

  • (Hash, nil)

    The parsed JSON response or nil if empty



45
46
47
48
49
50
51
# File 'lib/active_genie/providers/base_provider.rb', line 45

def post(endpoint, payload, params: {}, headers: {})
  uri = build_uri(endpoint, params)
  request = Net::HTTP::Post.new(uri)
  request.body = payload.to_json
  apply_headers(request, headers)
  execute_request(uri, request)
end

#put(endpoint, payload, headers: {}) ⇒ Hash?

Make a PUT request to the specified endpoint

Parameters:

  • endpoint (String)

    The API endpoint to call

  • payload (Hash)

    The request body to send

  • headers (Hash) (defaults to: {})

    Additional headers to include in the request

Returns:

  • (Hash, nil)

    The parsed JSON response or nil if empty



59
60
61
62
63
64
65
# File 'lib/active_genie/providers/base_provider.rb', line 59

def put(endpoint, payload, headers: {})
  uri = build_uri(endpoint)
  request = Net::HTTP::Put.new(uri)
  request.body = payload.to_json
  apply_headers(request, headers)
  execute_request(uri, request)
end