Skip to content

Commit

Permalink
Merge pull request #69 from launchdarkly/jko/sdk-key
Browse files Browse the repository at this point in the history
api_key -> sdk_key
  • Loading branch information
jkodumal authored Aug 5, 2016
2 parents 57082aa + 42228eb commit 90b74f2
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 43 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ gem install ldclient-rb
require 'ldclient-rb'
```

2. Create a new LDClient with your API key:
2. Create a new LDClient with your SDK key:

```ruby
client = LaunchDarkly::LDClient.new("your_api_key")
client = LaunchDarkly::LDClient.new("your_sdk_key")
```

### Ruby on Rails
Expand All @@ -36,7 +36,7 @@ client = LaunchDarkly::LDClient.new("your_api_key")
1. Initialize the launchdarkly client in `config/initializers/launchdarkly.rb`:

```ruby
Rails.configuration.ld_client = LaunchDarkly::LDClient.new("your_api_key")
Rails.configuration.ld_client = LaunchDarkly::LDClient.new("your_sdk_key")
```

2. You may want to include a function in your ApplicationController
Expand Down
2 changes: 1 addition & 1 deletion lib/ldclient-rb/evaluation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ def eval_internal(flag, user, store, events)
begin
prereq_res = eval_internal(prereq_flag, user, store, events)
variation = get_variation(prereq_flag, prerequisite[:variation])
events.push(kind: "feature", key: prereq_flag[:key], value: prereq_res)
events.push(kind: "feature", key: prereq_flag[:key], value: prereq_res, version: prereq_flag[:version], prereqOf: flag[:key])
if prereq_res.nil? || prereq_res!= variation
failed_prereq = true
end
Expand Down
6 changes: 3 additions & 3 deletions lib/ldclient-rb/events.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
module LaunchDarkly

class EventProcessor
def initialize(api_key, config)
def initialize(sdk_key, config)
@queue = Queue.new
@api_key = api_key
@sdk_key = sdk_key
@config = config
@client = Faraday.new
@worker = create_worker
Expand All @@ -27,7 +27,7 @@ def create_worker

def post_flushed_events(events)
res = @client.post (@config.events_uri + "/bulk") do |req|
req.headers["Authorization"] = "api_key " + @api_key
req.headers["Authorization"] = @sdk_key
req.headers["User-Agent"] = "RubyClient/" + LaunchDarkly::VERSION
req.headers["Content-Type"] = "application/json"
req.body = events.to_json
Expand Down
59 changes: 32 additions & 27 deletions lib/ldclient-rb/ldclient.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

module LaunchDarkly
#
# A client for the LaunchDarkly API. Client instances are thread-safe. Users
# A client for LaunchDarkly. Client instances are thread-safe. Users
# should create a single client instance for the lifetime of the application.
#
#
Expand All @@ -19,26 +19,26 @@ class LDClient
# but for most use cases, the default configuration is appropriate.
#
#
# @param api_key [String] the API key for your LaunchDarkly account
# @param sdk_key [String] the SDK key for your LaunchDarkly account
# @param config [Config] an optional client configuration object
#
# @return [LDClient] The LaunchDarkly client instance
def initialize(api_key, config = Config.default, wait_for_sec = 5)
@api_key = api_key
def initialize(sdk_key, config = Config.default, wait_for_sec = 5)
@sdk_key = sdk_key
@config = config
@store = config.feature_store
requestor = Requestor.new(api_key, config)
requestor = Requestor.new(sdk_key, config)

if !@config.offline?
if @config.stream?
@update_processor = StreamProcessor.new(api_key, config, requestor)
@update_processor = StreamProcessor.new(sdk_key, config, requestor)
else
@update_processor = PollingProcessor.new(config, requestor)
end
@update_processor.start
end

@event_processor = EventProcessor.new(api_key, config)
@event_processor = EventProcessor.new(sdk_key, config)

if !@config.offline? && wait_for_sec > 0
begin
Expand All @@ -61,8 +61,7 @@ def toggle?(key, user, default = False)
end

def secure_mode_hash(user)
puts @api_key
OpenSSL::HMAC.hexdigest('sha256', @api_key, user[:key].to_s)
OpenSSL::HMAC.hexdigest('sha256', @sdk_key, user[:key].to_s)
end

#
Expand Down Expand Up @@ -115,26 +114,32 @@ def variation(key, user, default)
sanitize_user(user)
feature = @store.get(key)

begin
res = evaluate(feature, user, @store)
if !res[:events].nil?
res[:events].each do |event|
@event_processor.add_event(event)
end
end
if !res[:value].nil?
@event_processor.add_event(kind: "feature", key: key, user: user, value: res[:value], default: default)
return res[:value]
else
@config.logger.debug("[LDClient] Result value is null in toggle")
@event_processor.add_event(kind: "feature", key: key, user: user, value: default, default: default)
return default
if feature.nil?
@config.logger.error("[LDClient] Unknown feature flag #{key}. Returning default value")
@event_processor.add_event(kind: "feature", key: key, value: default, default: default)
return default
end

begin
res = evaluate(feature, user, @store)
if !res[:events].nil?
res[:events].each do |event|
@event_processor.add_event(event)
end
rescue => exn
@config.logger.warn("[LDClient] Error evaluating feature flag: #{exn.inspect}. \nTrace: #{exn.backtrace}")
@event_processor.add_event(kind: "feature", key: key, user: user, value: default, default: default)
return default
end
if !res[:value].nil?
@event_processor.add_event(kind: "feature", key: key, user: user, value: res[:value], default: default, version: feature[:version])
return res[:value]
else
@config.logger.debug("[LDClient] Result value is null in toggle")
@event_processor.add_event(kind: "feature", key: key, user: user, value: default, default: default, version: feature[:version])
return default
end
rescue => exn
@config.logger.warn("[LDClient] Error evaluating feature flag: #{exn.inspect}. \nTrace: #{exn.backtrace}")
@event_processor.add_event(kind: "feature", key: key, user: user, value: default, default: default, version: feature[:version])
return default
end
end

#
Expand Down
8 changes: 4 additions & 4 deletions lib/ldclient-rb/requestor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
module LaunchDarkly

class Requestor
def initialize(api_key, config)
@api_key = api_key
def initialize(sdk_key, config)
@sdk_key = sdk_key
@config = config
@client = Faraday.new do |builder|
builder.use :http_cache, store: @config.cache_store
Expand All @@ -25,14 +25,14 @@ def request_flag(key)

def make_request(path)
res = @client.get (@config.base_uri + path) do |req|
req.headers["Authorization"] = "api_key " + @api_key
req.headers["Authorization"] = @sdk_key
req.headers["User-Agent"] = "RubyClient/" + LaunchDarkly::VERSION
req.options.timeout = @config.read_timeout
req.options.open_timeout = @config.connect_timeout
end

if res.status == 401
@config.logger.error("[LDClient] Invalid API key")
@config.logger.error("[LDClient] Invalid SDK key")
return nil
end

Expand Down
6 changes: 3 additions & 3 deletions lib/ldclient-rb/stream.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ module LaunchDarkly
INDIRECT_PATCH = :'indirect/patch'

class StreamProcessor
def initialize(api_key, config, requestor)
@api_key = api_key
def initialize(sdk_key, config, requestor)
@sdk_key = sdk_key
@config = config
@store = config.feature_store
@requestor = requestor
Expand All @@ -30,7 +30,7 @@ def start

headers =
{
'Authorization' => 'api_key ' + @api_key,
'Authorization' => @sdk_key,
'User-Agent' => 'RubyClient/' + LaunchDarkly::VERSION
}
opts = {:headers => headers, :with_credentials => true}
Expand Down
4 changes: 2 additions & 2 deletions spec/stream_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@
describe LaunchDarkly::StreamProcessor do
subject { LaunchDarkly::StreamProcessor }
let(:config) { LaunchDarkly::Config.new }
let(:requestor) { LaunchDarkly::Requestor.new("api_key", config)}
let(:processor) { subject.new("api_key", config, requestor) }
let(:requestor) { LaunchDarkly::Requestor.new("sdk_key", config)}
let(:processor) { subject.new("sdk_key", config, requestor) }

describe '#process_message' do
let(:put_message) { OpenStruct.new({data: '{"key": {"value": "asdf"}}'}) }
Expand Down

0 comments on commit 90b74f2

Please sign in to comment.