Class: Rox::Core::StateSender
- Inherits:
-
Object
- Object
- Rox::Core::StateSender
- Defined in:
- lib/rox/core/network/state_sender.rb
Class Method Summary collapse
- .md5_signature(serialized_feature_flags, serialized_custom_properties, device_properties, dev_mode_secret) ⇒ Object
- .seralize_flag_repository(flag_repository) ⇒ Object
- .seralize_flag_repository_with_values(flag_repository) ⇒ Object
- .serialize_custom_properties_repository(custom_property_repository) ⇒ Object
- .serialize_custom_properties_repository_with_values(custom_property_repository) ⇒ Object
- .state_payload(seralized_flag_repository, serialized_custom_property_repository, device_properties, dev_mode_secret) ⇒ Object
Instance Method Summary collapse
- #delayed_send ⇒ Object
- #dump_state ⇒ Object
- #get_state_from_CDN(rollout_key, md5_signature) ⇒ Object
-
#initialize(sdk_settings, device_properties, flag_repository, custom_property_repository) ⇒ StateSender
constructor
A new instance of StateSender.
- #send ⇒ Object
- #send_state_to_API(rollout_key, md5_signature, state_payload) ⇒ Object
Constructor Details
#initialize(sdk_settings, device_properties, flag_repository, custom_property_repository) ⇒ StateSender
Returns a new instance of StateSender.
15 16 17 18 19 20 21 22 |
# File 'lib/rox/core/network/state_sender.rb', line 15 def initialize(sdk_settings, device_properties, flag_repository, custom_property_repository) @sdk_settings = sdk_settings @device_properties = device_properties @flag_repository = flag_repository @custom_property_repository = custom_property_repository @request = Request.new @debouncer = Debouncer.new(1, proc { send }) end |
Class Method Details
.md5_signature(serialized_feature_flags, serialized_custom_properties, device_properties, dev_mode_secret) ⇒ Object
76 77 78 79 80 81 82 |
# File 'lib/rox/core/network/state_sender.rb', line 76 def self.md5_signature(serialized_feature_flags, serialized_custom_properties, device_properties, dev_mode_secret) values = state_payload(serialized_feature_flags, serialized_custom_properties, device_properties, dev_mode_secret).values hash = Digest::MD5.hexdigest(values.join('|')) hash.upcase end |
.seralize_flag_repository(flag_repository) ⇒ Object
96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/rox/core/network/state_sender.rb', line 96 def self.seralize_flag_repository(flag_repository) flags = [] flag_repository.all_flags.sort_by(&:name).each do |f| flags << { name: f.name, defaultValue: f.default_value, options: f. } end flags end |
.seralize_flag_repository_with_values(flag_repository) ⇒ Object
108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/rox/core/network/state_sender.rb', line 108 def self.seralize_flag_repository_with_values(flag_repository) flags = [] flag_repository.all_flags.sort_by(&:name).each do |f| flags << { name: f.name, enabled: f.enabled?, defaultValue: f.default_value, options: f. } end flags end |
.serialize_custom_properties_repository(custom_property_repository) ⇒ Object
121 122 123 124 125 126 127 128 129 130 131 |
# File 'lib/rox/core/network/state_sender.rb', line 121 def self.serialize_custom_properties_repository(custom_property_repository) properties = [] custom_property_repository.all_custom_properties.sort_by(&:name).each do |p| properties << { name: p.name, type: p.type.type, externalType: p.type.external_type } end properties end |
.serialize_custom_properties_repository_with_values(custom_property_repository) ⇒ Object
133 134 135 136 137 138 139 140 141 142 143 144 |
# File 'lib/rox/core/network/state_sender.rb', line 133 def self.serialize_custom_properties_repository_with_values(custom_property_repository) properties = [] custom_property_repository.all_custom_properties.sort_by(&:name).each do |p| properties << { name: p.name, value: p.value(nil), type: p.type.type, externalType: p.type.external_type } end properties end |
.state_payload(seralized_flag_repository, serialized_custom_property_repository, device_properties, dev_mode_secret) ⇒ Object
84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/rox/core/network/state_sender.rb', line 84 def self.state_payload(seralized_flag_repository, serialized_custom_property_repository, device_properties, dev_mode_secret) values = {} # be careful adding here properties, md5 signature uses all props # to add more payload data, please change the md5_signature to use only the relevant properties (the current ones creating the payload) values[PropertyType::APP_KEY.name] = device_properties.all_properties[PropertyType::APP_KEY.name] values[PropertyType::PLATFORM.name] = device_properties.all_properties[PropertyType::PLATFORM.name] values.merge!({ feature_flags: seralized_flag_repository }) values.merge!({ custom_properties: serialized_custom_property_repository }) values['devModeSecret'] = dev_mode_secret values end |
Instance Method Details
#delayed_send ⇒ Object
24 25 26 |
# File 'lib/rox/core/network/state_sender.rb', line 24 def delayed_send @debouncer.call end |
#dump_state ⇒ Object
57 58 59 60 61 62 |
# File 'lib/rox/core/network/state_sender.rb', line 57 def dump_state serialized_feature_flags = StateSender.seralize_flag_repository_with_values(@flag_repository) serialized_custom_properties = StateSender.serialize_custom_properties_repository_with_values(@custom_property_repository) StateSender.state_payload(serialized_feature_flags, serialized_custom_properties, @device_properties, @sdk_settings.dev_mode_secret) end |
#get_state_from_CDN(rollout_key, md5_signature) ⇒ Object
64 65 66 67 68 |
# File 'lib/rox/core/network/state_sender.rb', line 64 def get_state_from_CDN(rollout_key, md5_signature) url_for_state_cdn = RequestData.new("#{Rox::Core::Environment.state_cdn_path}/#{rollout_key}/#{md5_signature}", {}) @request.send_get(url_for_state_cdn) end |
#send ⇒ Object
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 |
# File 'lib/rox/core/network/state_sender.rb', line 28 def send rollout_key = @device_properties.rollout_key serialized_feature_flags = StateSender.seralize_flag_repository(@flag_repository) serialized_custom_properties = StateSender.serialize_custom_properties_repository(@custom_property_repository) state_payload = StateSender.state_payload(serialized_feature_flags, serialized_custom_properties, @device_properties, @sdk_settings.dev_mode_secret) md5_signature = StateSender.md5_signature(serialized_feature_flags, serialized_custom_properties, @device_properties, @sdk_settings.dev_mode_secret) unless @device_properties..self_managed? response = get_state_from_CDN(rollout_key, md5_signature) if response.success? Rox::Core::Logging.logger.debug('Successfully fetched state from CDN') return else Rox::Core::Logging.logger.debug('Failed to fetch state from CDN. Sending state to API...') end end response = send_state_to_API(rollout_key, md5_signature, state_payload) = if response.success? 'Successfully sent state to API.' else 'Failed to send state to API.' end Rox::Core::Logging.logger.debug() end |
#send_state_to_API(rollout_key, md5_signature, state_payload) ⇒ Object
70 71 72 73 74 |
# File 'lib/rox/core/network/state_sender.rb', line 70 def send_state_to_API(rollout_key, md5_signature, state_payload) @request.send_post( "#{Rox::Core::Environment.state_api_path}/#{rollout_key}/#{md5_signature}", state_payload ) end |