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 +4 -4
- data/.gitignore +9 -9
- data/.rspec +2 -2
- data/.travis.yml +5 -5
- data/Gemfile +4 -4
- data/LICENSE.txt +21 -21
- data/README.md +41 -41
- data/Rakefile +6 -6
- data/bin/console +14 -14
- data/bin/setup +8 -8
- data/lib/makitoo/feature_flag/memory_cache.rb +37 -0
- data/lib/makitoo/feature_flag/user.rb +28 -0
- data/lib/makitoo/feature_flag/version.rb +5 -5
- data/lib/makitoo/feature_flag.rb +226 -221
- data/makitoo-feature_flag.gemspec +27 -27
- data/readme.md +41 -0
- metadata +5 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 55f3f5c59ac511b1cac242157228bd8254c0b285
|
4
|
+
data.tar.gz: 265f040a3925b6f24f45ec43f7976344245adaec
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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
|
-
end
|
5
|
-
end
|
1
|
+
module Makitoo
|
2
|
+
module FeatureFlag
|
3
|
+
VERSION = "0.1.5"
|
4
|
+
end
|
5
|
+
end
|
data/lib/makitoo/feature_flag.rb
CHANGED
@@ -1,221 +1,226 @@
|
|
1
|
-
require 'net/http'
|
2
|
-
require 'time'
|
3
|
-
require 'json'
|
4
|
-
require
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
@
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
else
|
46
|
-
'
|
47
|
-
end
|
48
|
-
|
49
|
-
|
50
|
-
else
|
51
|
-
|
52
|
-
end
|
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
|
-
def
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
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
|
-
end
|
139
|
-
|
140
|
-
def
|
141
|
-
|
142
|
-
end
|
143
|
-
|
144
|
-
def
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
|
194
|
-
|
195
|
-
|
196
|
-
|
197
|
-
|
198
|
-
|
199
|
-
|
200
|
-
|
201
|
-
|
202
|
-
|
203
|
-
|
204
|
-
|
205
|
-
|
206
|
-
|
207
|
-
|
208
|
-
|
209
|
-
|
210
|
-
|
211
|
-
|
212
|
-
|
213
|
-
|
214
|
-
|
215
|
-
|
216
|
-
|
217
|
-
end
|
218
|
-
|
219
|
-
|
220
|
-
|
221
|
-
|
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
|
+
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-
|
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
|