Skip to content

Commit

Permalink
chore: Rubocop (#220)
Browse files Browse the repository at this point in the history
  • Loading branch information
markphelps authored Apr 4, 2024
1 parent 3340d52 commit 2227e45
Show file tree
Hide file tree
Showing 9 changed files with 79 additions and 42 deletions.
19 changes: 19 additions & 0 deletions .github/workflows/lint-sdks.yml
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,22 @@ jobs:
- name: Lint Java code
working-directory: flipt-client-java
run: ./gradlew spotlessCheck

lint-ruby:
name: Lint Ruby
runs-on: ubuntu-latest
steps:
- name: Checkout Sources
uses: actions/checkout@v4

- name: Setup Ruby
uses: ruby/setup-ruby@v1
with:
ruby-version: "3.2"

- name: Lint Ruby code
working-directory: flipt-client-ruby
run: |
gem install bundler
bundle install
bundle exec rubocop
22 changes: 22 additions & 0 deletions flipt-client-ruby/.rubocop.yml
Original file line number Diff line number Diff line change
@@ -1 +1,23 @@
inherit_from: .rubocop_todo.yml

Lint/MissingSuper:
Enabled: false

Lint/UselessAssignment:
Enabled: false

Metrics/BlockLength:
Max: 35
Exclude:
- "spec/**/*"
- "test/**/*"

Layout/LineLength:
Exclude:
- "spec/**/*"
- "test/**/*"

Style/Documentation:
Exclude:
- "spec/**/*"
- "test/**/*"
21 changes: 1 addition & 20 deletions flipt-client-ruby/.rubocop_todo.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,4 @@
# Include: **/*.gemspec
Gemspec/RequiredRubyVersion:
Exclude:
- 'flipt-client-ruby.gemspec'

# Offense count: 1
Lint/ShadowingOuterLocalVariable:
Exclude:
- 'lib/evaluation.rb'

# Offense count: 1
# Configuration parameters: CountComments, CountAsOne, AllowedMethods, AllowedPatterns.
# AllowedMethods: refine
Metrics/BlockLength:
Max: 35

# Offense count: 1
# Configuration parameters: AllowedConstants.
Style/Documentation:
Exclude:
- 'spec/**/*'
- 'test/**/*'
- 'lib/evaluation.rb'
- "flipt-client-ruby.gemspec"
2 changes: 1 addition & 1 deletion flipt-client-ruby/Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
flipt_client (0.0.4)
flipt_client (0.5.0)

GEM
remote: https://rubygems.org/
Expand Down
2 changes: 1 addition & 1 deletion flipt-client-ruby/flipt-client-ruby.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ Gem::Specification.new do |spec|
# spec.metadata['allowed_push_host'] = "TODO: Set to 'http://mygemserver.com'"

spec.metadata['homepage_uri'] = spec.homepage
spec.metadata["source_code_uri"] = "https://github.com/flipt-io/flipt-client-sdks"
spec.metadata['source_code_uri'] = 'https://github.com/flipt-io/flipt-client-sdks'

spec.files = Dir.glob('{lib}/**/*') + ['README.md', 'flipt-client-ruby.gemspec']
spec.bindir = 'exe'
Expand Down
9 changes: 7 additions & 2 deletions flipt-client-ruby/lib/flipt_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
module Flipt
class Error < StandardError; end

# EvaluationClient is a Ruby Client Side Evaluation Library for Flipt
class EvaluationClient
extend FFI::Library

Expand Down Expand Up @@ -56,7 +57,10 @@ def initialize(namespace = 'default', opts = {})

# set default no auth if not provided
authentication = opts.fetch(:authentication, Flipt::NoAuthentication.new)
raise ArgumentError, "invalid authentication strategy" unless authentication && authentication.is_a?(Flipt::AuthenticationStrategy)
unless authentication.is_a?(Flipt::AuthenticationStrategy)
raise ArgumentError,
'invalid authentication strategy'
end

opts[:authentication] = authentication.strategy

Expand Down Expand Up @@ -98,7 +102,7 @@ def evaluate_boolean(evaluation_request = {})
# - :entity_id [String] entity id
# - :flag_key [String] flag key
def evaluate_batch(batch_evaluation_request = [])
for request in batch_evaluation_request do
batch_evaluation_request.each do |request|
validate_evaluation_request(request)
end

Expand All @@ -115,6 +119,7 @@ def list_flags
end

private

def validate_evaluation_request(evaluation_request)
if evaluation_request[:entity_id].nil? || evaluation_request[:entity_id].empty?
raise ArgumentError, 'entity_id is required'
Expand Down
10 changes: 7 additions & 3 deletions flipt-client-ruby/lib/flipt_client/models.rb
Original file line number Diff line number Diff line change
@@ -1,21 +1,24 @@
# frozen_string_literal: true

module Flipt
# AuthenticationStrategy is a base class for different authentication strategies
class AuthenticationStrategy
def strategy
raise NotImplementedError
raise NotImplementedError
end
end

# NoAuthentication is a strategy that does not require authentication
class NoAuthentication < AuthenticationStrategy
def strategy
nil
end
end

# ClientTokenAuthentication is a strategy that uses a client token for authentication
class ClientTokenAuthentication < AuthenticationStrategy
def initialize(token)
@token = token
@token = token
end

def strategy
Expand All @@ -25,6 +28,7 @@ def strategy
end
end

# JWTAuthentication is a strategy that uses a JWT token for authentication
class JWTAuthentication < AuthenticationStrategy
def initialize(token)
@token = token
Expand All @@ -36,4 +40,4 @@ def strategy
}
end
end
end
end
20 changes: 11 additions & 9 deletions flipt-client-ruby/loadtest.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
require_relative "lib/flipt_client"
# frozen_string_literal: true

# note: this script assumes you have built the flipt-client Ruby gem locally via the instructions in the README
require_relative 'lib/flipt_client'

# NOTE: this script assumes you have built the flipt-client Ruby gem locally via the instructions in the README
# and that the flag "my-feature" exists in the default namespace
# and that the flag is a boolean flag

FLIPT_URL = ENV.fetch("FLIPT_URL", "http://localhost:8080")
FLIPT_URL = ENV.fetch('FLIPT_URL', 'http://localhost:8080')

NUM_CLIENTS = 100
NUM_REQUESTS_PER_CLIENT = 1000
Expand All @@ -13,19 +15,19 @@
clients = []

NUM_CLIENTS.times do
clients << Flipt::EvaluationClient.new("default", {url: FLIPT_URL, auth_token: "secret"})
clients << Flipt::EvaluationClient.new('default', { url: FLIPT_URL, auth_token: 'secret' })
end

# capture start time
start_time = Time.now

# each client will make 1000 requests
clients.each do |client|
NUM_REQUESTS_PER_CLIENT.times do
entity_id = rand(1000000).to_s
resp = client.evaluate_boolean({flag_key: "my-feature", entity_id: entity_id})
end
NUM_REQUESTS_PER_CLIENT.times do
entity_id = rand(1_000_000).to_s
resp = client.evaluate_boolean({ flag_key: 'my-feature', entity_id: entity_id })
end
end

puts "done: #{clients.length} clients made #{clients.length * NUM_REQUESTS_PER_CLIENT} requests in total"
puts "total time: #{Time.now - start_time} seconds"
puts "total time: #{Time.now - start_time} seconds"
16 changes: 10 additions & 6 deletions flipt-client-ruby/spec/evaluation_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
before(:all) do
url = ENV.fetch('FLIPT_URL', 'http://localhost:8080')
auth_token = ENV.fetch('FLIPT_AUTH_TOKEN', 'secret')
@client = Flipt::EvaluationClient.new('default', { url: url, authentication: Flipt::ClientTokenAuthentication.new(auth_token)} )
@client = Flipt::EvaluationClient.new('default',
{ url: url,
authentication: Flipt::ClientTokenAuthentication.new(auth_token) })
end

describe '#evaluate_variant' do
Expand All @@ -26,7 +28,8 @@

describe '#evaluate_boolean' do
it 'returns a boolean result' do
resp = @client.evaluate_boolean({ flag_key: 'flag_boolean', entity_id: 'someentity', context: { "fizz": 'buzz' } })
resp = @client.evaluate_boolean({ flag_key: 'flag_boolean', entity_id: 'someentity',
context: { "fizz": 'buzz' } })

expect(resp).to_not be_nil
expect(resp['status']).to eq('success')
Expand All @@ -39,22 +42,23 @@

describe '#evaluate_batch' do
it 'returns a batch result' do
resp = @client.evaluate_batch([{ flag_key: 'flag1', entity_id: 'someentity', context: { "fizz": 'buzz' } }, { flag_key: 'flag_boolean', entity_id: 'someentity', context: { "fizz": 'buzz' } }, { flag_key: 'notfound', entity_id: 'someentity', context: { "fizz": 'buzz' } }])
resp = @client.evaluate_batch([{ flag_key: 'flag1', entity_id: 'someentity', context: { "fizz": 'buzz' } },
{ flag_key: 'flag_boolean', entity_id: 'someentity', context: { "fizz": 'buzz' } }, { flag_key: 'notfound', entity_id: 'someentity', context: { "fizz": 'buzz' } }])

expect(resp).to_not be_nil
expect(resp['status']).to eq('success')
expect(resp['error_message']).to be_nil

expect(resp['result']['responses'].length).to be == 3

variant = resp['result']['responses'][0]
expect(variant['type']).to eq('VARIANT_EVALUATION_RESPONSE_TYPE')
expect(variant['variant_evaluation_response']['flag_key']).to eq('flag1')
expect(variant['variant_evaluation_response']['match']).to eq(true)
expect(variant['variant_evaluation_response']['reason']).to eq('MATCH_EVALUATION_REASON')
expect(variant['variant_evaluation_response']['variant_key']).to eq('variant1')
expect(variant['variant_evaluation_response']['segment_keys']).to eq(['segment1'])

boolean = resp['result']['responses'][1]
expect(boolean['type']).to eq('BOOLEAN_EVALUATION_RESPONSE_TYPE')
expect(boolean['boolean_evaluation_response']['flag_key']).to eq('flag_boolean')
Expand Down Expand Up @@ -98,7 +102,7 @@
expect(resp).to_not be_nil
expect(resp['status']).to eq('success')
expect(resp['error_message']).to be_nil
expect(resp['result']).to include({"enabled" => true, "key" => "flag_boolean", "type" => "BOOLEAN_FLAG_TYPE"})
expect(resp['result']).to include({ 'enabled' => true, 'key' => 'flag_boolean', 'type' => 'BOOLEAN_FLAG_TYPE' })
end
end
end

0 comments on commit 2227e45

Please sign in to comment.