|
1 | 1 | #!/usr/bin/env ruby |
2 | 2 |
|
3 | 3 | require 'featurevisor' |
4 | | -require 'net/http' |
5 | 4 | 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' }) } |
6 | 17 |
|
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 |
13 | 21 |
|
14 | | -# Create Featurevisor instance |
15 | 22 | 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 | + } |
17 | 31 | ) |
18 | 32 |
|
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') |
21 | 40 |
|
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