|
1 | 1 | # frozen_string_literal: true |
2 | 2 |
|
3 | | -class DevCycleProvider |
| 3 | +module DevCycle |
| 4 | + class Provider |
| 5 | + def initialize(client) |
| 6 | + unless client.is_a?(DevCycleClient) do |
| 7 | + fail ArgumentError('Client must be an instance of DevCycleClient') |
| 8 | + end |
| 9 | + @client = client |
| 10 | + end |
| 11 | + end |
4 | 12 |
|
5 | | - def initialize(client) |
6 | | - @client = client |
7 | | - end |
8 | | - def init |
9 | | - # We handle all initialization on the DVC Client itself |
10 | | - end |
| 13 | + def init |
| 14 | + # We handle all initialization on the DVC Client itself |
| 15 | + end |
11 | 16 |
|
12 | | - def shutdown |
13 | | - @client.close() |
14 | | - end |
| 17 | + def shutdown |
| 18 | + @client.close |
| 19 | + end |
15 | 20 |
|
16 | | - def fetch_boolean_value(flag_key:, default_value:, evaluation_context: nil) |
| 21 | + def fetch_boolean_value(flag_key:, default_value:, evaluation_context: nil) end |
17 | 22 |
|
18 | | - end |
| 23 | + def fetch_string_value(flag_key:, default_value:, evaluation_context: nil) |
| 24 | + # Retrieve a string value from provider source |
| 25 | + end |
19 | 26 |
|
20 | | - def fetch_string_value(flag_key:, default_value:, evaluation_context: nil) |
21 | | - # Retrieve a string value from provider source |
22 | | - end |
| 27 | + def fetch_number_value(flag_key:, default_value:, evaluation_context: nil) |
| 28 | + # Retrieve a numeric value from provider source |
| 29 | + end |
23 | 30 |
|
24 | | - def fetch_number_value(flag_key:, default_value:, evaluation_context: nil) |
25 | | - # Retrieve a numeric value from provider source |
26 | | - end |
| 31 | + def fetch_integer_value(flag_key:, default_value:, evaluation_context: nil) |
| 32 | + # Retrieve a integer value from provider source |
| 33 | + end |
27 | 34 |
|
28 | | - def fetch_integer_value(flag_key:, default_value:, evaluation_context: nil) |
29 | | - # Retrieve a integer value from provider source |
30 | | - end |
| 35 | + def fetch_float_value(flag_key:, default_value:, evaluation_context: nil) |
| 36 | + # Retrieve a float value from provider source |
| 37 | + end |
31 | 38 |
|
32 | | - def fetch_float_value(flag_key:, default_value:, evaluation_context: nil) |
33 | | - # Retrieve a float value from provider source |
34 | | - end |
| 39 | + def fetch_object_value(flag_key:, default_value:, evaluation_context: nil) |
| 40 | + # Retrieve a hash value from provider source |
| 41 | + end |
35 | 42 |
|
36 | | - def fetch_object_value(flag_key:, default_value:, evaluation_context: nil) |
37 | | - # Retrieve a hash value from provider source |
| 43 | + def self.user_from_openfeature_context(context) |
| 44 | + unless context.is_a?(OpenFeature::SDK::EvaluationContext) |
| 45 | + raise ArgumentError, "Invalid context type, expected OpenFeature::SDK::EvaluationContext but got #{context.class}" |
| 46 | + end |
| 47 | + args = {} |
| 48 | + if context.field('user_id') |
| 49 | + args.merge!(user_id: context.field('user_id')) |
| 50 | + elsif context.field('targeting_key') |
| 51 | + args.merge!(user_id: context.field('targeting_key')) |
| 52 | + end |
| 53 | + customData = {} |
| 54 | + privateCustomData = {} |
| 55 | + context.fields.each do |field, value| |
| 56 | + if field === 'user_id' || field === 'targeting_key' |
| 57 | + next |
| 58 | + end |
| 59 | + case field |
| 60 | + when 'email' |
| 61 | + args.merge!(email: value) |
| 62 | + when 'name' |
| 63 | + args.merge!(name: value) |
| 64 | + when 'language' |
| 65 | + args.merge!(language: value) |
| 66 | + when 'country' |
| 67 | + args.merge!(country: value) |
| 68 | + when 'appVersion' |
| 69 | + if value.is_a?(String) |
| 70 | + args.merge!(appVersion: value) |
| 71 | + end |
| 72 | + next |
| 73 | + when 'appBuild' |
| 74 | + if value.is_a?(Numeric) |
| 75 | + args.merge!(appBuild: value) |
| 76 | + end |
| 77 | + when 'customData' |
| 78 | + if value.is_a?(Hash) |
| 79 | + customData.merge!(value) |
| 80 | + end |
| 81 | + next |
| 82 | + when 'privateCustomData' |
| 83 | + if value.is_a?(Hash) |
| 84 | + privateCustomData.merge!(value) |
| 85 | + end |
| 86 | + else |
| 87 | + customData.merge!(field => value) |
| 88 | + end |
| 89 | + end |
| 90 | + args.merge!(customData: customData) |
| 91 | + args.merge!(privateCustomData: privateCustomData) |
| 92 | + User.new(**args) |
| 93 | + end |
38 | 94 | end |
39 | | - |
40 | | - |
41 | 95 | end |
0 commit comments