makitoo-feature_flag 0.1.4 → 0.1.5

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: 4c8f15caca683ce8f29ef9218fea9342e9062c43
4
- data.tar.gz: d2a3faa6dfaaf67e28863807c568bb14b960d9dd
3
+ metadata.gz: 55f3f5c59ac511b1cac242157228bd8254c0b285
4
+ data.tar.gz: 265f040a3925b6f24f45ec43f7976344245adaec
5
5
  SHA512:
6
- metadata.gz: 8d2d17cc00cfacd2c24412f24cef4f6222329da588419465d9259422e1ba52fcd9475bd9d8511405c367a2a04123229c9132473547bd42a79d9f70278d359902
7
- data.tar.gz: 2421b2b96f28a729af04d48e0f9370a48f274936971c0040180b16cfed1e118e73d15f92fe5597080225f05641bc54c2cb3dc10956f47bbdd9862fa6ef4e7e59
6
+ metadata.gz: eb8bd5d930cff3593965d0bb48eb9238d5c1ec5519faacb3c3fe8b67914f58bb8675702b482c9dd4e213eaf28a9d3f97150fa10cd4e139dc45edae8fba578e34
7
+ data.tar.gz: f1189fb213298ce8d369c8bdfe5bd5a631a8b290108af398e75c8b6b17b54d1cb8fd63d9b30e5c14f596755cb90ad335f0a0a8f1501e8056e7f3c3ca20e1ba35
data/.gitignore CHANGED
@@ -1,9 +1,9 @@
1
- /.bundle/
2
- /.yardoc
3
- /Gemfile.lock
4
- /_yardoc/
5
- /coverage/
6
- /doc/
7
- /pkg/
8
- /spec/reports/
9
- /tmp/
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.rspec CHANGED
@@ -1,2 +1,2 @@
1
- --format documentation
2
- --color
1
+ --format documentation
2
+ --color
data/.travis.yml CHANGED
@@ -1,5 +1,5 @@
1
- sudo: false
2
- language: ruby
3
- rvm:
4
- - 2.2.6
5
- before_install: gem install bundler -v 1.13.7
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.2.6
5
+ before_install: gem install bundler -v 1.13.7
data/Gemfile CHANGED
@@ -1,4 +1,4 @@
1
- source 'https://rubygems.org'
2
-
3
- # Specify your gem's dependencies in makitoo-feature_flag.gemspec
4
- gemspec
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in makitoo-feature_flag.gemspec
4
+ gemspec
data/LICENSE.txt CHANGED
@@ -1,21 +1,21 @@
1
- The MIT License (MIT)
2
-
3
- Copyright (c) 2017 Cy Brown
4
-
5
- Permission is hereby granted, free of charge, to any person obtaining a copy
6
- of this software and associated documentation files (the "Software"), to deal
7
- in the Software without restriction, including without limitation the rights
8
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
- copies of the Software, and to permit persons to whom the Software is
10
- furnished to do so, subject to the following conditions:
11
-
12
- The above copyright notice and this permission notice shall be included in
13
- all copies or substantial portions of the Software.
14
-
15
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
- THE SOFTWARE.
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 Cy Brown
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md CHANGED
@@ -1,41 +1,41 @@
1
- # Makitoo::FeatureFlag
2
-
3
- Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/makitoo/feature_flag`. To experiment with that code, run `bin/console` for an interactive prompt.
4
-
5
- TODO: Delete this and the text above, and describe your gem
6
-
7
- ## Installation
8
-
9
- Add this line to your application's Gemfile:
10
-
11
- ```ruby
12
- gem 'makitoo-feature_flag'
13
- ```
14
-
15
- And then execute:
16
-
17
- $ bundle
18
-
19
- Or install it yourself as:
20
-
21
- $ gem install makitoo-feature_flag
22
-
23
- ## Usage
24
-
25
- TODO: Write usage instructions here
26
-
27
- ## Development
28
-
29
- After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
30
-
31
- To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
32
-
33
- ## Contributing
34
-
35
- Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/makitoo-feature_flag.
36
-
37
-
38
- ## License
39
-
40
- The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
41
-
1
+ # Makitoo::FeatureFlag
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/makitoo/feature_flag`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'makitoo-feature_flag'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install makitoo-feature_flag
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
30
+
31
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
32
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/makitoo-feature_flag.
36
+
37
+
38
+ ## License
39
+
40
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
41
+
data/Rakefile CHANGED
@@ -1,6 +1,6 @@
1
- require "bundler/gem_tasks"
2
- require "rspec/core/rake_task"
3
-
4
- RSpec::Core::RakeTask.new(:spec)
5
-
6
- task :default => :spec
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console CHANGED
@@ -1,14 +1,14 @@
1
- #!/usr/bin/env ruby
2
-
3
- require "bundler/setup"
4
- require "makitoo/feature_flag"
5
-
6
- # You can add fixtures and/or initialization code here to make experimenting
7
- # with your gem easier. You can also use a different console, if you like.
8
-
9
- # (If you use this, don't forget to add pry to your Gemfile!)
10
- # require "pry"
11
- # Pry.start
12
-
13
- require "irb"
14
- IRB.start
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "makitoo/feature_flag"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/setup CHANGED
@@ -1,8 +1,8 @@
1
- #!/usr/bin/env bash
2
- set -euo pipefail
3
- IFS=$'\n\t'
4
- set -vx
5
-
6
- bundle install
7
-
8
- # Do any other automated setup that you need to do here
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,37 @@
1
+ module Makitoo
2
+ module FeatureFlag
3
+
4
+ class MemoryCache
5
+
6
+ def initialize(logger, ttl = 20.seconds)
7
+ @logger = logger
8
+ @cache = {}
9
+ @ttl = ttl
10
+ end
11
+
12
+ def get(key)
13
+ if @cache.has_key?(key)
14
+ if @cache[key][:end] > Time.now()
15
+ @logger.debug 'Cache hit'
16
+ @cache[key][:value]
17
+ else
18
+ @logger.debug 'Cache invalid'
19
+ @cache.delete(key)
20
+ nil
21
+ end
22
+ else
23
+ @logger.debug 'Cache miss'
24
+ nil
25
+ end
26
+ end
27
+
28
+ def put(key, value)
29
+ @logger.debug 'Cache put'
30
+ @cache[key] = {
31
+ value: value,
32
+ end: Time.now() + @ttl
33
+ }
34
+ end
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,28 @@
1
+ module Makitoo
2
+ module FeatureFlag
3
+
4
+ class User
5
+
6
+ attr_accessor :id
7
+
8
+ def initialize(id, attributes = {})
9
+ @id = id
10
+ @attributes = attributes
11
+ end
12
+
13
+ def as_json(options = {})
14
+ result = @attributes.clone
15
+ result[:id] = @id
16
+ result
17
+ end
18
+
19
+ def self.of(string_or_user)
20
+ if string_or_user.is_a? String
21
+ User.new(string_or_user)
22
+ else
23
+ string_or_user
24
+ end
25
+ end
26
+ end
27
+ end
28
+ end
@@ -1,5 +1,5 @@
1
- module Makitoo
2
- module FeatureFlag
3
- VERSION = "0.1.4"
4
- end
5
- end
1
+ module Makitoo
2
+ module FeatureFlag
3
+ VERSION = "0.1.5"
4
+ end
5
+ end
@@ -1,221 +1,226 @@
1
- require 'net/http'
2
- require 'time'
3
- require 'json'
4
- require "makitoo/feature_flag/version"
5
-
6
- module Makitoo
7
- module FeatureFlag
8
-
9
- class MemoryCache
10
-
11
- def initialize(ttl = 20.seconds)
12
- @cache = {}
13
- @ttl = ttl
14
- end
15
-
16
- def get(key)
17
- if @cache.has_key?(key)
18
- if @cache[key][:end] > Time.now()
19
- @cache[key][:value]
20
- else
21
- @cache.delete(key)
22
- nil
23
- end
24
- else
25
- nil
26
- end
27
- end
28
-
29
- def put(key, value)
30
- @cache[key] = {
31
- value: value,
32
- end: Time.now() + @ttl
33
- }
34
- end
35
- end
36
-
37
- class Client
38
-
39
- def initialize(application_id, options = {})
40
- @application_id = application_id
41
- @server = if options.has_key?(:server)
42
- options[:server]
43
- elsif options[:ssl]
44
- 'https://features.makitoo.com/api/v1'
45
- else
46
- 'http://features.makitoo.com/api/v1'
47
- end
48
- @cache = if options.has_key?(:cache)
49
- options[:cache]
50
- else
51
- MemoryCache.new
52
- end
53
- @environment_name = if options.has_key?(:environment_name)
54
- options[:environment_name]
55
- else
56
- 'production'
57
- end
58
- end
59
-
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)
63
- is_visible = if feature_configuration.nil?
64
- default_state
65
- else
66
- feature_configuration[:state] == 'ON'
67
- end
68
- event = if is_visible
69
- create_seen_event(feature_name, user)
70
- else
71
- create_skip_event(feature_name, user)
72
- end
73
- send_event(event)
74
- is_visible
75
- end
76
-
77
- def success(feature_name, user_or_id)
78
- user = User.of(user_or_id)
79
- send_event(create_success_event(feature_name, user))
80
- end
81
-
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))
85
- end
86
-
87
- def init_segment(segment_id, user_id)
88
- post_json(@server + '/associate-foreign-id', {
89
- foreignId: segment_id,
90
- source: 'segment.com',
91
- installationId: user_id
92
- })
93
- end
94
-
95
- private
96
-
97
- def refresh_feature_configurations_cache(user)
98
- feature_configurations = send_event(create_refresh_event(user))
99
- @cache.put('features.' + user.id, feature_configurations)
100
- feature_configurations
101
- end
102
-
103
- def get_feature_configuration(feature_name, user)
104
- feature_configurations = @cache.get('features.' + user.id)
105
- if feature_configurations.nil?
106
- feature_configurations = refresh_feature_configurations_cache(user)
107
- end
108
- if feature_configurations.nil?
109
- nil
110
- else
111
- feature_configurations[feature_name]
112
- end
113
- end
114
-
115
- def create_seen_event(feature_name, user)
116
- create_execution_event(feature_name, 'SEEN', user)
117
- end
118
-
119
- def create_skip_event(feature_name, user)
120
- create_execution_event(feature_name, 'SKIP', user)
121
- end
122
-
123
- def create_success_event(feature_name, user)
124
- create_execution_event(feature_name, 'SUCCESS', user)
125
- end
126
-
127
- def create_execution_event(feature_name, event_type, user)
128
- event = create_event(event_type, user)
129
- event[:featureName] = feature_name
130
- event
131
- end
132
-
133
- def create_track_event(event_name, user, properties)
134
- event = create_event('TRACK', user)
135
- event[:eventName] = event_name
136
- event[:properties] = properties
137
- event
138
- end
139
-
140
- def create_refresh_event(user)
141
- create_event('REFRESH', user)
142
- end
143
-
144
- def create_event(event_type, user)
145
- {
146
- eventType: event_type,
147
- date: Time.now().iso8601,
148
- count: 1,
149
- installationId: user.id,
150
- context: create_context(user),
151
- lib: {
152
- version: '0.0.1',
153
- type: 'ruby'
154
- }
155
- }
156
- end
157
-
158
- def post_json(url_str, data)
159
- puts url_str
160
- url = URI.parse(url_str)
161
- req = Net::HTTP::Post.new(url.to_s, 'Content-Type' => 'application/json')
162
- req.body = data.to_json
163
- res = Net::HTTP.start(url.host, url.port) {|http|
164
- http.request(req)
165
- }
166
- res
167
- end
168
-
169
- def send_event(event)
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?
176
- result = JSON.parse(res.body);
177
- feature_configurations = {}
178
- result['features'].each{|feature|
179
- feature_configurations[feature['name']] = {
180
- name: feature['name'],
181
- state: feature['state']
182
- }
183
- }
184
- feature_configurations
185
- end
186
- rescue
187
- end
188
- end
189
-
190
- def create_context(user)
191
- {
192
- user: user.as_json
193
- }
194
- end
195
- end
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
220
- end
221
- end
1
+ require 'net/http'
2
+ require 'time'
3
+ require 'json'
4
+ require 'makitoo/feature_flag/version'
5
+ require 'makitoo/feature_flag/memory_cache'
6
+ require 'makitoo/feature_flag/user'
7
+
8
+ module Makitoo
9
+ module FeatureFlag
10
+
11
+ class Client
12
+
13
+ def initialize(application_id, options = {})
14
+ @logger = if options.has_key?(:logger)
15
+ options[:logger]
16
+ elsif defined?(Rails)
17
+ Rails.logger
18
+ else
19
+ logger = Logger.new STDOUT
20
+ logger.progname = 'Makitoo::FeatureFlag'
21
+ logger
22
+ end
23
+ @logger.debug 'Initializing makitoo feature flag'
24
+ @application_id = application_id
25
+ @server = if options.has_key?(:server)
26
+ options[:server]
27
+ elsif options[:ssl]
28
+ 'https://features.makitoo.com/api/v1'
29
+ else
30
+ 'http://features.makitoo.com/api/v1'
31
+ end
32
+ @cache = options[:cache] || MemoryCache.new @logger
33
+ @environment_name = options[:environment_name] || 'production'
34
+ @concurrency = options[:concurrency] || 1
35
+ @force_sync = options[:force_sync] || @concurrency < 1
36
+ start_background_threads
37
+ @logger.debug 'Makitoo initialized'
38
+ end
39
+
40
+ def is_active(feature_name, user_or_id, default_state = false)
41
+ @logger.debug 'Start is_active'
42
+ feature_configuration = get_feature_configuration(feature_name, User.of(user_or_id))
43
+ is_visible = if feature_configuration.nil?
44
+ default_state
45
+ else
46
+ feature_configuration[:state] == 'ON'
47
+ end
48
+ event = if is_visible
49
+ create_seen_event(feature_name, user)
50
+ else
51
+ create_skip_event(feature_name, user)
52
+ end
53
+ send_event(event)
54
+ @logger.debug 'Stop is_active'
55
+ is_visible
56
+ end
57
+
58
+ def success(feature_name, user_or_id)
59
+ @logger.debug 'Start is_active'
60
+ send_event(create_success_event(feature_name, User.of(user_or_id)))
61
+ @logger.debug 'Stop success'
62
+ end
63
+
64
+ def track(event_name, user_or_id, properties = {})
65
+ @logger.debug 'Start is_active'
66
+ send_event(create_track_event(event_name, User.of(user_or_id), properties))
67
+ @logger.debug 'Stop track'
68
+ end
69
+
70
+ def init_segment(segment_id, user_or_id)
71
+ @logger.debug 'Start init_segment'
72
+ post_json(@server + '/associate-foreign-id', {
73
+ foreignId: segment_id,
74
+ source: 'segment.com',
75
+ installationId: User.of(user_or_id).id
76
+ })
77
+ @logger.debug 'Stop init_segment'
78
+ end
79
+
80
+ private
81
+
82
+ def start_background_threads
83
+ if !@force_sync
84
+ @queue = Queue.new
85
+ @logger.debug 'Background threads starting'
86
+ (1..@concurrency).each do
87
+ Thread.new do
88
+ @logger.debug 'Background thread started'
89
+ begin
90
+ loop do
91
+ begin
92
+ @logger.debug 'Async request waiting for task'
93
+ val = @queue.pop
94
+ @logger.debug 'Async request start'
95
+ res = Net::HTTP.start(val[:url].host, val[:url].port, use_ssl: val[:url].scheme == 'https') {|http|
96
+ http.request(val[:req])
97
+ }
98
+ @logger.debug 'Async request end'
99
+ rescue Exception => e
100
+ @logger.debug 'Error !!!'
101
+ log_error(e)
102
+ end
103
+ end
104
+ ensure
105
+ @logger.debug 'Background thread stopped'
106
+ end
107
+ end
108
+ end
109
+ end
110
+ end
111
+
112
+ def log_error(e)
113
+ @logger.error 'Exception'
114
+ @logger.error e.message
115
+ e.backtrace.each { |line| @logger.error line }
116
+ end
117
+
118
+ def refresh_feature_configurations_cache(user)
119
+ feature_configurations = send_event(create_refresh_event(user), false)
120
+ @cache.put('features.' + user.id, feature_configurations)
121
+ feature_configurations
122
+ end
123
+
124
+ def get_feature_configuration(feature_name, user)
125
+ feature_configurations = @cache.get('features.' + user.id)
126
+ if feature_configurations.nil?
127
+ feature_configurations = refresh_feature_configurations_cache(user)
128
+ end
129
+ if feature_configurations.nil?
130
+ nil
131
+ else
132
+ feature_configurations[feature_name]
133
+ end
134
+ end
135
+
136
+ def create_seen_event(feature_name, user)
137
+ create_execution_event(feature_name, 'SEEN', user)
138
+ end
139
+
140
+ def create_skip_event(feature_name, user)
141
+ create_execution_event(feature_name, 'SKIP', user)
142
+ end
143
+
144
+ def create_success_event(feature_name, user)
145
+ create_execution_event(feature_name, 'SUCCESS', user)
146
+ end
147
+
148
+ def create_execution_event(feature_name, event_type, user)
149
+ event = create_event(event_type, user)
150
+ event[:featureName] = feature_name
151
+ event
152
+ end
153
+
154
+ def create_track_event(event_name, user, properties)
155
+ event = create_event('TRACK', user)
156
+ event[:eventName] = event_name
157
+ event[:properties] = properties
158
+ event
159
+ end
160
+
161
+ def create_refresh_event(user)
162
+ create_event('REFRESH', user)
163
+ end
164
+
165
+ def create_event(event_type, user)
166
+ {
167
+ eventType: event_type,
168
+ date: Time.now().iso8601,
169
+ count: 1,
170
+ installationId: user.id,
171
+ context: create_context(user),
172
+ lib: {
173
+ version: '0.0.1',
174
+ type: 'ruby'
175
+ }
176
+ }
177
+ end
178
+
179
+ def post_json(url_str, data, async)
180
+ url = URI.parse(url_str)
181
+ req = Net::HTTP::Post.new(url.to_s, 'Content-Type' => 'application/json')
182
+ req.body = data.to_json
183
+ if async && !@force_sync
184
+ @logger.debug 'Queue async event'
185
+ @queue << {url: url, req: req}
186
+ nil
187
+ else
188
+ @logger.debug 'Sync request start'
189
+ res = Net::HTTP.start(url.host, url.port, use_ssl: url.scheme == 'https') {|http|
190
+ http.request(req)
191
+ }
192
+ @logger.debug 'Sync request end'
193
+ res.body
194
+ end
195
+ end
196
+
197
+ def send_event(event, async = true)
198
+ begin
199
+ body = post_json(@server + '/event?' + URI.encode_www_form({
200
+ application: @application_id,
201
+ environmentName: @environment_name
202
+ }), event, async)
203
+ if !body.nil?
204
+ result = JSON.parse(body);
205
+ feature_configurations = {}
206
+ result['features'].each{|feature|
207
+ feature_configurations[feature['name']] = {
208
+ name: feature['name'],
209
+ state: feature['state']
210
+ }
211
+ }
212
+ feature_configurations
213
+ end
214
+ rescue => e
215
+ log_error(e)
216
+ end
217
+ end
218
+
219
+ def create_context(user)
220
+ {
221
+ user: user.as_json
222
+ }
223
+ end
224
+ end
225
+ end
226
+ end
@@ -1,27 +1,27 @@
1
- # coding: utf-8
2
- lib = File.expand_path('../lib', __FILE__)
3
- $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
- require 'makitoo/feature_flag/version'
5
-
6
- Gem::Specification.new do |spec|
7
- spec.name = "makitoo-feature_flag"
8
- spec.version = Makitoo::FeatureFlag::VERSION
9
- spec.authors = ["Makitoo"]
10
- spec.email = ["support@makitoo.com"]
11
-
12
- spec.summary = "Makitoo feature flag"
13
- spec.description = "Makitoo feature flag client for ruby"
14
- spec.homepage = "https://makitoo.com/features"
15
- spec.license = "MIT"
16
-
17
- spec.files = `git ls-files -z`.split("\x0").reject do |f|
18
- f.match(%r{^(test|spec|features)/})
19
- end
20
- spec.bindir = "exe"
21
- spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
22
- spec.require_paths = ["lib"]
23
-
24
- spec.add_development_dependency "bundler", "~> 1.13"
25
- spec.add_development_dependency "rake", "~> 10.0"
26
- spec.add_development_dependency "rspec", "~> 3.0"
27
- end
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'makitoo/feature_flag/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "makitoo-feature_flag"
8
+ spec.version = Makitoo::FeatureFlag::VERSION
9
+ spec.authors = ["Makitoo"]
10
+ spec.email = ["support@makitoo.com"]
11
+
12
+ spec.summary = "Makitoo feature flag"
13
+ spec.description = "Makitoo feature flag client for ruby"
14
+ spec.homepage = "https://makitoo.com/features"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
18
+ f.match(%r{^(test|spec|features)/})
19
+ end
20
+ spec.bindir = "exe"
21
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
22
+ spec.require_paths = ["lib"]
23
+
24
+ spec.add_development_dependency "bundler", "~> 1.13"
25
+ spec.add_development_dependency "rake", "~> 10.0"
26
+ spec.add_development_dependency "rspec", "~> 3.0"
27
+ end
data/readme.md ADDED
@@ -0,0 +1,41 @@
1
+ # Makitoo::FeatureFlag
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/makitoo/feature_flag`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'makitoo-feature_flag'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install makitoo-feature_flag
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
30
+
31
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
32
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/makitoo-feature_flag.
36
+
37
+
38
+ ## License
39
+
40
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
41
+
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: makitoo-feature_flag
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.4
4
+ version: 0.1.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Makitoo
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-01-25 00:00:00.000000000 Z
11
+ date: 2017-01-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -69,8 +69,11 @@ files:
69
69
  - bin/console
70
70
  - bin/setup
71
71
  - lib/makitoo/feature_flag.rb
72
+ - lib/makitoo/feature_flag/memory_cache.rb
73
+ - lib/makitoo/feature_flag/user.rb
72
74
  - lib/makitoo/feature_flag/version.rb
73
75
  - makitoo-feature_flag.gemspec
76
+ - readme.md
74
77
  homepage: https://makitoo.com/features
75
78
  licenses:
76
79
  - MIT