lib/stream/client.rb in stream-ruby-2.0.6 vs lib/stream/client.rb in stream-ruby-2.1.0
- old
+ new
@@ -6,17 +6,17 @@
module Stream
STREAM_URL_RE = /https\:\/\/(?<key>\w+)\:(?<secret>\w+).*app_id=(?<app_id>\d+)/i
class Client
- @@http_client = nil
attr_reader :api_key
attr_reader :api_secret
attr_reader :app_id
attr_reader :api_version
+ attr_reader :location
- def initialize(api_key='', api_secret='', app_id=nil)
+ def initialize(api_key='', api_secret='', app_id=nil, opts={})
if ENV['STREAM_URL'] =~ Stream::STREAM_URL_RE and (api_key.nil? || api_key.empty?)
matches = Stream::STREAM_URL_RE.match(ENV['STREAM_URL'])
api_key = matches['key']
api_secret = matches['secret']
app_id = matches['app_id']
@@ -27,10 +27,12 @@
end
@api_key = api_key
@api_secret = api_secret
@app_id = app_id
+ @location = opts[:location]
+ @api_version = opts.fetch(:api_version, 'v1.0')
@signer = Stream::Signer.new(api_secret)
end
def feed(feed_slug, user_id)
token = @signer.sign(feed_slug, user_id)
@@ -40,11 +42,11 @@
def get_default_params
{:api_key => @api_key}
end
def get_http_client
- @@http_client ||= StreamHTTPClient.new
+ StreamHTTPClient.new(@api_version, @location)
end
def make_request(method, relative_url, signature, params=nil, data=nil)
auth_headers = {'Authorization' => signature}
params = params.nil? ? {} : params
@@ -57,11 +59,19 @@
end
class StreamHTTPClient
include HTTParty
- base_uri 'https://api.getstream.io/api/v1.0'
default_timeout 3
+
+ def initialize(api_version='v1.0', location=nil)
+ if location.nil?
+ location_name = "api"
+ else
+ location_name = "#{location}-api"
+ end
+ self.class.base_uri "https://#{location_name}.getstream.io/api/#{api_version}"
+ end
def make_http_request(method, relative_url, params=nil, data=nil, headers=nil)
headers['Content-Type'] = 'application/json'
headers['User-Agent'] = "stream-ruby-#{Stream::VERSION}"
response = self.class.send(method, relative_url, :headers => headers, :query => params, :body => data.to_json )