ldclient-rb 0.4.0 → 0.5.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.hound.yml +2 -0
- data/.rspec +2 -0
- data/.rubocop.yml +601 -0
- data/.simplecov +4 -0
- data/CONTRIBUTING.md +10 -0
- data/Gemfile +1 -1
- data/LICENSE.txt +1 -1
- data/README.md +72 -13
- data/Rakefile +3 -0
- data/circle.yml +29 -0
- data/ldclient-rb.gemspec +8 -6
- data/lib/ldclient-rb/config.rb +73 -76
- data/lib/ldclient-rb/ldclient.rb +201 -199
- data/lib/ldclient-rb/newrelic.rb +4 -7
- data/lib/ldclient-rb/store.rb +10 -11
- data/lib/ldclient-rb/stream.rb +67 -67
- data/lib/ldclient-rb/version.rb +1 -1
- data/spec/config_spec.rb +45 -0
- data/spec/fixtures/feature.json +67 -0
- data/spec/fixtures/user.json +9 -0
- data/spec/ldclient_spec.rb +226 -0
- data/spec/newrelic_spec.rb +5 -0
- data/spec/spec_helper.rb +9 -0
- data/spec/store_spec.rb +10 -0
- data/spec/stream_spec.rb +118 -0
- data/spec/version_spec.rb +7 -0
- metadata +82 -31
data/.simplecov
ADDED
data/CONTRIBUTING.md
ADDED
@@ -0,0 +1,10 @@
|
|
1
|
+
Contributing to LaunchDarkly SDK for Ruby
|
2
|
+
=========================================
|
3
|
+
|
4
|
+
We encourage pull-requests and other contributions from the community. We've also published an [SDK contributor's guide](http://docs.launchdarkly.com/v1.0/docs/sdk-contributors-guide) that provides a detailed explanation of how our SDKs work.
|
5
|
+
|
6
|
+
Style
|
7
|
+
-----
|
8
|
+
|
9
|
+
Our pull requests have [Hound CI](https://houndci.com/) set up to do style checking.
|
10
|
+
We also run [Rubocop](https://github.com/bbatsov/rubocop).
|
data/Gemfile
CHANGED
data/LICENSE.txt
CHANGED
@@ -10,4 +10,4 @@ Unless required by applicable law or agreed to in writing, software
|
|
10
10
|
distributed under the License is distributed on an "AS IS" BASIS,
|
11
11
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
12
12
|
See the License for the specific language governing permissions and
|
13
|
-
limitations under the License.
|
13
|
+
limitations under the License.
|
data/README.md
CHANGED
@@ -1,33 +1,94 @@
|
|
1
1
|
LaunchDarkly SDK for Ruby
|
2
2
|
===========================
|
3
3
|
|
4
|
+
[![Gem Version](https://badge.fury.io/rb/ldclient-rb.svg)](http://badge.fury.io/rb/ldclient-rb)
|
5
|
+
|
6
|
+
[![Circle CI](https://circleci.com/gh/launchdarkly/ruby-client/tree/master.svg?style=svg)](https://circleci.com/gh/launchdarkly/ruby-client/tree/master)
|
7
|
+
[![Code Climate](https://codeclimate.com/github/launchdarkly/ruby-client/badges/gpa.svg)](https://codeclimate.com/github/launchdarkly/ruby-client)
|
8
|
+
[![Test Coverage](https://codeclimate.com/github/launchdarkly/ruby-client/badges/coverage.svg)](https://codeclimate.com/github/launchdarkly/ruby-client/coverage)
|
9
|
+
[![security](https://hakiri.io/github/launchdarkly/ruby-client/master.svg)](https://hakiri.io/github/launchdarkly/ruby-client/master)
|
10
|
+
|
4
11
|
Quick setup
|
5
12
|
-----------
|
6
13
|
|
7
14
|
0. Install the Ruby SDK with `gem`
|
8
15
|
|
9
|
-
|
16
|
+
```shell
|
17
|
+
gem install ldclient-rb
|
18
|
+
```
|
10
19
|
|
11
20
|
1. Require the LaunchDarkly client:
|
12
21
|
|
13
|
-
|
14
|
-
|
22
|
+
```ruby
|
23
|
+
require 'ldclient-rb'
|
24
|
+
```
|
15
25
|
|
16
26
|
2. Create a new LDClient with your API key:
|
17
27
|
|
18
|
-
|
28
|
+
```ruby
|
29
|
+
client = LaunchDarkly::LDClient.new("your_api_key")
|
30
|
+
```
|
31
|
+
|
32
|
+
### Ruby on Rails
|
33
|
+
|
34
|
+
0. Add `gem 'ldclient-rb'` to your Gemfile and `bundle install`
|
35
|
+
|
36
|
+
1. Initialize the launchdarkly client in `config/initializers/launchdarkly.rb`:
|
37
|
+
|
38
|
+
```ruby
|
39
|
+
Rails.configuration.ld_client = LaunchDarkly::LDClient.new("your_api_key")
|
40
|
+
```
|
41
|
+
|
42
|
+
2. You may want to include a function in your ApplicationController
|
43
|
+
|
44
|
+
```ruby
|
45
|
+
def launchdarkly_settings
|
46
|
+
if current_user.present?
|
47
|
+
{
|
48
|
+
key: current_user.id,
|
49
|
+
anonymous: false,
|
50
|
+
email: current_user.email,
|
51
|
+
custom: { groups: current_user.groups.pluck(:name) },
|
52
|
+
# Any other fields you may have
|
53
|
+
# e.g. lastName: current_user.last_name,
|
54
|
+
}
|
55
|
+
else
|
56
|
+
if Rails::VERSION::MAJOR <= 3
|
57
|
+
hash_key = request.session_options[:id]
|
58
|
+
else
|
59
|
+
hash_key = session.id
|
60
|
+
end
|
61
|
+
# session ids should be private to prevent session hijacking
|
62
|
+
hash_key = Digest::SHA256.base64digest hash_key
|
63
|
+
{
|
64
|
+
key: hash_key,
|
65
|
+
anonymous: true,
|
66
|
+
}
|
67
|
+
end
|
68
|
+
end
|
69
|
+
```
|
70
|
+
|
71
|
+
3. In your controllers, access the client using
|
72
|
+
|
73
|
+
```ruby
|
74
|
+
Rails.application.config.ld_client.toggle?('your.flag.key', launchdarkly_settings, false)
|
75
|
+
```
|
76
|
+
|
77
|
+
Note that this gem will automatically switch to using the Rails logger it is detected.
|
19
78
|
|
20
79
|
Your first feature flag
|
21
80
|
-----------------------
|
22
81
|
|
23
82
|
1. Create a new feature flag on your [dashboard](https://app.launchdarkly.com)
|
24
|
-
2. In your application code, use the feature's key to check
|
83
|
+
2. In your application code, use the feature's key to check whether the flag is on for each user:
|
25
84
|
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
85
|
+
```ruby
|
86
|
+
if client.toggle?("your.flag.key", {key: "user@test.com"}, false)
|
87
|
+
# application code to show the feature
|
88
|
+
else
|
89
|
+
# the code to run if the feature is off
|
90
|
+
end
|
91
|
+
```
|
31
92
|
|
32
93
|
Learn more
|
33
94
|
-----------
|
@@ -37,7 +98,7 @@ Check out our [documentation](http://docs.launchdarkly.com) for in-depth instruc
|
|
37
98
|
Contributing
|
38
99
|
------------
|
39
100
|
|
40
|
-
|
101
|
+
See [Contributing](https://github.com/launchdarkly/ruby-client/blob/master/CONTRIBUTING.md)
|
41
102
|
|
42
103
|
About LaunchDarkly
|
43
104
|
-----------
|
@@ -62,5 +123,3 @@ About LaunchDarkly
|
|
62
123
|
* [docs.launchdarkly.com] (http://docs.launchdarkly.com/ "LaunchDarkly Documentation") for our documentation and SDKs
|
63
124
|
* [apidocs.launchdarkly.com] (http://apidocs.launchdarkly.com/ "LaunchDarkly API Documentation") for our API documentation
|
64
125
|
* [blog.launchdarkly.com] (http://blog.launchdarkly.com/ "LaunchDarkly Blog Documentation") for the latest product updates
|
65
|
-
|
66
|
-
|
data/Rakefile
CHANGED
data/circle.yml
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
machine:
|
2
|
+
environment:
|
3
|
+
RUBIES: "ruby-2.2.3;ruby-2.1.7;ruby-2.0.0;ruby-1.9.3;jruby-1.7.22"
|
4
|
+
|
5
|
+
dependencies:
|
6
|
+
cache_directories:
|
7
|
+
- '../.rvm/rubies'
|
8
|
+
|
9
|
+
override:
|
10
|
+
- >
|
11
|
+
rubiesArray=(${RUBIES//;/ });
|
12
|
+
for i in "${rubiesArray[@]}";
|
13
|
+
do
|
14
|
+
rvm install $i;
|
15
|
+
rvm use $i;
|
16
|
+
gem install jruby-openssl; # required by bundler, no effect on Ruby MRI
|
17
|
+
gem install bundler;
|
18
|
+
bundle install;
|
19
|
+
done
|
20
|
+
|
21
|
+
test:
|
22
|
+
override:
|
23
|
+
- >
|
24
|
+
rubiesArray=(${RUBIES//;/ });
|
25
|
+
for i in "${rubiesArray[@]}";
|
26
|
+
do
|
27
|
+
rvm use $i;
|
28
|
+
bundle exec rspec spec;
|
29
|
+
done
|
data/ldclient-rb.gemspec
CHANGED
@@ -1,16 +1,16 @@
|
|
1
1
|
# coding: utf-8
|
2
|
-
lib = File.expand_path(
|
2
|
+
lib = File.expand_path("../lib", __FILE__)
|
3
3
|
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
-
require
|
4
|
+
require "ldclient-rb/version"
|
5
5
|
|
6
6
|
Gem::Specification.new do |spec|
|
7
7
|
spec.name = "ldclient-rb"
|
8
8
|
spec.version = LaunchDarkly::VERSION
|
9
9
|
spec.authors = ["LaunchDarkly"]
|
10
10
|
spec.email = ["team@launchdarkly.com"]
|
11
|
-
spec.summary =
|
12
|
-
spec.description =
|
13
|
-
spec.homepage = "https://
|
11
|
+
spec.summary = "LaunchDarkly SDK for Ruby"
|
12
|
+
spec.description = "Official LaunchDarkly SDK for Ruby"
|
13
|
+
spec.homepage = "https://github.com/launchdarkly/ruby-client"
|
14
14
|
spec.license = "Apache 2.0"
|
15
15
|
|
16
16
|
spec.files = `git ls-files -z`.split("\x0")
|
@@ -20,13 +20,15 @@ Gem::Specification.new do |spec|
|
|
20
20
|
|
21
21
|
spec.add_development_dependency "bundler", "~> 1.7"
|
22
22
|
spec.add_development_dependency "rake", "~> 10.0"
|
23
|
+
spec.add_development_dependency "rspec", "~> 3.2"
|
24
|
+
spec.add_development_dependency "codeclimate-test-reporter", "~> 0"
|
23
25
|
|
24
26
|
spec.add_runtime_dependency "json", "~> 1.8"
|
25
27
|
spec.add_runtime_dependency "faraday", "~> 0.9"
|
26
28
|
spec.add_runtime_dependency "faraday-http-cache", "~> 0.4"
|
27
29
|
spec.add_runtime_dependency "thread_safe", "~> 0.3"
|
28
30
|
spec.add_runtime_dependency "net-http-persistent", "~> 2.9"
|
29
|
-
spec.add_runtime_dependency "concurrent-ruby", "~> 0.
|
31
|
+
spec.add_runtime_dependency "concurrent-ruby", "~> 1.0.0"
|
30
32
|
spec.add_runtime_dependency "hashdiff", "~> 0.2"
|
31
33
|
spec.add_runtime_dependency "ld-em-eventsource", "~> 0.2"
|
32
34
|
end
|
data/lib/ldclient-rb/config.rb
CHANGED
@@ -1,25 +1,36 @@
|
|
1
|
-
require
|
1
|
+
require "logger"
|
2
2
|
|
3
3
|
module LaunchDarkly
|
4
|
-
|
5
|
-
#
|
6
|
-
#
|
7
|
-
#
|
8
|
-
#
|
9
|
-
#
|
4
|
+
#
|
5
|
+
# This class exposes advanced configuration options for the LaunchDarkly
|
6
|
+
# client library. Most users will not need to use a custom configuration--
|
7
|
+
# the default configuration sets sane defaults for most use cases.
|
8
|
+
#
|
9
|
+
#
|
10
10
|
class Config
|
11
|
-
#
|
12
|
-
# Constructor for creating custom LaunchDarkly configurations.
|
13
|
-
#
|
11
|
+
#
|
12
|
+
# Constructor for creating custom LaunchDarkly configurations.
|
13
|
+
#
|
14
14
|
# @param opts [Hash] the configuration options
|
15
|
-
# @option opts [Logger] :logger A logger to use for messages from the
|
16
|
-
#
|
17
|
-
#
|
18
|
-
# @option opts [
|
19
|
-
#
|
20
|
-
# @option opts [
|
21
|
-
#
|
22
|
-
#
|
15
|
+
# @option opts [Logger] :logger A logger to use for messages from the
|
16
|
+
# LaunchDarkly client. Defaults to the Rails logger in a Rails
|
17
|
+
# environment, or stdout otherwise.
|
18
|
+
# @option opts [String] :base_uri ("https://app.launchdarkly.com") The base
|
19
|
+
# URL for the LaunchDarkly server. Most users should use the default value.
|
20
|
+
# @option opts [Integer] :capacity (10000) The capacity of the events
|
21
|
+
# buffer. The client buffers up to this many events in memory before
|
22
|
+
# flushing. If the capacity is exceeded before the buffer is flushed,
|
23
|
+
# events will be discarded.
|
24
|
+
# @option opts [Float] :flush_interval (30) The number of seconds between
|
25
|
+
# flushes of the event buffer.
|
26
|
+
# @option opts [Float] :read_timeout (10) The read timeout for network
|
27
|
+
# connections in seconds.
|
28
|
+
# @option opts [Float] :connect_timeout (2) The connect timeout for network
|
29
|
+
# connections in seconds.
|
30
|
+
# @option opts [Object] :store A cache store for the Faraday HTTP caching
|
31
|
+
# library. Defaults to the Rails cache in a Rails environment, or a
|
32
|
+
# thread-safe in-memory store otherwise.
|
33
|
+
#
|
23
34
|
# @return [type] [description]
|
24
35
|
def initialize(opts = {})
|
25
36
|
@base_uri = (opts[:base_uri] || Config.default_base_uri).chomp("/")
|
@@ -30,32 +41,28 @@ module LaunchDarkly
|
|
30
41
|
@flush_interval = opts[:flush_interval] || Config.default_flush_interval
|
31
42
|
@connect_timeout = opts[:connect_timeout] || Config.default_connect_timeout
|
32
43
|
@read_timeout = opts[:read_timeout] || Config.default_read_timeout
|
33
|
-
@log_timings = opts[:log_timings] || Config.default_log_timings
|
34
|
-
@stream = opts[:stream] || Config.default_stream
|
35
44
|
@feature_store = opts[:feature_store] || Config.default_feature_store
|
36
|
-
@
|
45
|
+
@stream = opts.has_key?(:stream) ? opts[:stream] : Config.default_stream
|
46
|
+
@log_timings = opts.has_key?(:log_timings) ? opts[:log_timings] : Config.default_log_timings
|
47
|
+
@debug_stream = opts.has_key?(:debug_stream) ? opts[:debug_stream] : Config.default_debug_stream
|
37
48
|
end
|
38
49
|
|
39
|
-
#
|
50
|
+
#
|
40
51
|
# The base URL for the LaunchDarkly server.
|
41
|
-
#
|
52
|
+
#
|
42
53
|
# @return [String] The configured base URL for the LaunchDarkly server.
|
43
|
-
|
44
|
-
@base_uri
|
45
|
-
end
|
54
|
+
attr_reader :base_uri
|
46
55
|
|
47
56
|
#
|
48
57
|
# The base URL for the LaunchDarkly streaming server.
|
49
|
-
#
|
58
|
+
#
|
50
59
|
# @return [String] The configured base URL for the LaunchDarkly streaming server.
|
51
|
-
|
52
|
-
@stream_uri
|
53
|
-
end
|
60
|
+
attr_reader :stream_uri
|
54
61
|
|
55
62
|
#
|
56
63
|
# Whether streaming mode should be enabled. Streaming mode asynchronously updates
|
57
64
|
# feature flags in real-time using server-sent events.
|
58
|
-
#
|
65
|
+
#
|
59
66
|
# @return [Boolean] True if streaming mode should be enabled
|
60
67
|
def stream?
|
61
68
|
@stream
|
@@ -64,65 +71,57 @@ module LaunchDarkly
|
|
64
71
|
#
|
65
72
|
# Whether we should debug streaming mode. If set, the client will fetch features via polling
|
66
73
|
# and compare the retrieved feature with the value in the feature store
|
67
|
-
#
|
74
|
+
#
|
68
75
|
# @return [Boolean] True if we should debug streaming mode
|
69
76
|
def debug_stream?
|
70
77
|
@debug_stream
|
71
78
|
end
|
72
79
|
|
73
|
-
#
|
80
|
+
#
|
74
81
|
# The number of seconds between flushes of the event buffer. Decreasing the flush interval means
|
75
82
|
# that the event buffer is less likely to reach capacity.
|
76
|
-
#
|
83
|
+
#
|
77
84
|
# @return [Float] The configured number of seconds between flushes of the event buffer.
|
78
|
-
|
79
|
-
@flush_interval
|
80
|
-
end
|
85
|
+
attr_reader :flush_interval
|
81
86
|
|
82
|
-
#
|
87
|
+
#
|
83
88
|
# The configured logger for the LaunchDarkly client. The client library uses the log to
|
84
|
-
# print warning and error messages.
|
85
|
-
#
|
89
|
+
# print warning and error messages.
|
90
|
+
#
|
86
91
|
# @return [Logger] The configured logger
|
87
|
-
|
88
|
-
@logger
|
89
|
-
end
|
92
|
+
attr_reader :logger
|
90
93
|
|
91
|
-
#
|
92
|
-
# The capacity of the events buffer. The client buffers up to this many
|
93
|
-
#
|
94
|
-
#
|
94
|
+
#
|
95
|
+
# The capacity of the events buffer. The client buffers up to this many
|
96
|
+
# events in memory before flushing. If the capacity is exceeded before
|
97
|
+
# the buffer is flushed, events will be discarded.
|
98
|
+
# Increasing the capacity means that events are less likely to be discarded,
|
99
|
+
# at the cost of consuming more memory.
|
100
|
+
#
|
95
101
|
# @return [Integer] The configured capacity of the event buffer
|
96
|
-
|
97
|
-
@capacity
|
98
|
-
end
|
102
|
+
attr_reader :capacity
|
99
103
|
|
100
|
-
#
|
101
|
-
# The store for the Faraday HTTP caching library. Stores should respond to
|
102
|
-
#
|
104
|
+
#
|
105
|
+
# The store for the Faraday HTTP caching library. Stores should respond to
|
106
|
+
# 'read' and 'write' requests.
|
107
|
+
#
|
103
108
|
# @return [Object] The configured store for the Faraday HTTP caching library.
|
104
|
-
|
105
|
-
@store
|
106
|
-
end
|
109
|
+
attr_reader :store
|
107
110
|
|
108
111
|
#
|
109
112
|
# The read timeout for network connections in seconds.
|
110
|
-
#
|
113
|
+
#
|
111
114
|
# @return [Float] The read timeout in seconds.
|
112
|
-
|
113
|
-
@read_timeout
|
114
|
-
end
|
115
|
+
attr_reader :read_timeout
|
115
116
|
|
116
117
|
#
|
117
118
|
# The connect timeout for network connections in seconds.
|
118
|
-
#
|
119
|
+
#
|
119
120
|
# @return [Float] The connect timeout in seconds.
|
120
|
-
|
121
|
-
@connect_timeout
|
122
|
-
end
|
121
|
+
attr_reader :connect_timeout
|
123
122
|
|
124
123
|
#
|
125
|
-
# Whether timing information should be logged. If it is logged, it will be logged to the DEBUG
|
124
|
+
# Whether timing information should be logged. If it is logged, it will be logged to the DEBUG
|
126
125
|
# level on the configured logger. This can be very verbose.
|
127
126
|
#
|
128
127
|
# @return [Boolean] True if timing information should be logged.
|
@@ -130,19 +129,18 @@ module LaunchDarkly
|
|
130
129
|
@log_timings
|
131
130
|
end
|
132
131
|
|
133
|
-
#
|
132
|
+
#
|
134
133
|
# TODO docs
|
135
134
|
#
|
136
|
-
|
137
|
-
@feature_store
|
138
|
-
end
|
135
|
+
attr_reader :feature_store
|
139
136
|
|
140
|
-
#
|
141
|
-
# The default LaunchDarkly client configuration. This configuration sets
|
142
|
-
#
|
137
|
+
#
|
138
|
+
# The default LaunchDarkly client configuration. This configuration sets
|
139
|
+
# reasonable defaults for most users.
|
140
|
+
#
|
143
141
|
# @return [Config] The default LaunchDarkly configuration.
|
144
142
|
def self.default
|
145
|
-
Config.new
|
143
|
+
Config.new
|
146
144
|
end
|
147
145
|
|
148
146
|
def self.default_capacity
|
@@ -159,7 +157,7 @@ module LaunchDarkly
|
|
159
157
|
|
160
158
|
def self.default_store
|
161
159
|
defined?(Rails) && Rails.respond_to?(:cache) ? Rails.cache : ThreadSafeMemoryStore.new
|
162
|
-
end
|
160
|
+
end
|
163
161
|
|
164
162
|
def self.default_flush_interval
|
165
163
|
10
|
@@ -192,6 +190,5 @@ module LaunchDarkly
|
|
192
190
|
def self.default_debug_stream
|
193
191
|
false
|
194
192
|
end
|
195
|
-
|
196
193
|
end
|
197
|
-
end
|
194
|
+
end
|