Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cleanup code, update readme #88

Closed
wants to merge 19 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@
*.a
mkmf.log
*.gem
Gemfile.lock
Gemfile.lock
.DS_Store
69 changes: 34 additions & 35 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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'
Expand All @@ -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'
Expand Down Expand Up @@ -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.'
Expand All @@ -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
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand Down
6 changes: 3 additions & 3 deletions ext/mkrf_conf.rb
Original file line number Diff line number Diff line change
@@ -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
f.close
6 changes: 4 additions & 2 deletions ldclient-rb.gemspec
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -33,8 +35,8 @@ Gem::Specification.new do |spec|
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'

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.
Expand Down
2 changes: 1 addition & 1 deletion lib/ldclient-rb/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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("/")
Expand Down Expand Up @@ -144,7 +145,6 @@ def offline?
#
attr_reader :feature_store


# The proxy configuration string
#
attr_reader :proxy
Expand Down
32 changes: 14 additions & 18 deletions lib/ldclient-rb/evaluation.rb
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -93,23 +92,22 @@ 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)
failed_prereq = false
# 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]
Expand All @@ -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
Expand Down Expand Up @@ -149,7 +147,7 @@ def eval_rules(flag, user)
end
end
end
end
end

# Check custom rules
if !flag[:rules].nil?
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -260,6 +258,4 @@ def match_any(op, value, values)
return false
end
end

end

6 changes: 2 additions & 4 deletions lib/ldclient-rb/events.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
require "faraday"

module LaunchDarkly

class EventProcessor
def initialize(sdk_key, config)
@queue = Queue.new
Expand Down Expand Up @@ -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
end
3 changes: 1 addition & 2 deletions lib/ldclient-rb/feature_store.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
require "concurrent/atomics"

module LaunchDarkly

class InMemoryFeatureStore
def initialize
@features = Hash.new
Expand Down Expand Up @@ -57,4 +56,4 @@ def initialized?
@initialized.value
end
end
end
end
Loading