Class: HatiConfig::RemoteLoader

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

Overview

RemoteLoader handles loading configurations from remote sources like HTTP, S3, and Redis. It supports automatic refresh and caching of configurations.

Class Method Summary collapse

Class Method Details

.from_http(url:, headers: {}) ⇒ Hash

Loads configuration from an HTTP endpoint

Parameters:

  • url (String) —

    The URL to load the configuration from

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

    Optional headers to include in the request

  • refresh_interval (Integer) —

    Optional interval in seconds to refresh the configuration

Returns:

  • (Hash) —

    The loaded configuration

Raises:



21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/hati_config/remote_loader.rb', line 21

def from_http(url:, headers: {})
  uri = URI(url)
  request = Net::HTTP::Get.new(uri)
  headers.each { |key, value| request[key] = value }

  response = Net::HTTP.start(uri.hostname, uri.port, use_ssl: uri.scheme == 'https') do |http|
    http.request(request)
  end

  parse_response(response.body, File.extname(uri.path))
rescue StandardError => e
  raise LoadDataError, "Failed to load configuration from HTTP: #{e.message}"
end

.from_redis(host:, key:, port: 6379, db: 0) ⇒ Hash

Loads configuration from Redis

Parameters:

  • host (String) —

    The Redis host

  • key (String) —

    The Redis key

  • port (Integer) (defaults to: 6379) —

    The Redis port (default: 6379)

  • db (Integer) (defaults to: 0) —

    The Redis database number (default: 0)

  • refresh_interval (Integer) —

    Optional interval in seconds to refresh the configuration

Returns:

  • (Hash) —

    The loaded configuration

Raises:



60
61
62
63
64
65
66
67
68
# File 'lib/hati_config/remote_loader.rb', line 60

def from_redis(host:, key:, port: 6379, db: 0)
  redis = Redis.new(host: host, port: port, db: db)
  data = redis.get(key)
  raise LoadDataError, "Key '#{key}' not found in Redis" unless data

  parse_response(data)
rescue Redis::BaseError => e
  raise LoadDataError, "Failed to load configuration from Redis: #{e.message}"
end

.from_s3(bucket:, key:, region:) ⇒ Hash

Loads configuration from an S3 bucket

Parameters:

  • bucket (String) —

    The S3 bucket name

  • key (String) —

    The S3 object key

  • region (String) —

    The AWS region

  • refresh_interval (Integer) —

    Optional interval in seconds to refresh the configuration

Returns:

  • (Hash) —

    The loaded configuration

Raises:



43
44
45
46
47
48
49
# File 'lib/hati_config/remote_loader.rb', line 43

def from_s3(bucket:, key:, region:)
  s3 = Aws::S3::Client.new(region: region)
  response = s3.get_object(bucket: bucket, key: key)
  parse_response(response.body.read, File.extname(key))
rescue Aws::S3::Errors::ServiceError => e
  raise LoadDataError, "Failed to load configuration from S3: #{e.message}"
end