Skip to content

Commit 0cdc38a

Browse files
authored
feat: env var handling enhancement (#76)
Note: if you used `STREAM_CHAT_URL` env var, you'll need to provide it manually in the `**options` as `base_url`. See the initializer of `Client` class for more information.
1 parent bfff20d commit 0cdc38a

File tree

2 files changed

+21
-9
lines changed

2 files changed

+21
-9
lines changed

lib/stream-chat/client.rb

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ module StreamChat
1919
HARD_DELETE = 'hard'
2020

2121
class Client
22-
BASE_URL = 'https://chat.stream-io-api.com'
22+
DEFAULT_BASE_URL = 'https://chat.stream-io-api.com'
23+
DEFAULT_TIMEOUT = 6.0
2324

2425
attr_reader :api_key
2526
attr_reader :api_secret
@@ -36,13 +37,15 @@ class Client
3637
# @example initialized the client with a timeout setting
3738
# StreamChat::Client.new('my_key', 'my_secret', 3.0)
3839
#
39-
def initialize(api_key = '', api_secret = '', timeout = 6.0, **options)
40-
@api_key = api_key || ENV['STREAM_KEY']
41-
@api_secret = api_secret || ENV['STREAM_SECRET']
42-
@timeout = timeout || ENV['STREAM_CHAT_TIMEOUT']
40+
def initialize(api_key, api_secret, timeout = nil, **options)
41+
raise ArgumentError, 'api_key and api_secret are required' if api_key.to_s.empty? || api_secret.to_s.empty?
42+
43+
@api_key = api_key
44+
@api_secret = api_secret
45+
@timeout = timeout || DEFAULT_TIMEOUT
4346
@options = options
4447
@auth_token = JWT.encode({ server: true }, @api_secret, 'HS256')
45-
@base_url = options[:base_url] || ENV['STREAM_CHAT_URL'] || BASE_URL
48+
@base_url = options[:base_url] || DEFAULT_BASE_URL
4649
@conn = Faraday.new(url: @base_url) do |faraday|
4750
faraday.options[:open_timeout] = @timeout
4851
faraday.options[:timeout] = @timeout
@@ -55,9 +58,14 @@ def initialize(api_key = '', api_secret = '', timeout = 6.0, **options)
5558
end
5659

5760
# initializes a Stream Chat API Client from STREAM_KEY and STREAM_SECRET
58-
# environmental variables.
59-
def self.from_env
60-
Client.new(ENV['STREAM_KEY'], ENV['STREAM_SECRET'])
61+
# environmental variables. STREAM_CHAT_TIMEOUT and STREAM_CHAT_URL
62+
# variables are optional.
63+
# @param [hash] options extra options
64+
def self.from_env(**options)
65+
Client.new(ENV['STREAM_KEY'],
66+
ENV['STREAM_SECRET'],
67+
ENV['STREAM_CHAT_TIMEOUT'],
68+
**{ base_url: ENV['STREAM_CHAT_URL'] }.merge(options))
6169
end
6270

6371
# Sets the underlying Faraday http client.

spec/client_spec.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,10 @@ def loop_times(times)
5454
expect(response).to include 'app'
5555
end
5656

57+
it 'raises ArgumentError if no api_key is provided' do
58+
expect { StreamChat::Client.new(nil, nil) }.to raise_error(ArgumentError)
59+
end
60+
5761
it 'properly handles stream response class' do
5862
response = @client.get_app_settings
5963
expect(response.rate_limit.limit).to be > 0

0 commit comments

Comments
 (0)