Class: Poodle::Configuration

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

Overview

Configuration class for Poodle SDK settings

Examples:

Basic configuration

config = Poodle::Configuration.new(api_key: "your_api_key")

Using environment variables

ENV["POODLE_API_KEY"] = "your_api_key"
config = Poodle::Configuration.new

Full configuration

config = Poodle::Configuration.new(
  api_key: "your_api_key",
  base_url: "https://api.usepoodle.com",
  timeout: 30,
  connect_timeout: 10,
  debug: true
)

Constant Summary collapse

DEFAULT_BASE_URL =

Default API base URL

"https://api.usepoodle.com"
DEFAULT_TIMEOUT =

Default timeout in seconds

30
DEFAULT_CONNECT_TIMEOUT =

Default connect timeout in seconds

10
MAX_CONTENT_SIZE =

Maximum content size in bytes (10MB)

10 * 1024 * 1024
SDK_VERSION =

SDK version

Poodle::VERSION

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(**options) ⇒ Configuration

Initialize a new Configuration

Parameters:

  • api_key (String, nil)

    API key (defaults to POODLE_API_KEY env var)

  • base_url (String, nil)

    Base URL (defaults to POODLE_BASE_URL env var or DEFAULT_BASE_URL)

  • timeout (Integer, nil)

    Request timeout (defaults to POODLE_TIMEOUT env var or DEFAULT_TIMEOUT)

  • connect_timeout (Integer, nil)

    Connect timeout (defaults to POODLE_CONNECT_TIMEOUT env var or DEFAULT_CONNECT_TIMEOUT)

  • debug (Boolean)

    Enable debug mode (defaults to POODLE_DEBUG env var or false)

  • http_options (Hash)

    Additional HTTP client options

Raises:

  • (ArgumentError)

    if api_key is missing or invalid

  • (ArgumentError)

    if base_url is invalid

  • (ArgumentError)

    if timeout values are invalid



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/poodle/configuration.rb', line 70

def initialize(**options)
  api_key = options[:api_key]
  base_url = options[:base_url]
  timeout = options[:timeout]
  connect_timeout = options[:connect_timeout]
  debug = options.fetch(:debug, false)
  http_options = options.fetch(:http_options, {})
  @api_key = api_key || ENV.fetch("POODLE_API_KEY", nil)
  @base_url = base_url || ENV["POODLE_BASE_URL"] || DEFAULT_BASE_URL
  @timeout = timeout || ENV.fetch("POODLE_TIMEOUT", DEFAULT_TIMEOUT).to_i
  @connect_timeout = connect_timeout || ENV.fetch("POODLE_CONNECT_TIMEOUT", DEFAULT_CONNECT_TIMEOUT).to_i
  @debug = debug || ENV["POODLE_DEBUG"] == "true"
  @http_options = http_options

  validate!
end

Instance Attribute Details

#api_keyString (readonly)

Returns the API key for authentication.

Returns:

  • (String)

    the API key for authentication



40
41
42
# File 'lib/poodle/configuration.rb', line 40

def api_key
  @api_key
end

#base_urlString (readonly)

Returns the base URL for the API.

Returns:

  • (String)

    the base URL for the API



43
44
45
# File 'lib/poodle/configuration.rb', line 43

def base_url
  @base_url
end

#connect_timeoutInteger (readonly)

Returns the connection timeout in seconds.

Returns:

  • (Integer)

    the connection timeout in seconds



49
50
51
# File 'lib/poodle/configuration.rb', line 49

def connect_timeout
  @connect_timeout
end

#debugBoolean (readonly)

Returns whether debug mode is enabled.

Returns:

  • (Boolean)

    whether debug mode is enabled



52
53
54
# File 'lib/poodle/configuration.rb', line 52

def debug
  @debug
end

#http_optionsHash (readonly)

Returns additional HTTP client options.

Returns:

  • (Hash)

    additional HTTP client options



55
56
57
# File 'lib/poodle/configuration.rb', line 55

def http_options
  @http_options
end

#timeoutInteger (readonly)

Returns the request timeout in seconds.

Returns:

  • (Integer)

    the request timeout in seconds



46
47
48
# File 'lib/poodle/configuration.rb', line 46

def timeout
  @timeout
end

Instance Method Details

#debug?Boolean

Check if debug mode is enabled

Returns:

  • (Boolean)

    true if debug mode is enabled



105
106
107
# File 'lib/poodle/configuration.rb', line 105

def debug?
  @debug
end

#url_for(endpoint) ⇒ String

Get the full URL for an endpoint

Parameters:

  • endpoint (String)

    the API endpoint

Returns:

  • (String)

    the full URL



98
99
100
# File 'lib/poodle/configuration.rb', line 98

def url_for(endpoint)
  "#{@base_url}/#{endpoint.gsub(%r{^/}, '')}"
end

#user_agentString

Get the User-Agent string for HTTP requests

Returns:

  • (String)

    the User-Agent string



90
91
92
# File 'lib/poodle/configuration.rb', line 90

def user_agent
  "poodle-ruby/#{SDK_VERSION} (Ruby #{RUBY_VERSION})"
end

#validate!Object (private)

Validate the configuration

Raises:

  • (ArgumentError)

    if any configuration is invalid



114
115
116
117
118
119
120
# File 'lib/poodle/configuration.rb', line 114

def validate!
  raise ArgumentError, "API key is required" if @api_key.nil? || @api_key.empty?

  validate_url!(@base_url, "base_url")
  validate_timeout!(@timeout, "timeout")
  validate_timeout!(@connect_timeout, "connect_timeout")
end

#validate_timeout!(value, field) ⇒ Object (private)

Validate a timeout value

Parameters:

  • value (Integer)

    the timeout value

  • field (String)

    the field name for error messages

Raises:

  • (ArgumentError)

    if the timeout is invalid



141
142
143
# File 'lib/poodle/configuration.rb', line 141

def validate_timeout!(value, field)
  raise ArgumentError, "#{field} must be a positive integer" unless value.is_a?(Integer) && value.positive?
end

#validate_url!(url, field) ⇒ Object (private)

Validate a URL

Parameters:

  • url (String)

    the URL to validate

  • field (String)

    the field name for error messages

Raises:

  • (ArgumentError)

    if the URL is invalid



127
128
129
130
131
132
133
134
# File 'lib/poodle/configuration.rb', line 127

def validate_url!(url, field)
  return if url.nil? || url.empty?

  uri = URI.parse(url)
  raise ArgumentError, "#{field} must be a valid HTTP or HTTPS URL" unless %w[http https].include?(uri.scheme)
rescue URI::InvalidURIError
  raise ArgumentError, "#{field} must be a valid URL"
end