Class: Deepgram::Base

Inherits:
Object
  • Object
show all
Includes:
ResponseHandler
Defined in:
lib/deepgram/base.rb

Overview

The Base class initializes the Faraday connection to interact with the Deepgram API and provides a generic request method for its subclasses.

Instance Method Summary collapse

Methods included from ResponseHandler

#handle_response

Constructor Details

#initialize(options = {}) ⇒ Base

Initializes a Faraday connection to the Deepgram API. The API endpoint and authorization token are set via environment variables, with defaults provided.

Parameters:

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

    Optional parameters for future extensions.



16
17
18
19
20
# File 'lib/deepgram/base.rb', line 16

def initialize(options = {})
  @connection = Faraday.new(url: ENV.fetch('DEEPGRAM_URL', 'https://api.deepgram.com'))
  @connection.headers['Authorization'] = "Token #{ENV.fetch('DEEPGRAM_API_KEY', 'api-key')}"
  @options = options
end

Instance Method Details

#request(method, path = nil, **kwargs) {|request| ... } ⇒ Faraday::Response

Sends an HTTP request to the specified path using the initialized Faraday connection. This method is designed to be flexible, allowing any HTTP method to be used.

Parameters:

  • method (Symbol)

    The HTTP method to use (:get, :post, etc.).

  • path (String, nil) (defaults to: nil)

    The path to append to the base API URL (optional).

  • kwargs (Hash)

    Additional keyword arguments to include in the request.

Yield Parameters:

  • request (Faraday::Request)

    The request object, allowing for further customization.

Returns:

  • (Faraday::Response)

    The response from the Faraday connection.



30
31
32
33
34
35
36
37
# File 'lib/deepgram/base.rb', line 30

def request(method, path = nil, **kwargs)
  res = @connection.send(method, path, **kwargs) do |request|
    yield(request) if block_given?
    request.params.merge!(kwargs)
  end

  handle_response(res)
end