Skip to content

Commit

Permalink
Merge 479d51d into c88196a
Browse files Browse the repository at this point in the history
  • Loading branch information
rarruda committed Sep 5, 2019
2 parents c88196a + 479d51d commit b4ffc2e
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
4 changes: 3 additions & 1 deletion 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 || ''
Expand Down
19 changes: 19 additions & 0 deletions spec/unleash/context_spec.rb
Expand Up @@ -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

0 comments on commit b4ffc2e

Please sign in to comment.