featureswitches 0.8.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +10 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +65 -0
- data/Rakefile +2 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/featureswitches.gemspec +26 -0
- data/lib/featureswitches/version.rb +3 -0
- data/lib/featureswitches.rb +188 -0
- metadata +110 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: ee74f39270d0d3c7a4dd1693098902de7115694b
|
4
|
+
data.tar.gz: 59c16ebdd822a0f138ea9d5133e16f43ad297947
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 0369a8e947e190a753f45f0cd7ccd9906b54cc81c51f4a708c8647555ff883422251f10365268ef5b19abee33674c691094b00d49c78a3db5749260a0580e3aa
|
7
|
+
data.tar.gz: 169427bdd5448049f7a759d2f3ea3bf21f243866e10d1cb8179d898e5267e8bbbd02e116b1b28c941e010a2bfc1cc94dad8a85b75f27a5b96808acec5a2d855a
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2016 Joel Weirauch
|
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
ADDED
@@ -0,0 +1,65 @@
|
|
1
|
+
# Featureswitches
|
2
|
+
|
3
|
+
A Ruby gem for interacting with [FeatureSwitches.com](https://featureswitches.com). This library is under active development and is likely to change frequently. Bug reports and pull requests are welcome.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'featureswitches'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install featureswitches
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
```ruby
|
24
|
+
fs = FeatureSwitches.new('customer_api_key', 'environment_api_key', {options})
|
25
|
+
|
26
|
+
# Ensure that the API credentials are valid
|
27
|
+
result = fs.authenticate # result will be true/false to indicate success
|
28
|
+
|
29
|
+
# Add a user
|
30
|
+
result = fs.add_user('user_identifier', 'optional_customer_identifier', 'optional_name', 'optional_email')
|
31
|
+
|
32
|
+
# Check if a feature is enabled
|
33
|
+
result = fs.is_enabled('feature_key', 'optional_user_identifier', default_return_value(true/false, default=false))
|
34
|
+
|
35
|
+
if result
|
36
|
+
# Feature enabled, do something
|
37
|
+
else
|
38
|
+
# Feature disabled, do something else
|
39
|
+
end
|
40
|
+
```
|
41
|
+
|
42
|
+
### Configuration Options
|
43
|
+
A few options are available to be tweaked if you so choose. The library makes use of a local cache to minimize requests back to the FeatureSwitches server. Additionally, a check it performed at an interval to automatically re-sync feature state when changes are made in the dashboard.
|
44
|
+
|
45
|
+
```ruby
|
46
|
+
{
|
47
|
+
:cache_timeout => SECONDS, # optional, defaults to 300 seconds
|
48
|
+
:check_interval => SECONDS # optional, defaults to 10 seconds
|
49
|
+
}
|
50
|
+
```
|
51
|
+
## Development
|
52
|
+
|
53
|
+
After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
54
|
+
|
55
|
+
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).
|
56
|
+
|
57
|
+
## Contributing
|
58
|
+
|
59
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/featureswitches/featureswitches-ruby.
|
60
|
+
|
61
|
+
|
62
|
+
## License
|
63
|
+
|
64
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
65
|
+
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "featureswitches"
|
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
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'featureswitches/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "featureswitches"
|
8
|
+
spec.version = Featureswitches::VERSION
|
9
|
+
spec.authors = ["Joel Weirauch"]
|
10
|
+
spec.email = ["joel@featureswitches.com"]
|
11
|
+
|
12
|
+
spec.summary = %q{Ruby client for FeatureSwitches.com, feature flags as a service.}
|
13
|
+
spec.homepage = "https://github.com/featureswitches/featureswitches-ruby"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
17
|
+
spec.bindir = "exe"
|
18
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_development_dependency "bundler", "~> 1.11"
|
22
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
23
|
+
|
24
|
+
spec.add_dependency "ruby-cache", "~> 0.3"
|
25
|
+
spec.add_dependency "nestful", "~> 1.1"
|
26
|
+
end
|
@@ -0,0 +1,188 @@
|
|
1
|
+
require "featureswitches/version"
|
2
|
+
require "thread"
|
3
|
+
require "cache"
|
4
|
+
require "nestful"
|
5
|
+
|
6
|
+
class Featureswitches
|
7
|
+
def initialize(customer_key, environment_key, options={})
|
8
|
+
@customer_key = customer_key
|
9
|
+
@environment_key = environment_key
|
10
|
+
@cache_timeout = options[:cache_timeout] ||= 300
|
11
|
+
@check_interval = options[:check_interval] ||= 10
|
12
|
+
@last_update = 0
|
13
|
+
@last_dirty_check = 0
|
14
|
+
@api = options[:api] ||= 'https://api.featureswitches.com/v1/'
|
15
|
+
|
16
|
+
@cache = Cache.new
|
17
|
+
|
18
|
+
@dirty_check_thread = do_dirty_check
|
19
|
+
end
|
20
|
+
|
21
|
+
def authenticate
|
22
|
+
endpoint = 'authenticate'
|
23
|
+
response = api_request(endpoint)
|
24
|
+
|
25
|
+
return response
|
26
|
+
end
|
27
|
+
|
28
|
+
def sync
|
29
|
+
endpoint = 'features'
|
30
|
+
response = api_request(endpoint)
|
31
|
+
|
32
|
+
features = response[:data]['features']
|
33
|
+
|
34
|
+
features.each do |feature|
|
35
|
+
feature['last_sync'] = Time.now.to_i
|
36
|
+
@cache[feature['feature_key']] = feature
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
def is_enabled(feature_key, user_identifier=nil, default=false)
|
41
|
+
feature = @cache[feature_key]
|
42
|
+
if feature and not cache_is_stale(feature)
|
43
|
+
return enabled_for_user(feature, user_identifier)
|
44
|
+
else
|
45
|
+
feature = get_feature(feature_key)
|
46
|
+
if feature
|
47
|
+
return enabled_for_user(feature, user_identifier)
|
48
|
+
end
|
49
|
+
end
|
50
|
+
return default
|
51
|
+
end
|
52
|
+
|
53
|
+
def add_user(user_identifier, customer_identifier=nil, name=nil, email=nil)
|
54
|
+
endpoint = 'user/add'
|
55
|
+
params = {
|
56
|
+
'user_identifier' => user_identifier,
|
57
|
+
'customer_identifier' => customer_identifier,
|
58
|
+
'name' => name,
|
59
|
+
'email' => email
|
60
|
+
}
|
61
|
+
response = api_request(endpoint, params, :post)
|
62
|
+
|
63
|
+
if response[:success]
|
64
|
+
return true
|
65
|
+
end
|
66
|
+
return false
|
67
|
+
end
|
68
|
+
|
69
|
+
def dirty_check
|
70
|
+
endpoint = 'dirty-check'
|
71
|
+
response = api_request(endpoint)
|
72
|
+
|
73
|
+
if response[:success]
|
74
|
+
@last_dirty_check = Time.now.to_i
|
75
|
+
|
76
|
+
if response[:data]['last_update'] > @last_update
|
77
|
+
@last_update = response[:data]['last_update']
|
78
|
+
sync()
|
79
|
+
end
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
83
|
+
private
|
84
|
+
|
85
|
+
def get_feature(feature_key)
|
86
|
+
endpoint = 'feature'
|
87
|
+
params = {'feature_key' => feature_key}
|
88
|
+
response = api_request(endpoint, params)
|
89
|
+
|
90
|
+
if response[:success]
|
91
|
+
feature = response[:data]['feature']
|
92
|
+
|
93
|
+
feature['last_sync'] = Time.now.to_i
|
94
|
+
@cache[feature['feature_key']] = feature
|
95
|
+
|
96
|
+
return feature
|
97
|
+
end
|
98
|
+
|
99
|
+
return nil
|
100
|
+
end
|
101
|
+
|
102
|
+
def enabled_for_user(feature, user_identifier)
|
103
|
+
if feature['enabled'] and user_identifier
|
104
|
+
if feature['include_users'].length > 0
|
105
|
+
if feature['include_users'].include? user_identifier
|
106
|
+
return true
|
107
|
+
else
|
108
|
+
return false
|
109
|
+
end
|
110
|
+
elsif feature['exclude_users'].length > 0
|
111
|
+
if feature['exclude_users'].include? user_identifier
|
112
|
+
return false
|
113
|
+
else
|
114
|
+
return true
|
115
|
+
end
|
116
|
+
end
|
117
|
+
elsif not user_identifier and (feature['include_users'].length > 0 or feature['exclude_users'].length > 0)
|
118
|
+
return false
|
119
|
+
end
|
120
|
+
|
121
|
+
return feature['enabled']
|
122
|
+
end
|
123
|
+
|
124
|
+
def cache_is_stale(feature)
|
125
|
+
cache_expiration = (Time.now.to_i - @cache_timeout)
|
126
|
+
if feature['last_sync'] > cache_expiration and @last_dirty_check > cache_expiration
|
127
|
+
return false
|
128
|
+
end
|
129
|
+
|
130
|
+
if @last_dirty_check < cache_expiration
|
131
|
+
return true
|
132
|
+
end
|
133
|
+
|
134
|
+
return false
|
135
|
+
end
|
136
|
+
|
137
|
+
def api_request(endpoint, params={}, method=:get)
|
138
|
+
headers = {
|
139
|
+
'Authorization' => "#{@customer_key}:#{@environment_key}"
|
140
|
+
}
|
141
|
+
|
142
|
+
begin
|
143
|
+
request = Nestful::Request.new(@api + endpoint, :headers => headers, :params => params, :method => method)
|
144
|
+
response = request.execute
|
145
|
+
|
146
|
+
if response.status == 200
|
147
|
+
data = response.decoded
|
148
|
+
result = {
|
149
|
+
'success': true,
|
150
|
+
'message': '',
|
151
|
+
'data': data
|
152
|
+
}
|
153
|
+
else
|
154
|
+
data = response.decoded
|
155
|
+
result = {
|
156
|
+
'success': false,
|
157
|
+
'message': data['message'],
|
158
|
+
'status': response.status
|
159
|
+
}
|
160
|
+
end
|
161
|
+
|
162
|
+
return result
|
163
|
+
rescue Nestful::ForbiddenAccess
|
164
|
+
result = {
|
165
|
+
'success': false,
|
166
|
+
'message': 'Authentication Error',
|
167
|
+
'status': 403
|
168
|
+
}
|
169
|
+
return result
|
170
|
+
rescue Nestful::ConnectionError
|
171
|
+
result = {
|
172
|
+
'success': false,
|
173
|
+
'message': 'Error communicating with FeatureSwitches',
|
174
|
+
'status': -1
|
175
|
+
}
|
176
|
+
return result
|
177
|
+
end
|
178
|
+
end
|
179
|
+
|
180
|
+
def do_dirty_check
|
181
|
+
Thread.new do
|
182
|
+
loop do
|
183
|
+
dirty_check()
|
184
|
+
sleep(@check_interval)
|
185
|
+
end
|
186
|
+
end
|
187
|
+
end
|
188
|
+
end
|
metadata
ADDED
@@ -0,0 +1,110 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: featureswitches
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.8.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Joel Weirauch
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-03-17 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.11'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.11'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: ruby-cache
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0.3'
|
48
|
+
type: :runtime
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0.3'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: nestful
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '1.1'
|
62
|
+
type: :runtime
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '1.1'
|
69
|
+
description:
|
70
|
+
email:
|
71
|
+
- joel@featureswitches.com
|
72
|
+
executables: []
|
73
|
+
extensions: []
|
74
|
+
extra_rdoc_files: []
|
75
|
+
files:
|
76
|
+
- ".gitignore"
|
77
|
+
- Gemfile
|
78
|
+
- LICENSE.txt
|
79
|
+
- README.md
|
80
|
+
- Rakefile
|
81
|
+
- bin/console
|
82
|
+
- bin/setup
|
83
|
+
- featureswitches.gemspec
|
84
|
+
- lib/featureswitches.rb
|
85
|
+
- lib/featureswitches/version.rb
|
86
|
+
homepage: https://github.com/featureswitches/featureswitches-ruby
|
87
|
+
licenses:
|
88
|
+
- MIT
|
89
|
+
metadata: {}
|
90
|
+
post_install_message:
|
91
|
+
rdoc_options: []
|
92
|
+
require_paths:
|
93
|
+
- lib
|
94
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
95
|
+
requirements:
|
96
|
+
- - ">="
|
97
|
+
- !ruby/object:Gem::Version
|
98
|
+
version: '0'
|
99
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - ">="
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '0'
|
104
|
+
requirements: []
|
105
|
+
rubyforge_project:
|
106
|
+
rubygems_version: 2.4.6
|
107
|
+
signing_key:
|
108
|
+
specification_version: 4
|
109
|
+
summary: Ruby client for FeatureSwitches.com, feature flags as a service.
|
110
|
+
test_files: []
|