Class: Stream::Client

Inherits:
Object
  • Object
show all
Includes:
Activities, Batch
Defined in:
lib/stream/client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Activities

#activity_partial_update, #batch_activity_partial_update, #get_activities

Methods included from Batch

#add_to_many, #follow_many, #unfollow_many

Constructor Details

#initialize(api_key = '', api_secret = '', app_id = nil, opts = {}) ⇒ Client

initializes a Stream API Client

Examples:

initialise the client to connect to EU-West location

Stream::Client.new('my_key', 'my_secret', 'my_app_id', :location => 'us-east')

Parameters:

  • api_key (string) (defaults to: '')

    your application api_key

  • api_secret (string) (defaults to: '')

    your application secret

  • app_id (string) (defaults to: nil)

    the id of your application (optional)

  • opts (hash) (defaults to: {})

    extra options


38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# 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

Instance Attribute Details

#api_keyObject (readonly)

Returns the value of attribute api_key


12
13
14
# File 'lib/stream/client.rb', line 12

def api_key
  @api_key
end

#api_secretObject (readonly)

Returns the value of attribute api_secret


13
14
15
# File 'lib/stream/client.rb', line 13

def api_secret
  @api_secret
end

#app_idObject (readonly)

Returns the value of attribute app_id


14
15
16
# File 'lib/stream/client.rb', line 14

def app_id
  @app_id
end

#client_optionsObject (readonly)

Returns the value of attribute client_options


15
16
17
# File 'lib/stream/client.rb', line 15

def client_options
  @client_options
end

Instance Method Details

#collectionsObject


110
111
112
# File 'lib/stream/client.rb', line 110

def collections
  CollectionsClient.new(api_key, api_secret, app_id, client_options)
end

#create_user_session_token(user_id, extra_data = {}) ⇒ string

Deprecated.

Use Client#create_user_token instead

Creates a user token

Parameters:

  • user_id (string)

    the user_if of this token (e.g. User42)

  • extra_data (hash) (defaults to: {})

    additional token data

Returns:

  • (string)

91
92
93
# File 'lib/stream/client.rb', line 91

def create_user_session_token(user_id, extra_data = {})
  create_user_token(user_id, extra_data)
end

#create_user_token(user_id, extra_data = {}) ⇒ string

Creates a user token

Parameters:

  • user_id (string)

    the user_if of this token (e.g. User42)

  • extra_data (hash) (defaults to: {})

    additional token data

Returns:

  • (string)

102
103
104
# File 'lib/stream/client.rb', line 102

def create_user_token(user_id, extra_data = {})
  return Stream::Signer.create_user_token(user_id, extra_data, api_secret)
end

#feed(feed_slug, user_id) ⇒ Stream::Feed

Creates a feed instance

Parameters:

  • feed_slug (string)

    the feed slug (eg. flat, aggregated…)

  • user_id (user_id)

    the user_id of this feed (eg. User42)

Returns:


78
79
80
# File 'lib/stream/client.rb', line 78

def feed(feed_slug, user_id)
  Stream::Feed.new(self, feed_slug, user_id)
end

#get_default_paramsObject


131
132
133
# File 'lib/stream/client.rb', line 131

def get_default_params
  {:api_key => @api_key}
end

#get_http_clientObject


135
136
137
# File 'lib/stream/client.rb', line 135

def get_http_client
  @http_client ||= StreamHTTPClient.new(url_generator)
end

#make_query_params(params) ⇒ Object


139
140
141
# File 'lib/stream/client.rb', line 139

def make_query_params(params)
  Hash[get_default_params.merge(params).sort_by {|k, v| k.to_s}]
end

#make_request(method, relative_url, signature, params = {}, data = {}, headers = {}) ⇒ Object


143
144
145
146
147
# File 'lib/stream/client.rb', line 143

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

#personalizationObject


106
107
108
# File 'lib/stream/client.rb', line 106

def personalization
  PersonalizationClient.new(api_key, api_secret, app_id, client_options)
end

#reactionsObject


114
115
116
# File 'lib/stream/client.rb', line 114

def reactions
  ReactionsClient.new(api_key, api_secret, app_id, client_options)
end

#update_activities(activities) ⇒ Object


126
127
128
129
# File 'lib/stream/client.rb', line 126

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) ⇒ Object


122
123
124
# File 'lib/stream/client.rb', line 122

def update_activity(activity)
  update_activities([activity])
end

#usersObject


118
119
120
# File 'lib/stream/client.rb', line 118

def users
  UsersClient.new(api_key, api_secret, app_id, client_options)
end