@@ -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.
0 commit comments