Class: Client
- Inherits:
-
Object
- Object
- Client
- Defined in:
- lib/testlab_sdk_ruby/testlab_client.rb
Instance Attribute Summary collapse
-
#config ⇒ Object
Returns the value of attribute config.
-
#context ⇒ Object
Returns the value of attribute context.
-
#features ⇒ Object
Returns the value of attribute features.
Instance Method Summary collapse
- #add_default_context ⇒ Object
- #create_event(variant_id, user_id) ⇒ Object
- #create_user(id, variant_id, ip_address) ⇒ Object
- #disconnect ⇒ Object
- #fetch_features ⇒ Object
- #get_feature_value(name) ⇒ Object
- #get_ip ⇒ Object
- #get_users ⇒ Object
-
#initialize(config) ⇒ Client
constructor
A new instance of Client.
- #timed_fetch(interval) ⇒ Object
- #update_context(new_context) ⇒ Object
Constructor Details
#initialize(config) ⇒ Client
Returns a new instance of Client.
8 9 10 11 12 13 |
# File 'lib/testlab_sdk_ruby/testlab_client.rb', line 8 def initialize(config) @config = config @context = nil @features = {} @process_thread = nil end |
Instance Attribute Details
#config ⇒ Object
Returns the value of attribute config.
6 7 8 |
# File 'lib/testlab_sdk_ruby/testlab_client.rb', line 6 def config @config end |
#context ⇒ Object
Returns the value of attribute context.
6 7 8 |
# File 'lib/testlab_sdk_ruby/testlab_client.rb', line 6 def context @context end |
#features ⇒ Object
Returns the value of attribute features.
6 7 8 |
# File 'lib/testlab_sdk_ruby/testlab_client.rb', line 6 def features @features end |
Instance Method Details
#add_default_context ⇒ Object
15 16 17 |
# File 'lib/testlab_sdk_ruby/testlab_client.rb', line 15 def add_default_context self.context = { user_id: SecureRandom.uuid, ip: get_ip } end |
#create_event(variant_id, user_id) ⇒ Object
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 |
# File 'lib/testlab_sdk_ruby/testlab_client.rb', line 121 def create_event(variant_id, user_id) url = "#{config[:server_address]}/api/events" response = HTTParty.post( url, { body: { variant_id: variant_id, user_id: user_id }.to_json, headers: { "Content-Type" => "application/json", }, }, ) if response.code == 200 return response.parsed_response else puts "Error creating user: #{response.body}" return response.parsed_response end end |
#create_user(id, variant_id, ip_address) ⇒ Object
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/testlab_sdk_ruby/testlab_client.rb', line 96 def create_user(id, variant_id, ip_address) url = "#{config[:server_address]}/api/users" response = HTTParty.post( url, { body: { id: id, variant_id: variant_id, ip_address: ip_address, }.to_json, headers: { "Content-Type" => "application/json", }, }, ) if response.code == 200 return response.parsed_response else puts "Error creating user: #{response.body}" return response.parsed_response end end |
#disconnect ⇒ Object
65 66 67 68 |
# File 'lib/testlab_sdk_ruby/testlab_client.rb', line 65 def disconnect # Stop the previous process if it exists @process_thread&.kill end |
#fetch_features ⇒ Object
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/testlab_sdk_ruby/testlab_client.rb', line 70 def fetch_features url = "#{config[:server_address]}/api/feature/current" if not features.empty? last_modified = Time.now - config[:interval] response = HTTParty.get( url, headers: { "If-Modified-Since" => last_modified.httpdate, }, ) self.features = response.parsed_response if response.code == 200 else response = HTTParty.get(url) self.features = response.parsed_response end end |
#get_feature_value(name) ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/testlab_sdk_ruby/testlab_client.rb', line 28 def get_feature_value(name) feature = features["experiments"] .concat(features["toggles"], features["rollouts"]) .find { |f| f["name"] == name } return false unless feature if feature["type_id"] != 3 return is_enabled(features, name, context[:user_id]) else enabled = is_enabled(features, name, context[:user_id]) variant = get_variant(features, name, context[:user_id]) users = get_users existing_user = users.find do |user| user["id"] == context[:user_id] && user["variant_id"] == variant["id"] end if enabled && variant && !existing_user create_user(context[:user_id], variant[:id], context[:ip]) end enabled && variant end end |
#get_ip ⇒ Object
23 24 25 26 |
# File 'lib/testlab_sdk_ruby/testlab_client.rb', line 23 def get_ip response = HTTParty.get("https://ipapi.co/json/") response.parsed_response["ip"] end |
#get_users ⇒ Object
90 91 92 93 94 |
# File 'lib/testlab_sdk_ruby/testlab_client.rb', line 90 def get_users url = "#{config[:server_address]}/api/users" response = HTTParty.get(url) response.parsed_response end |
#timed_fetch(interval) ⇒ Object
53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/testlab_sdk_ruby/testlab_client.rb', line 53 def timed_fetch(interval) disconnect @process_thread = Thread.new do loop do fetch_features sleep interval end end end |
#update_context(new_context) ⇒ Object
19 20 21 |
# File 'lib/testlab_sdk_ruby/testlab_client.rb', line 19 def update_context(new_context) self.context = context.merge(new_context) end |