Skip to content

Commit 79113bc

Browse files
committed
chore: upgrade to Ruby SDK v3
1 parent db5d39c commit 79113bc

6 files changed

Lines changed: 94 additions & 38 deletions

File tree

‎.github/workflows/checks.yml‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,5 +32,8 @@ jobs:
3232
- name: Check syntax
3333
run: bundle exec ruby -c app.rb
3434

35+
- name: Lint
36+
run: bundle exec rubocop --cache false
37+
3538
- name: Run application
3639
run: bundle exec ruby app.rb

‎Gemfile‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ source 'https://rubygems.org'
33
ruby '>= 3.0.0'
44

55
# Add your gems here
6+
gem 'featurevisor', '~> 3.0'
67
gem 'rake', '~> 13.0'
7-
gem 'featurevisor', '~> 2.0'
88

99
group :development do
1010
gem 'rubocop', '~> 1.50'

‎Gemfile.lock‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ GEM
33
specs:
44
ast (2.4.3)
55
benchmark (0.5.0)
6-
featurevisor (2.0.0)
6+
featurevisor (3.0.0)
77
benchmark (>= 0, < 1)
88
json (2.21.1)
99
language_server-protocol (3.17.0.6)
@@ -41,7 +41,7 @@ PLATFORMS
4141
ruby
4242

4343
DEPENDENCIES
44-
featurevisor (~> 2.0)
44+
featurevisor (~> 3.0)
4545
rake (~> 13.0)
4646
rubocop (~> 1.50)
4747

‎README.md‎

Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,51 @@
11
# featurevisor-example-ruby
22

3-
Example Ruby application that demonstrates how to use Featurevisor [Ruby SDK](https://github.com/featurevisor/featurevisor-ruby).
3+
A small Ruby application showing how to use the [Featurevisor Ruby SDK](https://github.com/featurevisor/featurevisor-ruby).
44

55
Learn more about Featurevisor [here](https://featurevisor.com).
66

7-
## Installation
7+
It fetches a production datafile, creates a Featurevisor instance with customer context, then evaluates a flag, a variation, feature variables, and global variables.
88

9-
Clone the repository, and run:
9+
## Installation
1010

11+
```sh
12+
bundle install
1113
```
12-
$ bundle install
14+
15+
## Run the example
16+
17+
```sh
18+
bundle exec ruby app.rb
1319
```
1420

15-
## Usage
21+
The application evaluates `commerce_platform`, `checkout_experience`, `max_items`, `payment_methods`, `serviceEndpoints`, and `supportContact` from this datafile:
22+
23+
```text
24+
https://featurevisor-example-cloudflare.pages.dev/production/featurevisor-sdk-v3.json
25+
```
1626

17-
Run the server locally, running:
27+
Expected output:
1828

29+
```text
30+
Commerce platform enabled: true
31+
Checkout variation: express
32+
Maximum checkout items: 25
33+
Payment methods: ["card", "wallet"]
34+
Service endpoint: https://api.eu.example.com (timeout: 1200 ms, retries: 4)
35+
Support contact: support-nl@example.com
1936
```
20-
$ rake run
37+
38+
Change the context in `app.rb` to see how Featurevisor selects different rules, variations, and global variable overrides.
39+
40+
## Checks
41+
42+
```sh
43+
bundle exec rubocop
44+
bundle exec ruby app.rb
2145
```
2246

2347
## Featurevisor project
2448

25-
Uses this Featurevisor project to fetch the configuration from: https://github.com/featurevisor/featurevisor-example-cloudflare
49+
The datafile is generated by the [Featurevisor Cloudflare example](https://github.com/featurevisor/featurevisor-example-cloudflare).
50+
51+
Learn more in the [Featurevisor Ruby SDK documentation](https://featurevisor.com/docs/sdks/ruby/).

‎Rakefile‎

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,33 +4,33 @@ require 'rake'
44
task default: [:run]
55

66
# Run the Hello World program
7-
desc "Run the Hello World program with Featurevisor"
7+
desc 'Run the Hello World program with Featurevisor'
88
task :run do
9-
puts "Running Ruby Hello World program with Featurevisor..."
9+
puts 'Running Ruby Hello World program with Featurevisor...'
1010
puts
11-
system("ruby app.rb")
11+
system('ruby app.rb')
1212
end
1313

1414
# Clean temporary files
15-
desc "Clean temporary files"
15+
desc 'Clean temporary files'
1616
task :clean do
17-
puts "Cleaning temporary files..."
18-
FileList["*.log", "*.tmp", "*.temp"].each do |file|
19-
File.delete(file) if File.exist?(file)
17+
puts 'Cleaning temporary files...'
18+
FileList['*.log', '*.tmp', '*.temp'].each do |file|
19+
FileUtils.rm_f(file)
2020
end
21-
puts "Cleanup complete!"
21+
puts 'Cleanup complete!'
2222
end
2323

2424
# Install dependencies
25-
desc "Install gem dependencies"
25+
desc 'Install gem dependencies'
2626
task :install do
27-
puts "Installing gem dependencies..."
28-
system("bundle install")
27+
puts 'Installing gem dependencies...'
28+
system('bundle install')
2929
end
3030

3131
# Run RuboCop
32-
desc "Run RuboCop for code style checking"
32+
desc 'Run RuboCop for code style checking'
3333
task :lint do
34-
puts "Running RuboCop..."
35-
system("bundle exec rubocop")
34+
puts 'Running RuboCop...'
35+
system('bundle exec rubocop')
3636
end

‎app.rb‎

100644100755
Lines changed: 40 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,50 @@
11
#!/usr/bin/env ruby
22

33
require 'featurevisor'
4-
require 'net/http'
54
require 'json'
5+
require 'net/http'
6+
7+
DATAFILE_URL = 'https://featurevisor-example-cloudflare.pages.dev/production/featurevisor-sdk-v3.json'.freeze
8+
9+
uri = URI(DATAFILE_URL)
10+
response = Net::HTTP.start(
11+
uri.host,
12+
uri.port,
13+
use_ssl: uri.scheme == 'https',
14+
open_timeout: 10,
15+
read_timeout: 10
16+
) { |http| http.get(uri.request_uri, { 'Accept' => 'application/json' }) }
617

7-
##
8-
# Fetch datafile
9-
#
10-
datafile_url = "https://featurevisor-example-cloudflare.pages.dev/production/featurevisor-tag-all.json"
11-
response = Net::HTTP.get(URI(datafile_url))
12-
datafile_content = JSON.parse(response, symbolize_names: true)
18+
unless response.is_a?(Net::HTTPSuccess)
19+
raise "Datafile request failed with HTTP #{response.code}"
20+
end
1321

14-
# Create Featurevisor instance
1522
f = Featurevisor.create_featurevisor(
16-
datafile: datafile_content,
23+
datafile: JSON.parse(response.body, symbolize_names: true),
24+
log_level: 'error',
25+
context: {
26+
userId: 'customer-123',
27+
country: 'nl',
28+
locale: 'nl-NL',
29+
accountPlan: 'pro'
30+
}
1731
)
1832

19-
# Get the feature flag value
20-
feature_flag_value = f.is_enabled("my_feature")
33+
begin
34+
commerce_enabled = f.is_enabled('commerce_platform')
35+
checkout_variation = f.get_variation('checkout_experience')
36+
max_items = f.get_variable_integer('checkout_experience', 'max_items')
37+
payment_methods = f.get_variable_array('checkout_experience', 'payment_methods')
38+
endpoints = f.get_variable_object('serviceEndpoints')
39+
support_contact = f.get_variable_string('supportContact')
2140

22-
# Print results to terminal
23-
puts "Feature flag 'my_feature' is enabled: #{feature_flag_value}"
41+
puts "Commerce platform enabled: #{commerce_enabled}"
42+
puts "Checkout variation: #{checkout_variation || 'unavailable'}"
43+
puts "Maximum checkout items: #{max_items || 'unavailable'}"
44+
puts "Payment methods: #{payment_methods || []}"
45+
puts "Service endpoint: #{endpoints[:baseUrl]} " \
46+
"(timeout: #{endpoints[:timeoutMs]} ms, retries: #{endpoints[:retries]})"
47+
puts "Support contact: #{support_contact || 'unavailable'}"
48+
ensure
49+
f.close
50+
end

0 commit comments

Comments
 (0)