Class: Conversant::Configuration

Inherits:
Object
  • Object
show all
Defined in:
lib/conversant/configuration.rb,
sig/conversant.rbs

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize ⇒ Configuration

Returns a new instance of Configuration.



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/conversant/configuration.rb', line 24

def initialize
  # All configuration automatically uses ENV variables as defaults
  # Users only need to override if they want different values

  # Portal configuration - defaults from ENV
  @portal_root_hostname = ENV['PORTAL_ROOT_HOSTNAME'] || 'console.flashcone.com'
  @portal_sso_hostname = ENV['PORTAL_SSO_HOSTNAME'] || 'sso2.flashcone.com'

  # API Endpoints - defaults from ENV (matching existing CONVERSANT setup)
  @private_cdn_endpoint = ENV['PRIVATE_CDN_ENDPOINT']
  @private_lms_endpoint = ENV['PRIVATE_LMS_ENDPOINT']
  @private_vms_endpoint = ENV['PRIVATE_VMS_ENDPOINT']
  @private_oss_endpoint = ENV['PRIVATE_OSS_ENDPOINT']
  @private_portal_endpoint = ENV['PRIVATE_PORTAL_ENDPOINT'] || "https://#{@portal_root_hostname}"
  @private_sso_endpoint = ENV['PRIVATE_SSO_ENDPOINT'] || "https://#{@portal_sso_hostname}"

  # Authentication - defaults from ENV
  @swiftserve_identifier_id = ENV['SWIFTSERVE_IDENTIFIER_ID']
  @swiftserve_identifier_hash = ENV['SWIFTSERVE_IDENTIFIER_HASH']

  # HTTP settings - defaults from ENV
  @default_ua = ENV['DEFAULT_UA'] || 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36'
  @default_content_type = ENV['DEFAULT_CONTENT_TYPE'] || 'application/json'

  # Additional CDN API URL if needed
  @conversant_cdn_api_url = ENV['CONVERSANT_CDN_API_URL']

  # Runtime settings
  @logger = Logger.new($stdout)
  @debug_mode = ENV['CONVERSANT_DEBUG'] == 'true' || ENV['DEBUG'] == 'true'
  @verify_ssl = ENV['RAILS_ENV'] == 'production'
  @cache_ttl = (ENV['CONVERSANT_CACHE_TTL'] || 1200).to_i

  # Redis - auto-detect from global $redis or ENV
  @redis = if defined?($redis) && $redis
             $redis
           elsif defined?(::Redis) && ENV['REDIS_URL']
             ::Redis.new(url: ENV['REDIS_URL'])
           elsif defined?(::Redis)
             ::Redis.new
           end
end

Instance Attribute Details

#cache_ttl ⇒ Integer

Returns the value of attribute cache_ttl.

Returns:

  • (Integer)


5
6
7
# File 'lib/conversant/configuration.rb', line 5

def cache_ttl
  @cache_ttl
end

#conversant_cdn_api_url ⇒ Object

Returns the value of attribute conversant_cdn_api_url.



5
6
7
# File 'lib/conversant/configuration.rb', line 5

def conversant_cdn_api_url
  @conversant_cdn_api_url
end

#debug_mode ⇒ Object

Returns the value of attribute debug_mode.



5
6
7
# File 'lib/conversant/configuration.rb', line 5

def debug_mode
  @debug_mode
end

#default_content_type ⇒ String

Returns the value of attribute default_content_type.

Returns:

  • (String)


5
6
7
# File 'lib/conversant/configuration.rb', line 5

def default_content_type
  @default_content_type
end

#default_ua ⇒ String

Returns the value of attribute default_ua.

Returns:

  • (String)


5
6
7
# File 'lib/conversant/configuration.rb', line 5

def default_ua
  @default_ua
end

#enable_logging ⇒ Boolean

Returns the value of attribute enable_logging.

Returns:

  • (Boolean)


18
19
20
# File 'sig/conversant.rbs', line 18

def enable_logging
  @enable_logging
end

#log_level ⇒ Symbol

Returns the value of attribute log_level.

Returns:

  • (Symbol)


19
20
21
# File 'sig/conversant.rbs', line 19

def log_level
  @log_level
end

#logger ⇒ Object

Returns the value of attribute logger.



5
6
7
# File 'lib/conversant/configuration.rb', line 5

def logger
  @logger
end

#portal_endpoint ⇒ String

Returns the value of attribute portal_endpoint.

Returns:

  • (String)


67
68
69
# File 'lib/conversant/configuration.rb', line 67

def portal_endpoint
  @private_portal_endpoint || "https://#{portal_root_hostname}"
end

#portal_root_hostname ⇒ Object

Returns the value of attribute portal_root_hostname.



5
6
7
# File 'lib/conversant/configuration.rb', line 5

def portal_root_hostname
  @portal_root_hostname
end

#portal_sso_hostname ⇒ Object

Returns the value of attribute portal_sso_hostname.



5
6
7
# File 'lib/conversant/configuration.rb', line 5

def portal_sso_hostname
  @portal_sso_hostname
end

#private_cdn_endpoint ⇒ String

Returns the value of attribute private_cdn_endpoint.

Returns:

  • (String)


5
6
7
# File 'lib/conversant/configuration.rb', line 5

def private_cdn_endpoint
  @private_cdn_endpoint
end

#private_lms_endpoint ⇒ String

Returns the value of attribute private_lms_endpoint.

Returns:

  • (String)


5
6
7
# File 'lib/conversant/configuration.rb', line 5

def private_lms_endpoint
  @private_lms_endpoint
end

#private_oss_endpoint ⇒ String

Returns the value of attribute private_oss_endpoint.

Returns:

  • (String)


5
6
7
# File 'lib/conversant/configuration.rb', line 5

def private_oss_endpoint
  @private_oss_endpoint
end

#private_portal_endpoint ⇒ Object

Returns the value of attribute private_portal_endpoint.



5
6
7
# File 'lib/conversant/configuration.rb', line 5

def private_portal_endpoint
  @private_portal_endpoint
end

#private_sso_endpoint ⇒ Object

Returns the value of attribute private_sso_endpoint.



5
6
7
# File 'lib/conversant/configuration.rb', line 5

def private_sso_endpoint
  @private_sso_endpoint
end

#private_vms_endpoint ⇒ String

Returns the value of attribute private_vms_endpoint.

Returns:

  • (String)


5
6
7
# File 'lib/conversant/configuration.rb', line 5

def private_vms_endpoint
  @private_vms_endpoint
end

#redis ⇒ Object

Returns the value of attribute redis.

Returns:

  • (Object)


5
6
7
# File 'lib/conversant/configuration.rb', line 5

def redis
  @redis
end

#swiftserve_identifier_hash ⇒ Object

Returns the value of attribute swiftserve_identifier_hash.



5
6
7
# File 'lib/conversant/configuration.rb', line 5

def swiftserve_identifier_hash
  @swiftserve_identifier_hash
end

#swiftserve_identifier_id ⇒ Object

Returns the value of attribute swiftserve_identifier_id.



5
6
7
# File 'lib/conversant/configuration.rb', line 5

def swiftserve_identifier_id
  @swiftserve_identifier_id
end

#verify_ssl ⇒ Object

Returns the value of attribute verify_ssl.



5
6
7
# File 'lib/conversant/configuration.rb', line 5

def verify_ssl
  @verify_ssl
end

Class Method Details

.from_env ⇒ Object

Load configuration from environment and validate



124
125
126
127
128
# File 'lib/conversant/configuration.rb', line 124

def self.from_env
  config = new
  config.validate!
  config
end

Instance Method Details

#auto_configured? ⇒ Boolean

Check if configuration is loaded from environment

Returns:

  • (Boolean)


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

def auto_configured?
  !@swiftserve_identifier_id.nil? && !@swiftserve_identifier_hash.nil?
end

#env_var_name(field) ⇒ Object

Helper to map config field to ENV variable name



110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/conversant/configuration.rb', line 110

def env_var_name(field)
  case field
  when :swiftserve_identifier_id then 'SWIFTSERVE_IDENTIFIER_ID'
  when :swiftserve_identifier_hash then 'SWIFTSERVE_IDENTIFIER_HASH'
  when :private_cdn_endpoint then 'PRIVATE_CDN_ENDPOINT'
  when :private_lms_endpoint then 'PRIVATE_LMS_ENDPOINT'
  when :private_vms_endpoint then 'PRIVATE_VMS_ENDPOINT'
  when :portal_root_hostname then 'PORTAL_ROOT_HOSTNAME'
  when :portal_sso_hostname then 'PORTAL_SSO_HOSTNAME'
  else field.to_s.upcase
  end
end

#sso_endpoint ⇒ Object



71
72
73
# File 'lib/conversant/configuration.rb', line 71

def sso_endpoint
  @private_sso_endpoint || "https://#{portal_sso_hostname}"
end

#validate! ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/conversant/configuration.rb', line 75

def validate!
  required_fields = []

  # Only require credentials if not auto-configured
  unless auto_configured?
    required_fields += %i[
      swiftserve_identifier_id
      swiftserve_identifier_hash
    ]
  end

  # Always require endpoints
  required_fields += %i[
    private_cdn_endpoint
    private_lms_endpoint
    private_vms_endpoint
    redis
  ]

  missing_fields = required_fields.select { |field| send(field).nil? || send(field).to_s.empty? }

  if missing_fields.any?
    raise Error, "Missing required configuration: #{missing_fields.join(', ')}\n" \
                 "Please set the following environment variables: #{missing_fields.map { |f| env_var_name(f) }.join(', ')}"
  end

  true
end