From 479d51d5c8584ddd815411eaa1c950ffbd6d93f3 Mon Sep 17 00:00:00 2001 From: Renato Arruda Date: Thu, 5 Sep 2019 23:47:42 +0200 Subject: [PATCH] #20 - Added support for static context fields --- lib/unleash/context.rb | 4 +++- spec/unleash/context_spec.rb | 19 +++++++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/lib/unleash/context.rb b/lib/unleash/context.rb index 24dff137..ee45ffa6 100644 --- a/lib/unleash/context.rb +++ b/lib/unleash/context.rb @@ -1,11 +1,13 @@ module Unleash class Context - attr_accessor :user_id, :session_id, :remote_address, :properties + attr_accessor :app_name, :environment, :user_id, :session_id, :remote_address, :properties def initialize(params = {}) raise ArgumentError, "Unleash::Context must be initialized with a hash." unless params.is_a?(Hash) + self.app_name = params.values_at('appName', :app_name).compact.first || ( !Unleash.configuration.nil? ? Unleash.configuration.app_name : nil ) + self.environment = params.values_at('environment', :environment).compact.first || 'default' self.user_id = params.values_at('userId', :user_id).compact.first || '' self.session_id = params.values_at('sessionId', :session_id).compact.first || '' self.remote_address = params.values_at('remoteAddress', :remote_address).compact.first || '' diff --git a/spec/unleash/context_spec.rb b/spec/unleash/context_spec.rb index 65317959..78f24ad1 100644 --- a/spec/unleash/context_spec.rb +++ b/spec/unleash/context_spec.rb @@ -42,4 +42,23 @@ context = Unleash::Context.new(params) expect(context.properties).to eq({}) end + + it "will correctly use default values when using empty hash and client is not configured" do + params = Hash.new + context = Unleash::Context.new(params) + expect(context.app_name).to be_nil + expect(context.environment).to eq('default') + end + + it "will correctly use default values when using empty hash and client is configured" do + Unleash.configure do |config| + config.url = 'http://testurl/api' + config.app_name = 'my_ruby_app' + end + + params = Hash.new + context = Unleash::Context.new(params) + expect(context.app_name).to eq('my_ruby_app') + expect(context.environment).to eq('default') + end end