class Stream::Client

Attributes

api_key[R]
api_secret[R]
app_id[R]
client_options[R]

Public Class Methods

new(api_key = '', api_secret = '', app_id = nil, opts = {}) click to toggle source

initializes a Stream API Client

@param [string] api_key your application api_key @param [string] api_secret your application secret @param [string] app_id the id of your application (optional) @param [hash] opts extra options

@example initialise the client to connect to EU-West location

Stream::Client.new('my_key', 'my_secret', 'my_app_id', :location => 'us-east')
# File lib/stream/client.rb, line 38
def initialize(api_key = '', api_secret = '', app_id = nil, opts = {})
  if api_key.nil? || api_key.empty?
    env_url = ENV['STREAM_URL']
    if env_url =~ Stream::STREAM_URL_COM_RE
      re = Stream::STREAM_URL_COM_RE
    elsif env_url =~ Stream::STREAM_URL_IO_RE
      re = Stream::STREAM_URL_IO_RE
    end
    raise ArgumentError, 'empty api_key parameter and missing or invalid STREAM_URL env variable' unless re

    matches = re.match(ENV['STREAM_URL'])
    api_key = matches['key']
    api_secret = matches['secret']
    app_id = matches['app_id']
    opts[:location] = matches['location']
    opts[:api_hostname] = matches['api_hostname']
  end

  @api_key = api_key
  @api_secret = api_secret
  @app_id = app_id
  @signer = Stream::Signer.new(api_secret)

  @client_options = {
    api_key: @api_key,
    api_version: opts[:api_version] || 'v1.0',
    location: opts[:location],
    default_timeout: opts[:default_timeout] || 3,
    api_hostname: opts[:api_hostname] || 'stream-io-api.com'
  }
end

Public Instance Methods

collections() click to toggle source
# File lib/stream/client.rb, line 109
def collections
  CollectionsClient.new(api_key, api_secret, app_id, client_options)
end
create_user_session_token(user_id, extra_data = {}) click to toggle source

Creates a user token

@deprecated Use Client#create_user_token instead

@param [string] user_id the user_if of this token (e.g. User42) @param [hash] extra_data additional token data

@return [string]

# File lib/stream/client.rb, line 90
def create_user_session_token(user_id, extra_data = {})
  create_user_token(user_id, extra_data)
end
create_user_token(user_id, extra_data = {}) click to toggle source

Creates a user token

@param [string] user_id the user_if of this token (e.g. User42) @param [hash] extra_data additional token data

@return [string]

# File lib/stream/client.rb, line 101
def create_user_token(user_id, extra_data = {})
  Stream::Signer.create_user_token(user_id, extra_data, api_secret)
end
feed(feed_slug, user_id) click to toggle source

Creates a feed instance

@param [string] feed_slug the feed slug (eg. flat, aggregated…) @param [user_id] user_id the user_id of this feed (eg. User42)

@return [Stream::Feed]

# File lib/stream/client.rb, line 77
def feed(feed_slug, user_id)
  Stream::Feed.new(self, feed_slug, user_id)
end
get_default_params() click to toggle source
# File lib/stream/client.rb, line 135
def get_default_params
  { api_key: @api_key }
end
get_http_client() click to toggle source
# File lib/stream/client.rb, line 139
def get_http_client
  @get_http_client ||= StreamHTTPClient.new(url_generator)
end
make_query_params(params) click to toggle source
# File lib/stream/client.rb, line 143
def make_query_params(params)
  get_default_params.merge(params).sort_by { |k, _v| k.to_s }.to_h
end
make_request(method, relative_url, signature, params = {}, data = {}, headers = {}) click to toggle source
# File lib/stream/client.rb, line 147
def make_request(method, relative_url, signature, params = {}, data = {}, headers = {})
  headers['Authorization'] = signature
  headers['stream-auth-type'] = 'jwt'
  get_http_client.make_http_request(method, relative_url, make_query_params(params), data, headers)
end
og(uri) click to toggle source
# File lib/stream/client.rb, line 130
def og(uri)
  auth_token = Stream::Signer.create_jwt_token('*', '*', @api_secret, '*')
  make_request(:get, '/og', auth_token, { url: uri })
end
personalization() click to toggle source
# File lib/stream/client.rb, line 105
def personalization
  PersonalizationClient.new(api_key, api_secret, app_id, client_options)
end
reactions() click to toggle source
# File lib/stream/client.rb, line 113
def reactions
  ReactionsClient.new(api_key, api_secret, app_id, client_options)
end
update_activities(activities) click to toggle source
# File lib/stream/client.rb, line 125
def update_activities(activities)
  auth_token = Stream::Signer.create_jwt_token('activities', '*', @api_secret, '*')
  make_request(:post, '/activities/', auth_token, {}, 'activities' => activities)
end
update_activity(activity) click to toggle source
# File lib/stream/client.rb, line 121
def update_activity(activity)
  update_activities([activity])
end
users() click to toggle source
# File lib/stream/client.rb, line 117
def users
  UsersClient.new(api_key, api_secret, app_id, client_options)
end

Private Instance Methods

url_generator() click to toggle source
# File lib/stream/client.rb, line 155
def url_generator
  APIURLGenerator.new(@client_options)
end