From cd807e963f6c0d8f5d8ef45f77ad4f7966cc05ca Mon Sep 17 00:00:00 2001 From: Adam Date: Mon, 27 Mar 2017 13:38:56 -0700 Subject: [PATCH 01/13] rails 5.1 support --- CHANGELOG.md | 4 ++++ circle.yml | 2 +- ldclient-rb.gemspec | 2 +- lib/ldclient-rb/version.rb | 2 +- 4 files changed, 7 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e3f02bf2..e1589d20 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,10 @@ All notable changes to the LaunchDarkly Ruby SDK will be documented in this file. This project adheres to [Semantic Versioning](http://semver.org). +## [2.1.1] - 2017-03-22 +## Changed +- Bumped nio4r to 2.0 + ## [2.0.6] - 2017-02-10 ## Changed - Improved handling of http status codes that may not be integers. diff --git a/circle.yml b/circle.yml index b9b92b98..6ab86988 100644 --- a/circle.yml +++ b/circle.yml @@ -1,6 +1,6 @@ machine: environment: - RUBIES: "ruby-2.2.3;ruby-2.1.7;ruby-2.0.0;ruby-1.9.3;jruby-1.7.22" + RUBIES: "ruby-2.4.1;ruby-2.2.2;jruby-9.0.0.0" dependencies: cache_directories: diff --git a/ldclient-rb.gemspec b/ldclient-rb.gemspec index 65f70532..a01f8a70 100644 --- a/ldclient-rb.gemspec +++ b/ldclient-rb.gemspec @@ -33,7 +33,7 @@ Gem::Specification.new do |spec| spec.add_runtime_dependency "hashdiff", "~> 0.2" spec.add_runtime_dependency "ld-celluloid-eventsource", "~> 0.9.0" spec.add_runtime_dependency "celluloid", "~> 0.18.0.pre" # transitive dep; specified here for more control - spec.add_runtime_dependency "nio4r", "~> 1.1" # for maximum ruby version compatibility. + spec.add_runtime_dependency "nio4r", "~> 2.0" # for maximum ruby version compatibility. spec.add_runtime_dependency "waitutil", "0.2" end diff --git a/lib/ldclient-rb/version.rb b/lib/ldclient-rb/version.rb index cc6064e3..b8ba2b7c 100644 --- a/lib/ldclient-rb/version.rb +++ b/lib/ldclient-rb/version.rb @@ -1,3 +1,3 @@ module LaunchDarkly - VERSION = "2.1.0" + VERSION = "2.1.1" end From 41d56ef3e1cb607b5d7509a835e97782139445cc Mon Sep 17 00:00:00 2001 From: Daryl Lau Date: Tue, 25 Apr 2017 14:34:30 -0700 Subject: [PATCH 02/13] Add proxy config --- lib/ldclient-rb/config.rb | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/lib/ldclient-rb/config.rb b/lib/ldclient-rb/config.rb index fd564dab..32a8a015 100644 --- a/lib/ldclient-rb/config.rb +++ b/lib/ldclient-rb/config.rb @@ -56,6 +56,7 @@ def initialize(opts = {}) @stream = opts.has_key?(:stream) ? opts[:stream] : Config.default_stream @offline = opts.has_key?(:offline) ? opts[:offline] : Config.default_offline @poll_interval = opts.has_key?(:poll_interval) && opts[:poll_interval] > 1 ? opts[:poll_interval] : Config.default_poll_interval + @proxy = opts[:proxy] || Config.default_proxy end # @@ -143,6 +144,11 @@ def offline? # attr_reader :feature_store + + # The proxy configuration + # + attr_reader :proxy + # # The default LaunchDarkly client configuration. This configuration sets # reasonable defaults for most users. @@ -184,6 +190,10 @@ def self.default_connect_timeout 2 end + def self.default_proxy + nil + end + def self.default_logger if defined?(Rails) && Rails.respond_to?(:logger) Rails.logger From 8da918b773c80800e15ce4f7f70b85357a691698 Mon Sep 17 00:00:00 2001 From: Daryl Lau Date: Tue, 25 Apr 2017 14:34:57 -0700 Subject: [PATCH 03/13] Add proxy support to polling --- lib/ldclient-rb/requestor.rb | 3 ++ spec/requestor_spec.rb | 53 ++++++++++++++++++++++++++++++++++++ 2 files changed, 56 insertions(+) create mode 100644 spec/requestor_spec.rb diff --git a/lib/ldclient-rb/requestor.rb b/lib/ldclient-rb/requestor.rb index b43cf209..1da7c784 100644 --- a/lib/ldclient-rb/requestor.rb +++ b/lib/ldclient-rb/requestor.rb @@ -30,6 +30,9 @@ def make_request(path) req.headers["User-Agent"] = "RubyClient/" + LaunchDarkly::VERSION req.options.timeout = @config.read_timeout req.options.open_timeout = @config.connect_timeout + if @config.proxy + req.options.proxy = Faraday::ProxyOptions.from @config.proxy + end end @config.logger.debug("[LDClient] Got response from uri: #{uri}\n\tstatus code: #{res.status}\n\theaders: #{res.headers}\n\tbody: #{res.body}") diff --git a/spec/requestor_spec.rb b/spec/requestor_spec.rb new file mode 100644 index 00000000..21e06ed4 --- /dev/null +++ b/spec/requestor_spec.rb @@ -0,0 +1,53 @@ +require "spec_helper" +require "faraday" + +describe LaunchDarkly::Requestor do + describe ".request_all_flags" do + describe "with a proxy" do + let(:requestor) { + LaunchDarkly::Requestor.new( + "key", + LaunchDarkly::Config.new({ + :proxy => "http://proxy.com", + :base_uri => "http://ld.com" + }) + ) + } + it "converts the proxy option" do + faraday = Faraday.new + requestor.instance_variable_set(:@client, faraday) + allow(faraday).to receive(:get) do |*args, &block| + req = double(Faraday::Request, :headers => {}, :options => Faraday::RequestOptions.new) + block.call(req) + expect(args).to eq ['http://ld.com/sdk/latest-flags'] + expect(req.options.proxy[:uri]).to eq URI("http://proxy.com") + double(body: '{"foo": "bar"}', status: 200, headers: {}) + end + + requestor.request_all_flags() + end + end + describe "without a proxy" do + let(:requestor) { + LaunchDarkly::Requestor.new( + "key", + LaunchDarkly::Config.new({ + :base_uri => "http://ld.com" + }) + ) + } + it "converts the proxy option" do + faraday = Faraday.new + requestor.instance_variable_set(:@client, faraday) + allow(faraday).to receive(:get) do |*args, &block| + req = double(Faraday::Request, :headers => {}, :options => Faraday::RequestOptions.new) + block.call(req) + expect(args).to eq ['http://ld.com/sdk/latest-flags'] + expect(req.options.proxy).to eq nil + double(body: '{"foo": "bar"}', status: 200, headers: {}) + end + requestor.request_all_flags() + end + end + end +end From c987c910afe152f199ed7ad586d3da95559bcc88 Mon Sep 17 00:00:00 2001 From: Daryl Lau Date: Tue, 25 Apr 2017 14:35:25 -0700 Subject: [PATCH 04/13] Fix encoding issue deserializing responses --- lib/ldclient-rb/requestor.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/ldclient-rb/requestor.rb b/lib/ldclient-rb/requestor.rb index 1da7c784..4ab50bc1 100644 --- a/lib/ldclient-rb/requestor.rb +++ b/lib/ldclient-rb/requestor.rb @@ -9,7 +9,7 @@ def initialize(sdk_key, config) @sdk_key = sdk_key @config = config @client = Faraday.new do |builder| - builder.use :http_cache, store: @config.cache_store + builder.use :http_cache, store: @config.cache_store, serializer: Marshal builder.adapter :net_http_persistent end From 71f0889383a517c32a542f3029323ec71d17d7b8 Mon Sep 17 00:00:00 2001 From: Daryl Lau Date: Wed, 26 Apr 2017 15:54:29 -0700 Subject: [PATCH 05/13] Pass proxy to streaming EventSource --- lib/ldclient-rb/config.rb | 2 +- lib/ldclient-rb/stream.rb | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/ldclient-rb/config.rb b/lib/ldclient-rb/config.rb index 32a8a015..6f02266e 100644 --- a/lib/ldclient-rb/config.rb +++ b/lib/ldclient-rb/config.rb @@ -145,7 +145,7 @@ def offline? attr_reader :feature_store - # The proxy configuration + # The proxy configuration string # attr_reader :proxy diff --git a/lib/ldclient-rb/stream.rb b/lib/ldclient-rb/stream.rb index f78f17ee..2c432fb2 100644 --- a/lib/ldclient-rb/stream.rb +++ b/lib/ldclient-rb/stream.rb @@ -33,7 +33,7 @@ def start 'Authorization' => @sdk_key, 'User-Agent' => 'RubyClient/' + LaunchDarkly::VERSION } - opts = {:headers => headers, :with_credentials => true} + opts = {:headers => headers, :with_credentials => true, :proxy => @config.proxy} @es = Celluloid::EventSource.new(@config.stream_uri + "/flags", opts) do |conn| conn.on(PUT) { |message| process_message(message, PUT) } conn.on(PATCH) { |message| process_message(message, PATCH) } From 45144413e14783f7a9d75a386a427893f67922b5 Mon Sep 17 00:00:00 2001 From: Daryl Lau Date: Tue, 2 May 2017 16:53:31 -0700 Subject: [PATCH 06/13] Sync to branch --- Gemfile | 2 ++ ldclient-rb.gemspec | 1 - 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/Gemfile b/Gemfile index b4e2a20b..3cb1a930 100644 --- a/Gemfile +++ b/Gemfile @@ -1,3 +1,5 @@ source "https://rubygems.org" gemspec +gem 'ld-celluloid-eventsource', :git => 'https://github.com/launchdarkly/celluloid-eventsource.git', :branch => 'sync-0.9.0-master' + diff --git a/ldclient-rb.gemspec b/ldclient-rb.gemspec index 97525142..03d93bfa 100644 --- a/ldclient-rb.gemspec +++ b/ldclient-rb.gemspec @@ -31,7 +31,6 @@ Gem::Specification.new do |spec| spec.add_runtime_dependency "net-http-persistent", "~> 2.9" spec.add_runtime_dependency "concurrent-ruby", "~> 1.0.4" spec.add_runtime_dependency "hashdiff", "~> 0.2" - spec.add_runtime_dependency "ld-celluloid-eventsource", "~> 0.9.0" spec.add_runtime_dependency "celluloid", "~> 0.18.0.pre" # transitive dep; specified here for more control if RUBY_VERSION >= '2.2.2' From f3d43122567dce27037d76509f7fb4af581a2a03 Mon Sep 17 00:00:00 2001 From: Daryl Lau Date: Fri, 5 May 2017 12:03:43 -0700 Subject: [PATCH 07/13] Bump celluloid eventsource --- Gemfile | 2 -- ldclient-rb.gemspec | 1 + 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/Gemfile b/Gemfile index 3cb1a930..b4e2a20b 100644 --- a/Gemfile +++ b/Gemfile @@ -1,5 +1,3 @@ source "https://rubygems.org" gemspec -gem 'ld-celluloid-eventsource', :git => 'https://github.com/launchdarkly/celluloid-eventsource.git', :branch => 'sync-0.9.0-master' - diff --git a/ldclient-rb.gemspec b/ldclient-rb.gemspec index 03d93bfa..b45a74de 100644 --- a/ldclient-rb.gemspec +++ b/ldclient-rb.gemspec @@ -31,6 +31,7 @@ Gem::Specification.new do |spec| spec.add_runtime_dependency "net-http-persistent", "~> 2.9" spec.add_runtime_dependency "concurrent-ruby", "~> 1.0.4" spec.add_runtime_dependency "hashdiff", "~> 0.2" + spec.add_runtime_dependency "ld-celluloid-eventsource", "~> 0.10.0" spec.add_runtime_dependency "celluloid", "~> 0.18.0.pre" # transitive dep; specified here for more control if RUBY_VERSION >= '2.2.2' From 7e044603104dd35cbf2609905d55a546e3e9f685 Mon Sep 17 00:00:00 2001 From: Daryl Lau Date: Fri, 5 May 2017 14:05:37 -0700 Subject: [PATCH 08/13] Revert "Fix encoding issue deserializing responses" This reverts commit c987c910afe152f199ed7ad586d3da95559bcc88. --- lib/ldclient-rb/requestor.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/ldclient-rb/requestor.rb b/lib/ldclient-rb/requestor.rb index 4ab50bc1..1da7c784 100644 --- a/lib/ldclient-rb/requestor.rb +++ b/lib/ldclient-rb/requestor.rb @@ -9,7 +9,7 @@ def initialize(sdk_key, config) @sdk_key = sdk_key @config = config @client = Faraday.new do |builder| - builder.use :http_cache, store: @config.cache_store, serializer: Marshal + builder.use :http_cache, store: @config.cache_store builder.adapter :net_http_persistent end From f61d5c77868a831be827ac51d6f779daaadc4793 Mon Sep 17 00:00:00 2001 From: Zuhaib Siddique Date: Wed, 12 Jul 2017 10:45:49 -0700 Subject: [PATCH 09/13] Rubocop cleanup (#7) * Clean up rubocop and fixes a few error, from 200 offenses to 100 * Remove dup line lenght, set lenght to 150, and one clean up * contains not contaians --- .rubocop.yml | 69 ++++++++++++++++---------------- ext/mkrf_conf.rb | 6 +-- ldclient-rb.gemspec | 4 +- lib/ldclient-rb/config.rb | 2 +- lib/ldclient-rb/evaluation.rb | 32 +++++++-------- lib/ldclient-rb/events.rb | 6 +-- lib/ldclient-rb/feature_store.rb | 3 +- lib/ldclient-rb/ldclient.rb | 16 ++++---- lib/ldclient-rb/polling.rb | 6 +-- lib/ldclient-rb/requestor.rb | 4 +- lib/ldclient-rb/stream.rb | 15 ++++--- spec/ldclient_spec.rb | 4 +- spec/requestor_spec.rb | 10 ++--- 13 files changed, 83 insertions(+), 94 deletions(-) diff --git a/.rubocop.yml b/.rubocop.yml index 5bbd7cde..85b05f8b 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -94,7 +94,7 @@ Rails/Delegate: Description: 'Prefer delegate method for delegations.' Enabled: false -Style/DeprecatedHashMethods: +Style/PreferredHashMethods: Description: 'Checks for use of deprecated Hash methods.' StyleGuide: 'https://github.com/bbatsov/ruby-style-guide#hash-key' Enabled: false @@ -103,11 +103,6 @@ Style/Documentation: Description: 'Document classes and non-namespace modules.' Enabled: false -Style/DotPosition: - Description: 'Checks the position of the dot in multi-line method calls.' - StyleGuide: 'https://github.com/bbatsov/ruby-style-guide#consistent-multi-line-chains' - EnforcedStyle: trailing - Style/DoubleNegation: Description: 'Checks for uses of double negation (!!).' StyleGuide: 'https://github.com/bbatsov/ruby-style-guide#no-bang-bang' @@ -133,10 +128,6 @@ Style/EvenOdd: StyleGuide: 'https://github.com/bbatsov/ruby-style-guide#predicate-methods' Enabled: false -Style/ExtraSpacing: - Description: 'Do not use unnecessary spacing.' - Enabled: true - Style/FileName: Description: 'Use snake_case for source file names.' StyleGuide: 'https://github.com/bbatsov/ruby-style-guide#snake-case-files' @@ -196,9 +187,9 @@ Style/LineEndConcatenation: Enabled: false Metrics/LineLength: - Description: 'Limit lines to 100 characters.' + Description: 'Limit lines to 150 characters.' StyleGuide: 'https://github.com/bbatsov/ruby-style-guide#80-character-limits' - Max: 100 + Max: 150 Metrics/MethodLength: Description: 'Avoid methods longer than 10 lines of code.' @@ -210,13 +201,6 @@ Style/ModuleFunction: StyleGuide: 'https://github.com/bbatsov/ruby-style-guide#module-function' Enabled: false -Style/MultilineOperationIndentation: - Description: >- - Checks indentation of binary operations that span more than - one line. - Enabled: true - EnforcedStyle: indented - Style/NegatedIf: Description: >- Favor unless over if for negative conditions @@ -334,13 +318,14 @@ Style/StringLiterals: EnforcedStyle: double_quotes Enabled: true -Style/TrailingComma: - Description: 'Checks for trailing comma in parameter lists and literals.' - StyleGuide: 'https://github.com/bbatsov/ruby-style-guide#no-trailing-array-commas' - EnforcedStyleForMultiline: comma - SupportedStyles: - - comma - - no_comma +Style/TrailingCommaInArguments: + Description: 'Checks for trailing comma in argument lists.' + StyleGuide: '#no-trailing-params-comma' + Enabled: true + +Style/TrailingCommaInLiteral: + Description: 'Checks for trailing comma in array and hash literals.' + StyleGuide: '#no-trailing-array-commas' Enabled: true Style/TrivialAccessors: @@ -372,6 +357,29 @@ Style/WordArray: StyleGuide: 'https://github.com/bbatsov/ruby-style-guide#percent-w' Enabled: false +# Layout +Layout/DotPosition: + Description: 'Checks the position of the dot in multi-line method calls.' + StyleGuide: 'https://github.com/bbatsov/ruby-style-guide#consistent-multi-line-chains' + EnforcedStyle: trailing + +Layout/ExtraSpacing: + Description: 'Do not use unnecessary spacing.' + Enabled: true + +Layout/MultilineOperationIndentation: + Description: >- + Checks indentation of binary operations that span more than + one line. + Enabled: true + EnforcedStyle: indented + +Layout/InitialIndentation: + Description: >- + Checks the indentation of the first non-blank non-comment line in a file. + Enabled: false + + # Lint Lint/AmbiguousOperator: @@ -434,11 +442,6 @@ Lint/InvalidCharacterLiteral: whitespace character. Enabled: false -Style/InitialIndentation: - Description: >- - Checks the indentation of the first non-blank non-comment line in a file. - Enabled: false - Lint/LiteralInCondition: Description: 'Checks of literals used in conditions.' Enabled: false @@ -560,10 +563,6 @@ Rails/Date: such as Date.today, Date.current etc. Enabled: false -Rails/DefaultScope: - Description: 'Checks if the argument passed to default_scope is a block.' - Enabled: false - Rails/FindBy: Description: 'Prefer find_by over where.first.' Enabled: false diff --git a/ext/mkrf_conf.rb b/ext/mkrf_conf.rb index bb77ec5b..23c2c7b6 100644 --- a/ext/mkrf_conf.rb +++ b/ext/mkrf_conf.rb @@ -1,11 +1,11 @@ -require 'rubygems' +require "rubygems" # From http://stackoverflow.com/questions/5830835/how-to-add-openssl-dependency-to-gemspec # the whole reason this file exists: to return an error if openssl # isn't installed. -require 'openssl' +require "openssl" f = File.open(File.join(File.dirname(__FILE__), "Rakefile"), "w") # create dummy rakefile to indicate success f.write("task :default\n") -f.close \ No newline at end of file +f.close diff --git a/ldclient-rb.gemspec b/ldclient-rb.gemspec index b45a74de..ee640e0a 100644 --- a/ldclient-rb.gemspec +++ b/ldclient-rb.gemspec @@ -1,8 +1,10 @@ # coding: utf-8 + lib = File.expand_path("../lib", __FILE__) $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib) require "ldclient-rb/version" +# rubocop:disable Metrics/BlockLength Gem::Specification.new do |spec| spec.name = "ldclient-rb" spec.version = LaunchDarkly::VERSION @@ -34,7 +36,7 @@ Gem::Specification.new do |spec| spec.add_runtime_dependency "ld-celluloid-eventsource", "~> 0.10.0" spec.add_runtime_dependency "celluloid", "~> 0.18.0.pre" # transitive dep; specified here for more control - if RUBY_VERSION >= '2.2.2' + if RUBY_VERSION >= "2.2.2" spec.add_runtime_dependency "nio4r", "< 3" # for maximum ruby version compatibility. else spec.add_runtime_dependency "nio4r", "~> 1.1" # for maximum ruby version compatibility. diff --git a/lib/ldclient-rb/config.rb b/lib/ldclient-rb/config.rb index 6f02266e..7eb0bb12 100644 --- a/lib/ldclient-rb/config.rb +++ b/lib/ldclient-rb/config.rb @@ -42,6 +42,7 @@ class Config # @option opts [Boolean] :stream (true) Whether or not the streaming API should be used to receive flag updates. # # @return [type] [description] + # rubocop:disable Metrics/AbcSize, Metrics/PerceivedComplexity def initialize(opts = {}) @base_uri = (opts[:base_uri] || Config.default_base_uri).chomp("/") @stream_uri = (opts[:stream_uri] || Config.default_stream_uri).chomp("/") @@ -144,7 +145,6 @@ def offline? # attr_reader :feature_store - # The proxy configuration string # attr_reader :proxy diff --git a/lib/ldclient-rb/evaluation.rb b/lib/ldclient-rb/evaluation.rb index 90df5159..4a6c942f 100644 --- a/lib/ldclient-rb/evaluation.rb +++ b/lib/ldclient-rb/evaluation.rb @@ -1,20 +1,19 @@ require "date" module LaunchDarkly - module Evaluation BUILTINS = [:key, :ip, :country, :email, :firstName, :lastName, :avatar, :name, :anonymous] OPERATORS = { - in: + in: lambda do |a, b| a == b end, - endsWith: + endsWith: lambda do |a, b| (a.is_a? String) && (a.end_with? b) end, - startsWith: + startsWith: lambda do |a, b| (a.is_a? String) && (a.start_with? b) end, @@ -50,7 +49,7 @@ module Evaluation end if b.is_a? String b = DateTime.rfc3339(b).strftime('%Q').to_i - end + end (a.is_a? Numeric) ? a < b : false rescue => e false @@ -60,11 +59,11 @@ module Evaluation lambda do |a, b| begin if a.is_a? String - a = DateTime.rfc3339(a).strftime('%Q').to_i + a = DateTime.rfc3339(a).strftime("%Q").to_i end if b.is_a? String - b = DateTime.rfc3339(b).strftime('%Q').to_i - end + b = DateTime.rfc3339(b).strftime("%Q").to_i + end (a.is_a? Numeric) ? a > b : false rescue => e false @@ -93,15 +92,15 @@ def evaluate(flag, user, store) if flag[:on] res = eval_internal(flag, user, store, events) - return {value: res, events: events} if !res.nil? + return { value: res, events: events } if !res.nil? end if !flag[:offVariation].nil? && flag[:offVariation] < flag[:variations].length value = flag[:variations][flag[:offVariation]] - return {value: value, events: events} + return { value: value, events: events } end - {value: nil, events: events} + { value: nil, events: events } end def eval_internal(flag, user, store, events) @@ -109,7 +108,6 @@ def eval_internal(flag, user, store, events) # Evaluate prerequisites, if any if !flag[:prerequisites].nil? flag[:prerequisites].each do |prerequisite| - prereq_flag = store.get(prerequisite[:key]) if prereq_flag.nil? || !prereq_flag[:on] @@ -119,7 +117,7 @@ def eval_internal(flag, user, store, events) prereq_res = eval_internal(prereq_flag, user, store, events) variation = get_variation(prereq_flag, prerequisite[:variation]) events.push(kind: "feature", key: prereq_flag[:key], value: prereq_res, version: prereq_flag[:version], prereqOf: flag[:key]) - if prereq_res.nil? || prereq_res!= variation + if prereq_res.nil? || prereq_res != variation failed_prereq = true end rescue => exn @@ -149,7 +147,7 @@ def eval_rules(flag, user) end end end - end + end # Check custom rules if !flag[:rules].nil? @@ -202,7 +200,7 @@ def clause_match_user(clause, user) end maybe_negate(clause, match_any(op, val, clause[:values])) - end + end def variation_for_user(rule, user, flag) if !rule[:variation].nil? # fixed variation @@ -234,7 +232,7 @@ def bucket_user(user, key, bucket_by, salt) hash_key = "%s.%s.%s" % [key, salt, id_hash] hash_val = (Digest::SHA1.hexdigest(hash_key))[0..14] - hash_val.to_i(16) / Float(0xFFFFFFFFFFFFFFF) + hash_val.to_i(16) / Float(0xFFFFFFFFFFFFFFF) end def user_value(user, attribute) @@ -260,6 +258,4 @@ def match_any(op, value, values) return false end end - end - diff --git a/lib/ldclient-rb/events.rb b/lib/ldclient-rb/events.rb index b26f2722..1d54b845 100644 --- a/lib/ldclient-rb/events.rb +++ b/lib/ldclient-rb/events.rb @@ -2,7 +2,6 @@ require "faraday" module LaunchDarkly - class EventProcessor def initialize(sdk_key, config) @queue = Queue.new @@ -67,9 +66,8 @@ def add_event(event) else @config.logger.warn("[LDClient] Exceeded event queue capacity. Increase capacity to avoid dropping events.") end - end + end private :create_worker, :post_flushed_events - end -end \ No newline at end of file +end diff --git a/lib/ldclient-rb/feature_store.rb b/lib/ldclient-rb/feature_store.rb index 332ed804..39bc7e1f 100644 --- a/lib/ldclient-rb/feature_store.rb +++ b/lib/ldclient-rb/feature_store.rb @@ -1,7 +1,6 @@ require "concurrent/atomics" module LaunchDarkly - class InMemoryFeatureStore def initialize @features = Hash.new @@ -57,4 +56,4 @@ def initialized? @initialized.value end end -end \ No newline at end of file +end diff --git a/lib/ldclient-rb/ldclient.rb b/lib/ldclient-rb/ldclient.rb index 611bdc23..f057550a 100644 --- a/lib/ldclient-rb/ldclient.rb +++ b/lib/ldclient-rb/ldclient.rb @@ -32,7 +32,7 @@ def initialize(sdk_key, config = Config.default, wait_for_sec = 5) if !@config.offline? if @config.stream? @update_processor = StreamProcessor.new(sdk_key, config, requestor) - else + else @update_processor = PollingProcessor.new(config, requestor) end @update_processor.start @@ -42,7 +42,7 @@ def initialize(sdk_key, config = Config.default, wait_for_sec = 5) if !@config.offline? && wait_for_sec > 0 begin - WaitUtil.wait_for_condition("LaunchDarkly client initialization", :timeout_sec => wait_for_sec, :delay_sec => 0.1) do + WaitUtil.wait_for_condition("LaunchDarkly client initialization", timeout_sec: wait_for_sec, delay_sec: 0.1) do @update_processor.initialized? end rescue WaitUtil::TimeoutError @@ -61,7 +61,7 @@ def toggle?(key, user, default = False) end def secure_mode_hash(user) - OpenSSL::HMAC.hexdigest('sha256', @sdk_key, user[:key].to_s) + OpenSSL::HMAC.hexdigest("sha256", @sdk_key, user[:key].to_s) end # Returns whether the client has been initialized and is ready to serve feature flag requests @@ -107,13 +107,13 @@ def variation(key, user, default) unless user @config.logger.error("[LDClient] Must specify user") - @event_processor.add_event(kind: "feature", key: key, value: default, default: default, user: user) + @event_processor.add_event(kind: "feature", key: key, value: default, default: default, user: user) return default end if !@update_processor.initialized? @config.logger.error("[LDClient] Client has not finished initializing. Returning default value") - @event_processor.add_event(kind: "feature", key: key, value: default, default: default, user: user) + @event_processor.add_event(kind: "feature", key: key, value: default, default: default, user: user) return default end @@ -138,8 +138,8 @@ def variation(key, user, default) return res[:value] else @config.logger.debug("[LDClient] Result value is null in toggle") - @event_processor.add_event(kind: "feature", key: key, user: user, value: default, default: default, version: feature[:version]) - return default + @event_processor.add_event(kind: "feature", key: key, user: user, value: default, default: default, version: feature[:version]) + return default end rescue => exn @config.logger.warn("[LDClient] Error evaluating feature flag: #{exn.inspect}. \nTrace: #{exn.backtrace}") @@ -188,7 +188,7 @@ def all_flags(user) features = @store.all # TODO rescue if necessary - Hash[features.map{|k,f| [k, evaluate(f, user, @store)[:value]] }] + Hash[features.map{ |k, f| [k, evaluate(f, user, @store)[:value]] }] rescue => exn @config.logger.warn("[LDClient] Error evaluating all flags: #{exn.inspect}. \nTrace: #{exn.backtrace}") return Hash.new diff --git a/lib/ldclient-rb/polling.rb b/lib/ldclient-rb/polling.rb index f3e3bf69..7672c1ab 100644 --- a/lib/ldclient-rb/polling.rb +++ b/lib/ldclient-rb/polling.rb @@ -3,7 +3,6 @@ module LaunchDarkly class PollingProcessor - def initialize(config, requestor) @config = config @requestor = requestor @@ -44,13 +43,12 @@ def create_worker end rescue StandardError => exn @config.logger.error("[LDClient] Exception while polling: #{exn.inspect}") - # TODO: log_exception(__method__.to_s, exn) + # TODO: log_exception(__method__.to_s, exn) end end end end - private :poll, :create_worker end -end \ No newline at end of file +end diff --git a/lib/ldclient-rb/requestor.rb b/lib/ldclient-rb/requestor.rb index 1da7c784..86e6e555 100644 --- a/lib/ldclient-rb/requestor.rb +++ b/lib/ldclient-rb/requestor.rb @@ -56,7 +56,5 @@ def make_request(path) end private :make_request - end - -end \ No newline at end of file +end diff --git a/lib/ldclient-rb/stream.rb b/lib/ldclient-rb/stream.rb index 2c432fb2..e33a8b95 100644 --- a/lib/ldclient-rb/stream.rb +++ b/lib/ldclient-rb/stream.rb @@ -27,13 +27,12 @@ def start return unless @started.make_true @config.logger.info("[LDClient] Initializing stream connection") - - headers = - { - 'Authorization' => @sdk_key, - 'User-Agent' => 'RubyClient/' + LaunchDarkly::VERSION - } - opts = {:headers => headers, :with_credentials => true, :proxy => @config.proxy} + headers = + { + "Authorization" => @sdk_key, + "User-Agent" => "RubyClient/" + LaunchDarkly::VERSION + } + opts = { headers: headers, with_credentials: true, proxy: @config.proxy } @es = Celluloid::EventSource.new(@config.stream_uri + "/flags", opts) do |conn| conn.on(PUT) { |message| process_message(message, PUT) } conn.on(PATCH) { |message| process_message(message, PATCH) } @@ -61,7 +60,7 @@ def process_message(message, method) @initialized.make_true @config.logger.info("[LDClient] Stream initialized (via indirect message)") elsif method == INDIRECT_PATCH - @store.upsert(message.data, @requestor.request_flag(message.data)) + @store.upsert(message.data, @requestor.request_flag(message.data)) else @config.logger.warn("[LDClient] Unknown message received: #{method}") end diff --git a/spec/ldclient_spec.rb b/spec/ldclient_spec.rb index 0bd872cd..47ee8f44 100644 --- a/spec/ldclient_spec.rb +++ b/spec/ldclient_spec.rb @@ -3,7 +3,7 @@ describe LaunchDarkly::LDClient do subject { LaunchDarkly::LDClient } - let(:config) { LaunchDarkly::Config.new({:offline => true}) } + let(:config) { LaunchDarkly::Config.new({offline: true}) } let(:client) do subject.new("secret", config) end @@ -33,7 +33,7 @@ describe '#secure_mode_hash' do it "will return the expected value for a known message and secret" do - result = client.secure_mode_hash({:key => :Message}) + result = client.secure_mode_hash({key: :Message}) expect(result).to eq "aa747c502a898200f9e4fa21bac68136f886a0e27aec70ba06daf2e2a5cb5597" end end diff --git a/spec/requestor_spec.rb b/spec/requestor_spec.rb index 21e06ed4..1e26b1dc 100644 --- a/spec/requestor_spec.rb +++ b/spec/requestor_spec.rb @@ -8,8 +8,8 @@ LaunchDarkly::Requestor.new( "key", LaunchDarkly::Config.new({ - :proxy => "http://proxy.com", - :base_uri => "http://ld.com" + proxy: "http://proxy.com", + base_uri: "http://ld.com" }) ) } @@ -17,7 +17,7 @@ faraday = Faraday.new requestor.instance_variable_set(:@client, faraday) allow(faraday).to receive(:get) do |*args, &block| - req = double(Faraday::Request, :headers => {}, :options => Faraday::RequestOptions.new) + req = double(Faraday::Request, headers: {}, options: Faraday::RequestOptions.new) block.call(req) expect(args).to eq ['http://ld.com/sdk/latest-flags'] expect(req.options.proxy[:uri]).to eq URI("http://proxy.com") @@ -32,7 +32,7 @@ LaunchDarkly::Requestor.new( "key", LaunchDarkly::Config.new({ - :base_uri => "http://ld.com" + base_uri: "http://ld.com" }) ) } @@ -40,7 +40,7 @@ faraday = Faraday.new requestor.instance_variable_set(:@client, faraday) allow(faraday).to receive(:get) do |*args, &block| - req = double(Faraday::Request, :headers => {}, :options => Faraday::RequestOptions.new) + req = double(Faraday::Request, headers: {}, options: Faraday::RequestOptions.new) block.call(req) expect(args).to eq ['http://ld.com/sdk/latest-flags'] expect(req.options.proxy).to eq nil From 6c49856898660b60fa64b89372a1bb08492ac3e8 Mon Sep 17 00:00:00 2001 From: Zuhaib Siddique Date: Wed, 26 Jul 2017 09:55:35 -0700 Subject: [PATCH 10/13] Zs/readme update (#8) * gitignore DS_Store * Update readme with note on how to install via gem command * Update grammar --- .gitignore | 3 ++- README.md | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/.gitignore b/.gitignore index 10b25ba0..bb576123 100644 --- a/.gitignore +++ b/.gitignore @@ -12,4 +12,5 @@ *.a mkmf.log *.gem -Gemfile.lock \ No newline at end of file +Gemfile.lock +.DS_Store diff --git a/README.md b/README.md index a3a7cf65..f250ffbc 100644 --- a/README.md +++ b/README.md @@ -14,8 +14,9 @@ Quick setup 0. Install the Ruby SDK with `gem` ```shell -gem install ldclient-rb +gem install ldclient-rb --prerelease ``` +Note: The `--prerelease` flag is there to satisfy the dependency of celluloid 0.18pre which we have tested extensively and have found stable in our use case. Unfortunately, the upstream provider has not promoted this version to stable yet. See [here](https://github.com/celluloid/celluloid/issues/762) This is not required for use in a Gemfile. 1. Require the LaunchDarkly client: From a493ce01628a8ada152b8d2faec38b01d43adfce Mon Sep 17 00:00:00 2001 From: Zuhaib Siddique Date: Wed, 26 Jul 2017 11:49:18 -0700 Subject: [PATCH 11/13] bump patch version --- lib/ldclient-rb/version.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/ldclient-rb/version.rb b/lib/ldclient-rb/version.rb index ac30aecb..7122b4f5 100644 --- a/lib/ldclient-rb/version.rb +++ b/lib/ldclient-rb/version.rb @@ -1,3 +1,3 @@ module LaunchDarkly - VERSION = "2.2.5" + VERSION = "2.2.6" end From 455c8efec29abc651841185d388c6a0b17eb0fc7 Mon Sep 17 00:00:00 2001 From: Zuhaib Siddique Date: Wed, 26 Jul 2017 12:54:30 -0700 Subject: [PATCH 12/13] Fixed a bad merge --- lib/ldclient-rb/config.rb | 3 --- lib/ldclient-rb/stream.rb | 9 --------- spec/requestor_spec.rb | 17 ----------------- 3 files changed, 29 deletions(-) diff --git a/lib/ldclient-rb/config.rb b/lib/ldclient-rb/config.rb index 42ff6672..8945c44b 100644 --- a/lib/ldclient-rb/config.rb +++ b/lib/ldclient-rb/config.rb @@ -145,10 +145,7 @@ def offline? # attr_reader :feature_store -<<<<<<< HEAD -======= ->>>>>>> ba355ed1fc08c6162e2335f60480c0d658b04964 # The proxy configuration string # attr_reader :proxy diff --git a/lib/ldclient-rb/stream.rb b/lib/ldclient-rb/stream.rb index 9f1f9d59..8286b610 100644 --- a/lib/ldclient-rb/stream.rb +++ b/lib/ldclient-rb/stream.rb @@ -27,14 +27,6 @@ def start return unless @started.make_true @config.logger.info("[LDClient] Initializing stream connection") -<<<<<<< HEAD - headers = - { - "Authorization" => @sdk_key, - "User-Agent" => "RubyClient/" + LaunchDarkly::VERSION - } - opts = { headers: headers, with_credentials: true, proxy: @config.proxy } -======= headers = { @@ -42,7 +34,6 @@ def start 'User-Agent' => 'RubyClient/' + LaunchDarkly::VERSION } opts = {:headers => headers, :with_credentials => true, :proxy => @config.proxy} ->>>>>>> ba355ed1fc08c6162e2335f60480c0d658b04964 @es = Celluloid::EventSource.new(@config.stream_uri + "/flags", opts) do |conn| conn.on(PUT) { |message| process_message(message, PUT) } conn.on(PATCH) { |message| process_message(message, PATCH) } diff --git a/spec/requestor_spec.rb b/spec/requestor_spec.rb index c237a34d..21e06ed4 100644 --- a/spec/requestor_spec.rb +++ b/spec/requestor_spec.rb @@ -8,13 +8,8 @@ LaunchDarkly::Requestor.new( "key", LaunchDarkly::Config.new({ -<<<<<<< HEAD - proxy: "http://proxy.com", - base_uri: "http://ld.com" -======= :proxy => "http://proxy.com", :base_uri => "http://ld.com" ->>>>>>> ba355ed1fc08c6162e2335f60480c0d658b04964 }) ) } @@ -22,11 +17,7 @@ faraday = Faraday.new requestor.instance_variable_set(:@client, faraday) allow(faraday).to receive(:get) do |*args, &block| -<<<<<<< HEAD - req = double(Faraday::Request, headers: {}, options: Faraday::RequestOptions.new) -======= req = double(Faraday::Request, :headers => {}, :options => Faraday::RequestOptions.new) ->>>>>>> ba355ed1fc08c6162e2335f60480c0d658b04964 block.call(req) expect(args).to eq ['http://ld.com/sdk/latest-flags'] expect(req.options.proxy[:uri]).to eq URI("http://proxy.com") @@ -41,11 +32,7 @@ LaunchDarkly::Requestor.new( "key", LaunchDarkly::Config.new({ -<<<<<<< HEAD - base_uri: "http://ld.com" -======= :base_uri => "http://ld.com" ->>>>>>> ba355ed1fc08c6162e2335f60480c0d658b04964 }) ) } @@ -53,11 +40,7 @@ faraday = Faraday.new requestor.instance_variable_set(:@client, faraday) allow(faraday).to receive(:get) do |*args, &block| -<<<<<<< HEAD - req = double(Faraday::Request, headers: {}, options: Faraday::RequestOptions.new) -======= req = double(Faraday::Request, :headers => {}, :options => Faraday::RequestOptions.new) ->>>>>>> ba355ed1fc08c6162e2335f60480c0d658b04964 block.call(req) expect(args).to eq ['http://ld.com/sdk/latest-flags'] expect(req.options.proxy).to eq nil From 77b692a56dcd7d7a8bd737404ed2fb0fbe32ad57 Mon Sep 17 00:00:00 2001 From: Zuhaib Siddique Date: Wed, 26 Jul 2017 13:47:32 -0700 Subject: [PATCH 13/13] new release script --- script/release.sh | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100755 script/release.sh diff --git a/script/release.sh b/script/release.sh new file mode 100755 index 00000000..18537846 --- /dev/null +++ b/script/release.sh @@ -0,0 +1,27 @@ +#!/usr/bin/env bash +# This script updates the version for the ldclient library and releases it to RubyGems +# It will only work if you have the proper credentials set up in ~/.gem/credentials + +# It takes exactly one argument: the new version. +# It should be run from the root of this git repo like this: +# ./scripts/release.sh 4.0.9 + +# When done you should commit and push the changes made. + +set -uxe +echo "Starting ruby-client release." + +VERSION=$1 + +#Update version in ldclient/version.py +VERSION_RB_TEMP=./version.rb.tmp +sed "s/VERSION =.*/VERSION = \"${VERSION}\"/g" lib/ldclient-rb/version.rb > ${VERSION_RB_TEMP} +mv ${VERSION_RB_TEMP} lib/ldclient-rb/version.rb + +# Build Ruby Gem +gem build ldclient-rb.gemspec + +# Publish Ruby Gem +gem push ldclient-rb-${VERSION}.gem + +echo "Done with ruby-client release" \ No newline at end of file