Class: SettingsService
- Inherits:
-
Object
- Object
- SettingsService
- Defined in:
- lib/wingify/services/settings_service.rb
Class Attribute Summary collapse
-
.instance ⇒ Object
Returns the value of attribute instance.
Instance Attribute Summary collapse
-
#account_id ⇒ Object
Returns the value of attribute account_id.
-
#collection_prefix ⇒ Object
Returns the value of attribute collection_prefix.
-
#events_hostname ⇒ Object
Returns the value of attribute events_hostname.
-
#expiry ⇒ Object
Returns the value of attribute expiry.
-
#hostname ⇒ Object
Returns the value of attribute hostname.
-
#is_gateway_service_provided ⇒ Object
Returns the value of attribute is_gateway_service_provided.
-
#is_proxy_url_provided ⇒ Object
Returns the value of attribute is_proxy_url_provided.
-
#is_settings_valid ⇒ Object
Returns the value of attribute is_settings_valid.
-
#network_timeout ⇒ Object
Returns the value of attribute network_timeout.
-
#port ⇒ Object
Returns the value of attribute port.
-
#protocol ⇒ Object
Returns the value of attribute protocol.
-
#proxy_url ⇒ Object
Returns the value of attribute proxy_url.
-
#sdk_key ⇒ Object
Returns the value of attribute sdk_key.
-
#sdk_name ⇒ Object
Returns the value of attribute sdk_name.
-
#settings_fetch_time ⇒ Object
Returns the value of attribute settings_fetch_time.
Class Method Summary collapse
Instance Method Summary collapse
-
#fetch_settings(is_via_webhook = false) ⇒ SettingsModel
Fetch settings from the server.
-
#fetch_settings_and_cache_in_storage ⇒ SettingsModel
Fetch settings and cache them in storage.
-
#get_settings ⇒ SettingsModel
Get settings (either from storage or by forcing fetch).
- #get_updated_endpoint_with_collection_prefix(endpoint, is_gateway_provided = false) ⇒ Object
-
#initialize(options) ⇒ SettingsService
constructor
A new instance of SettingsService.
Constructor Details
#initialize(options) ⇒ SettingsService
Returns a new instance of SettingsService.
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/wingify/services/settings_service.rb', line 47 def initialize() @sdk_key = [:sdk_key] @account_id = [:account_id] @expiry = .dig(:settings, :expiry) || Constants::SETTINGS_EXPIRY @network_timeout = .dig(:settings, :timeout) || Constants::SETTINGS_TIMEOUT @is_settings_valid = false if [:gateway_service] && [:gateway_service][:url] parsed_url = URI.parse([:gateway_service][:url].start_with?(Constants::HTTP_PROTOCOL) || [:gateway_service][:url].start_with?(Constants::HTTPS_PROTOCOL) ? [:gateway_service][:url] : "#{Constants::HTTPS_PROTOCOL}#{[:gateway_service][:url]}") @hostname = parsed_url.hostname @events_hostname = parsed_url.hostname @sdk_name = BrandUtil.get_sdk_name(BrandContext.is_via_vwo?) @protocol = parsed_url.scheme @port = parsed_url.port || .dig(:gateway_service, :port) @is_gateway_service_provided = true if [:proxy_url] && ![:proxy_url].nil? && ![:proxy_url].empty? LoggerService.log(LogLevelEnum::INFO, "PROXY_AND_GATEWAY_SERVICE_PROVIDED") end elsif [:proxy_url] && ![:proxy_url].nil? && ![:proxy_url].empty? parsed_url = URI.parse([:proxy_url]) @hostname = parsed_url.hostname @events_hostname = parsed_url.hostname @sdk_name = BrandUtil.get_sdk_name(BrandContext.is_via_vwo?) @protocol = parsed_url.scheme @port = parsed_url.port @is_proxy_url_provided = true else is_via_vwo = BrandContext.is_via_vwo? @hostname = BrandUtil.get_settings_hostname(is_via_vwo) @events_hostname = BrandUtil.get_events_hostname(is_via_vwo) @sdk_name = BrandUtil.get_sdk_name(is_via_vwo) @protocol = Constants::HTTPS_PROTOCOL end LoggerService.log(LogLevelEnum::DEBUG, "SERVICE_INITIALIZED", { service: 'Settings Manager' }) SettingsService.instance = self end |
Class Attribute Details
.instance ⇒ Object
Returns the value of attribute instance.
33 34 35 |
# File 'lib/wingify/services/settings_service.rb', line 33 def instance @instance end |
Instance Attribute Details
#account_id ⇒ Object
Returns the value of attribute account_id.
30 31 32 |
# File 'lib/wingify/services/settings_service.rb', line 30 def account_id @account_id end |
#collection_prefix ⇒ Object
Returns the value of attribute collection_prefix.
30 31 32 |
# File 'lib/wingify/services/settings_service.rb', line 30 def collection_prefix @collection_prefix end |
#events_hostname ⇒ Object
Returns the value of attribute events_hostname.
30 31 32 |
# File 'lib/wingify/services/settings_service.rb', line 30 def events_hostname @events_hostname end |
#expiry ⇒ Object
Returns the value of attribute expiry.
30 31 32 |
# File 'lib/wingify/services/settings_service.rb', line 30 def expiry @expiry end |
#hostname ⇒ Object
Returns the value of attribute hostname.
30 31 32 |
# File 'lib/wingify/services/settings_service.rb', line 30 def hostname @hostname end |
#is_gateway_service_provided ⇒ Object
Returns the value of attribute is_gateway_service_provided.
30 31 32 |
# File 'lib/wingify/services/settings_service.rb', line 30 def is_gateway_service_provided @is_gateway_service_provided end |
#is_proxy_url_provided ⇒ Object
Returns the value of attribute is_proxy_url_provided.
30 31 32 |
# File 'lib/wingify/services/settings_service.rb', line 30 def is_proxy_url_provided @is_proxy_url_provided end |
#is_settings_valid ⇒ Object
Returns the value of attribute is_settings_valid.
30 31 32 |
# File 'lib/wingify/services/settings_service.rb', line 30 def is_settings_valid @is_settings_valid end |
#network_timeout ⇒ Object
Returns the value of attribute network_timeout.
30 31 32 |
# File 'lib/wingify/services/settings_service.rb', line 30 def network_timeout @network_timeout end |
#port ⇒ Object
Returns the value of attribute port.
30 31 32 |
# File 'lib/wingify/services/settings_service.rb', line 30 def port @port end |
#protocol ⇒ Object
Returns the value of attribute protocol.
30 31 32 |
# File 'lib/wingify/services/settings_service.rb', line 30 def protocol @protocol end |
#proxy_url ⇒ Object
Returns the value of attribute proxy_url.
30 31 32 |
# File 'lib/wingify/services/settings_service.rb', line 30 def proxy_url @proxy_url end |
#sdk_key ⇒ Object
Returns the value of attribute sdk_key.
30 31 32 |
# File 'lib/wingify/services/settings_service.rb', line 30 def sdk_key @sdk_key end |
#sdk_name ⇒ Object
Returns the value of attribute sdk_name.
30 31 32 |
# File 'lib/wingify/services/settings_service.rb', line 30 def sdk_name @sdk_name end |
#settings_fetch_time ⇒ Object
Returns the value of attribute settings_fetch_time.
30 31 32 |
# File 'lib/wingify/services/settings_service.rb', line 30 def settings_fetch_time @settings_fetch_time end |
Class Method Details
.get_instance ⇒ Object
35 36 37 |
# File 'lib/wingify/services/settings_service.rb', line 35 def get_instance @instance ||= SettingsService.new end |
.normalize_settings(settings) ⇒ Object
39 40 41 42 43 44 |
# File 'lib/wingify/services/settings_service.rb', line 39 def normalize_settings(settings) normalized_settings = settings.dup normalized_settings['features'] = [] if normalized_settings['features'].is_a?(Hash) && normalized_settings['features'].empty? normalized_settings['campaigns'] = [] if normalized_settings['campaigns'].is_a?(Hash) && normalized_settings['campaigns'].empty? normalized_settings end |
Instance Method Details
#fetch_settings(is_via_webhook = false) ⇒ SettingsModel
Fetch settings from the server.
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 |
# File 'lib/wingify/services/settings_service.rb', line 111 def fetch_settings(is_via_webhook = false) if @sdk_key.nil? || @account_id.nil? LoggerService.log(LogLevelEnum::ERROR, "INVALID_SDK_KEY_OR_ACCOUNT_ID", { an: ApiEnum::INIT}) end network_instance = NetworkManager.instance = NetworkUtil.get_settings_path(@sdk_key, @account_id) ['api-version'] = Constants::API_VERSION [:source] = 'prod' [:sn] = @sdk_name [:sv] = Constants::SDK_VERSION # When using gateway service, always fetch from SETTINGS_ENDPOINT since the gateway maintains the latest settings if @is_gateway_service_provided path = Constants::SETTINGS_ENDPOINT else path = is_via_webhook ? Constants::WEBHOOK_SETTINGS_ENDPOINT : Constants::SETTINGS_ENDPOINT end request = RequestModel.new(@hostname, "GET", path, , nil, nil, @protocol, @port) request.set_timeout(@network_timeout) # store the current time in milliseconds settings_fetch_start_time = (Time.now.to_f * 1000).to_i begin response = network_instance.get(request) # calculate the time taken to fetch the settings settings_fetch_end_time = (Time.now.to_f * 1000).to_i time_taken = settings_fetch_end_time - settings_fetch_start_time @settings_fetch_time = time_taken.to_s if response.get_total_attempts > 0 api_enum = is_via_webhook ? ApiEnum::UPDATE_SETTINGS : ApiEnum::INIT debug_event_props = NetworkUtil.create_network_and_retry_debug_event(response, nil, api_enum, path) # send debug event DebuggerServiceUtil.send_debugger_event(debug_event_props) end settings = response.get_data if settings.nil? || settings.empty? settings = {} end # Deep duplicate the settings to avoid modifying the original object normalized_settings = SettingsService.normalize_settings(settings) @collection_prefix = normalized_settings['collectionPrefix'] if normalized_settings['collectionPrefix'] && !normalized_settings['collectionPrefix'].empty? normalized_settings rescue => e LoggerService.log(LogLevelEnum::ERROR, "ERROR_FETCHING_SETTINGS", { err: e., an: ApiEnum::INIT}) {} end end |
#fetch_settings_and_cache_in_storage ⇒ SettingsModel
Fetch settings and cache them in storage.
98 99 100 101 102 103 104 105 106 |
# File 'lib/wingify/services/settings_service.rb', line 98 def fetch_settings_and_cache_in_storage begin response = fetch_settings response rescue => e LoggerService.log(LogLevelEnum::ERROR, "ERROR_FETCHING_SETTINGS", { err: e., an: ApiEnum::INIT}, false) {} end end |
#get_settings ⇒ SettingsModel
Get settings (either from storage or by forcing fetch).
166 167 168 169 170 171 172 173 174 175 176 177 |
# File 'lib/wingify/services/settings_service.rb', line 166 def get_settings settings = fetch_settings_and_cache_in_storage is_valid = SettingsSchema.new.is_settings_valid(settings) if is_valid @is_settings_valid = true LoggerService.log(LogLevelEnum::INFO, "SETTINGS_FETCH_SUCCESS") settings else LoggerService.log(LogLevelEnum::ERROR, "INVALID_SETTINGS_SCHEMA", { accountId: @account_id, sdkKey: @sdk_key, settings: settings, an: ApiEnum::INIT}, false) {} end end |
#get_updated_endpoint_with_collection_prefix(endpoint, is_gateway_provided = false) ⇒ Object
85 86 87 88 89 90 91 92 93 94 |
# File 'lib/wingify/services/settings_service.rb', line 85 def get_updated_endpoint_with_collection_prefix(endpoint, is_gateway_provided = false) if is_gateway_provided return endpoint end if @collection_prefix && !@collection_prefix.empty? return "/#{@collection_prefix}#{endpoint}" end return endpoint end |