makitoo-feature_flag 0.1.3 → 0.1.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: fd95da8157bbdd2b50a43afb0867c3c8909f7daf
4
- data.tar.gz: 7d755e2c1e2a18daf57dd9005afe04599ea2290c
3
+ metadata.gz: 4c8f15caca683ce8f29ef9218fea9342e9062c43
4
+ data.tar.gz: d2a3faa6dfaaf67e28863807c568bb14b960d9dd
5
5
  SHA512:
6
- metadata.gz: 0fe9e466fceef62b8f18aeecbb367b87bf1106f4cf91a4fbe59e4e96a67f0eb2dd0fd9e8868e6262a7b5b546a7fdfe2303bd47101fd81cf86c1ecaf266c77a78
7
- data.tar.gz: 6fef9fb622338ee15e918542e4d72cc097d83871f995d7a55cf25293611e3e712337e2d75ac5b5c96ae8f8540d192994376fec1b4b167642432d26b84e5bb4ee
6
+ metadata.gz: 8d2d17cc00cfacd2c24412f24cef4f6222329da588419465d9259422e1ba52fcd9475bd9d8511405c367a2a04123229c9132473547bd42a79d9f70278d359902
7
+ data.tar.gz: 2421b2b96f28a729af04d48e0f9370a48f274936971c0040180b16cfed1e118e73d15f92fe5597080225f05641bc54c2cb3dc10956f47bbdd9862fa6ef4e7e59
@@ -1,5 +1,5 @@
1
1
  module Makitoo
2
2
  module FeatureFlag
3
- VERSION = "0.1.3"
3
+ VERSION = "0.1.4"
4
4
  end
5
5
  end
@@ -57,54 +57,53 @@ module Makitoo
57
57
  end
58
58
  end
59
59
 
60
- def is_active(feature_name, installation_id, default_state = false)
61
- feature_configuration = get_feature_configuration(feature_name, installation_id)
60
+ def is_active(feature_name, user_or_id, default_state = false)
61
+ user = User.of(user_or_id)
62
+ feature_configuration = get_feature_configuration(feature_name, user)
62
63
  is_visible = if feature_configuration.nil?
63
64
  default_state
64
65
  else
65
66
  feature_configuration[:state] == 'ON'
66
67
  end
67
68
  event = if is_visible
68
- create_seen_event(feature_name, installation_id)
69
+ create_seen_event(feature_name, user)
69
70
  else
70
- create_skip_event(feature_name, installation_id)
71
+ create_skip_event(feature_name, user)
71
72
  end
72
73
  send_event(event)
73
74
  is_visible
74
75
  end
75
76
 
76
- def success(feature_name, installation_id)
77
- send_event(create_success_event(feature_name, installation_id))
77
+ def success(feature_name, user_or_id)
78
+ user = User.of(user_or_id)
79
+ send_event(create_success_event(feature_name, user))
78
80
  end
79
81
 
80
- def track(event_name, installation_id, properties = {})
81
- send_event(create_track_event(event_name, installation_id, properties))
82
+ def track(event_name, user_or_id, properties = {})
83
+ user = User.of(user_or_id)
84
+ send_event(create_track_event(event_name, user, properties))
82
85
  end
83
86
 
84
- def init_segment(segment_id, makitoo_id)
87
+ def init_segment(segment_id, user_id)
85
88
  post_json(@server + '/associate-foreign-id', {
86
89
  foreignId: segment_id,
87
90
  source: 'segment.com',
88
- installationId: makitoo_id
91
+ installationId: user_id
89
92
  })
90
93
  end
91
94
 
92
- def user
93
- #TODO
94
- end
95
-
96
95
  private
97
96
 
98
- def refresh_feature_configurations_cache(installation_id)
99
- feature_configurations = send_event(create_refresh_event(installation_id))
100
- @cache.put('features.' + installation_id, feature_configurations)
97
+ def refresh_feature_configurations_cache(user)
98
+ feature_configurations = send_event(create_refresh_event(user))
99
+ @cache.put('features.' + user.id, feature_configurations)
101
100
  feature_configurations
102
101
  end
103
-
104
- def get_feature_configuration(feature_name, installation_id)
105
- feature_configurations = @cache.get('features.' + installation_id)
102
+
103
+ def get_feature_configuration(feature_name, user)
104
+ feature_configurations = @cache.get('features.' + user.id)
106
105
  if feature_configurations.nil?
107
- feature_configurations = refresh_feature_configurations_cache(installation_id)
106
+ feature_configurations = refresh_feature_configurations_cache(user)
108
107
  end
109
108
  if feature_configurations.nil?
110
109
  nil
@@ -113,42 +112,42 @@ module Makitoo
113
112
  end
114
113
  end
115
114
 
116
- def create_seen_event(feature_name, installation_id)
117
- create_execution_event(feature_name, 'SEEN', installation_id)
115
+ def create_seen_event(feature_name, user)
116
+ create_execution_event(feature_name, 'SEEN', user)
118
117
  end
119
118
 
120
- def create_skip_event(feature_name, installation_id)
121
- create_execution_event(feature_name, 'SKIP', installation_id)
119
+ def create_skip_event(feature_name, user)
120
+ create_execution_event(feature_name, 'SKIP', user)
122
121
  end
123
122
 
124
- def create_success_event(feature_name, installation_id)
125
- create_execution_event(feature_name, 'SUCCESS', installation_id)
123
+ def create_success_event(feature_name, user)
124
+ create_execution_event(feature_name, 'SUCCESS', user)
126
125
  end
127
126
 
128
- def create_execution_event(feature_name, event_type, installation_id)
129
- event = create_event(event_type, installation_id)
127
+ def create_execution_event(feature_name, event_type, user)
128
+ event = create_event(event_type, user)
130
129
  event[:featureName] = feature_name
131
130
  event
132
131
  end
133
132
 
134
- def create_track_event(event_name, installation_id, properties)
135
- event = create_event('TRACK', installation_id)
133
+ def create_track_event(event_name, user, properties)
134
+ event = create_event('TRACK', user)
136
135
  event[:eventName] = event_name
137
136
  event[:properties] = properties
138
137
  event
139
138
  end
140
139
 
141
- def create_refresh_event(installation_id)
142
- create_event('REFRESH', installation_id)
140
+ def create_refresh_event(user)
141
+ create_event('REFRESH', user)
143
142
  end
144
143
 
145
- def create_event(event_type, installation_id)
144
+ def create_event(event_type, user)
146
145
  {
147
146
  eventType: event_type,
148
147
  date: Time.now().iso8601,
149
148
  count: 1,
150
- installationId: installation_id,
151
- context: create_context,
149
+ installationId: user.id,
150
+ context: create_context(user),
152
151
  lib: {
153
152
  version: '0.0.1',
154
153
  type: 'ruby'
@@ -168,12 +167,12 @@ module Makitoo
168
167
  end
169
168
 
170
169
  def send_event(event)
171
- res = post_json(@server + '/event?' + URI.encode_www_form({
172
- application: @application_id,
173
- environmentName: @environment_name
174
- }), event)
175
- if !res.body.nil?
176
- begin
170
+ begin
171
+ res = post_json(@server + '/event?' + URI.encode_www_form({
172
+ application: @application_id,
173
+ environmentName: @environment_name
174
+ }), event)
175
+ if !res.body.nil?
177
176
  result = JSON.parse(res.body);
178
177
  feature_configurations = {}
179
178
  result['features'].each{|feature|
@@ -183,15 +182,40 @@ module Makitoo
183
182
  }
184
183
  }
185
184
  feature_configurations
186
- rescue
187
185
  end
186
+ rescue
188
187
  end
189
188
  end
190
189
 
191
- def create_context
192
- {}
190
+ def create_context(user)
191
+ {
192
+ user: user.as_json
193
+ }
193
194
  end
194
195
  end
195
196
 
197
+ class User
198
+
199
+ attr_accessor :id
200
+
201
+ def initialize(id, attributes = {})
202
+ @id = id
203
+ @attributes = attributes
204
+ end
205
+
206
+ def as_json(options = {})
207
+ result = @attributes.clone
208
+ result[:id] = @id
209
+ result
210
+ end
211
+
212
+ def self.of(string_or_user)
213
+ if string_or_user.is_a? String
214
+ User.new(string_or_user)
215
+ else
216
+ string_or_user
217
+ end
218
+ end
219
+ end
196
220
  end
197
221
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: makitoo-feature_flag
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 0.1.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Makitoo