Class: StreamChat::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/stream-chat/client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_key = '', api_secret = '', timeout = 6.0, **options) ⇒ Client

initializes a Stream Chat API Client

Examples:

initialized the client with a timeout setting

StreamChat::Client.new('my_key', 'my_secret', 3.0)

Parameters:

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

    your application api_key

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

    your application secret

  • (string)
  • options (hash)

    extra options



26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/stream-chat/client.rb', line 26

def initialize(api_key = '', api_secret = '', timeout = 6.0, **options)
  @api_key = api_key
  @api_secret = api_secret
  @timeout = timeout
  @options = options
  @base_url = 'https://chat-us-east-1.stream-io-api.com'
  @auth_token = JWT.encode({server: true}, @api_secret, 'HS256')
  @conn = Faraday.new(url: @base_url) do |faraday|
    faraday.options[:open_timeout] = @timeout
    faraday.options[:timeout] = @timeout
    faraday.adapter Faraday.default_adapter
  end
end

Instance Attribute Details

#api_keyObject (readonly)

Returns the value of attribute api_key.



11
12
13
# File 'lib/stream-chat/client.rb', line 11

def api_key
  @api_key
end

#api_secretObject (readonly)

Returns the value of attribute api_secret.



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

def api_secret
  @api_secret
end

#connObject (readonly)

Returns the value of attribute conn.



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

def conn
  @conn
end

#optionsObject (readonly)

Returns the value of attribute options.



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

def options
  @options
end

Instance Method Details

#add_device(device_id, push_provider, user_id) ⇒ Object



180
181
182
183
184
185
186
# File 'lib/stream-chat/client.rb', line 180

def add_device(device_id, push_provider, user_id)
  post("devices", data: {
    "id": device_id,
    "push_provider": push_provider,
    "user_id": user_id
  })
end

#ban_user(target_id, **options) ⇒ Object



80
81
82
83
# File 'lib/stream-chat/client.rb', line 80

def ban_user(target_id, **options)
  payload = {'target_user_id': target_id}.merge(options)
  post("moderation/ban", data: payload)
end

#channel(channel_type, channel_id: nil, data: nil) ⇒ StreamChat::Channel

Creates a channel instance

Parameters:

  • channel_type (string)

    the channel type

  • channel_id (string) (defaults to: nil)

    the channel identifier

  • data (hash) (defaults to: nil)

    additional channel data

Returns:



176
177
178
# File 'lib/stream-chat/client.rb', line 176

def channel(channel_type, channel_id: nil, data: nil)
  StreamChat::Channel.new(self, channel_type, channel_id, data)
end

#create_channel_type(data) ⇒ Object



145
146
147
148
149
150
# File 'lib/stream-chat/client.rb', line 145

def create_channel_type(data)
  if !data.key? "commands" || data["commands"].nil? || data["commands"].empty?
    data["commands"] = ["all"]
  end
  post("channeltypes", data: data)
end

#create_token(user_id, exp = nil) ⇒ Object



40
41
42
43
44
45
46
# File 'lib/stream-chat/client.rb', line 40

def create_token(user_id, exp = nil)
  payload = {user_id: user_id}
  if exp != nil
    payload['exp'] = exp
  end
  JWT.encode(payload, @api_secret, 'HS256')
end

#deactivate_user(user_id, **options) ⇒ Object



72
73
74
# File 'lib/stream-chat/client.rb', line 72

def deactivate_user(user_id, **options)
  post("users/#{user_id}/deactivate", **options)
end

#delete(relative_url, params: nil) ⇒ Object



213
214
215
# File 'lib/stream-chat/client.rb', line 213

def delete(relative_url, params: nil)
  make_http_request(:delete, relative_url, params: params)
end

#delete_channel_type(channel_type) ⇒ Object



164
165
166
# File 'lib/stream-chat/client.rb', line 164

def delete_channel_type(channel_type)
  delete("channeltypes/#{channel_type}")
end

#delete_device(device_id, user_id) ⇒ Object



188
189
190
# File 'lib/stream-chat/client.rb', line 188

def delete_device(device_id, user_id)
  delete("devices", params: {"id": device_id, "user_id": user_id})
end

#delete_message(message_id) ⇒ Object



112
113
114
# File 'lib/stream-chat/client.rb', line 112

def delete_message(message_id)
  delete("messages/#{message_id}")
end

#delete_user(user_id, **options) ⇒ Object



68
69
70
# File 'lib/stream-chat/client.rb', line 68

def delete_user(user_id, **options)
  delete("users/#{user_id}", **options)
end

#export_user(user_id, **options) ⇒ Object



76
77
78
# File 'lib/stream-chat/client.rb', line 76

def export_user(user_id, **options)
  get("users/#{user_id}/export", params: options)
end

#get(relative_url, params: nil) ⇒ Object



209
210
211
# File 'lib/stream-chat/client.rb', line 209

def get(relative_url, params: nil)
  make_http_request(:get, relative_url, params: params)
end

#get_app_settingsObject



52
53
54
# File 'lib/stream-chat/client.rb', line 52

def get_app_settings
  get('app')
end

#get_channel_type(channel_type) ⇒ Object



152
153
154
# File 'lib/stream-chat/client.rb', line 152

def get_channel_type(channel_type)
  get("channeltypes/#{channel_type}")
end

#get_devices(user_id) ⇒ Object



192
193
194
# File 'lib/stream-chat/client.rb', line 192

def get_devices(user_id)
  get("devices", params: {"user_id": user_id})
end

#list_channel_typesObject



156
157
158
# File 'lib/stream-chat/client.rb', line 156

def list_channel_types
  get("channeltypes")
end

#mark_all_read(user_id) ⇒ Object



100
101
102
103
# File 'lib/stream-chat/client.rb', line 100

def mark_all_read(user_id)
  payload = {'user': {'id': user_id}}
  post('channels/read', data: payload)
end

#mute_user(target_id, user_id) ⇒ Object



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

def mute_user(target_id, user_id)
  payload = {'target_id': target_id, 'user_id': user_id}
  post('moderation/mute', data: payload)
end

#patch(relative_url, params: nil, data: nil) ⇒ Object



217
218
219
# File 'lib/stream-chat/client.rb', line 217

def patch(relative_url, params: nil, data: nil)
  make_http_request(:patch, relative_url, params: params, data: data)
end

#post(relative_url, params: nil, data: nil) ⇒ Object



205
206
207
# File 'lib/stream-chat/client.rb', line 205

def post(relative_url, params: nil, data: nil)
  make_http_request(:post, relative_url, params: params, data: data)
end

#put(relative_url, params: nil, data: nil) ⇒ Object



201
202
203
# File 'lib/stream-chat/client.rb', line 201

def put(relative_url, params: nil, data: nil)
  make_http_request(:put, relative_url, params: params, data: data)
end

#query_channels(filter_conditions, sort: nil, **options) ⇒ Object



130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/stream-chat/client.rb', line 130

def query_channels(filter_conditions, sort: nil, **options)
  params = {"state": true, "watch": false, "presence": false}
  sort_fields = []
  if sort != nil
    sort.each do |k, v|
      sort_fields << {"field": k, "direction": v}
    end
  end
  params = params.merge(options).merge({
    "filter_conditions": filter_conditions,
    "sort": sort_fields
  })
  get("channels", params: {"payload": params.to_json})
end

#query_users(filter_conditions, sort: nil, **options) ⇒ Object



116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/stream-chat/client.rb', line 116

def query_users(filter_conditions, sort: nil, **options)
  sort_fields = []
  if sort != nil
    sort.each do |k ,v|
      sort_fields << {"field": k, "direction":  v}
    end
  end
  params = options.merge({
    "filter_conditions": filter_conditions,
    "sort": sort_fields
  })
  get("users", params: {"payload": params.to_json})
end

#unban_user(target_id, **options) ⇒ Object



85
86
87
88
# File 'lib/stream-chat/client.rb', line 85

def unban_user(target_id, **options)
  params = {'target_user_id': target_id}.merge(options)
  delete("moderation/ban", params: params)
end

#unmute_user(target_id, user_id) ⇒ Object



95
96
97
98
# File 'lib/stream-chat/client.rb', line 95

def unmute_user(target_id, user_id)
  payload = {'target_id': target_id, 'user_id': user_id}
  post('moderation/unmute', data: payload)
end

#update_app_settings(**settings) ⇒ Object



48
49
50
# File 'lib/stream-chat/client.rb', line 48

def update_app_settings(**settings)
  patch('app', **settings)
end

#update_channel_type(channel_type, **options) ⇒ Object



160
161
162
# File 'lib/stream-chat/client.rb', line 160

def update_channel_type(channel_type, **options)
  put("channeltypes/#{channel_type}", **options)
end

#update_message(message) ⇒ Object



105
106
107
108
109
110
# File 'lib/stream-chat/client.rb', line 105

def update_message(message)
  if !message.key? 'id'
    raise ArgumentError "message must have an id"
  end
  post("messages/#{message['id']}", data: {'message': message})
end

#update_user(user) ⇒ Object



64
65
66
# File 'lib/stream-chat/client.rb', line 64

def update_user(user)
    update_users([user])
end

#update_users(users) ⇒ Object



56
57
58
59
60
61
62
# File 'lib/stream-chat/client.rb', line 56

def update_users(users)
  payload = {}
  users.each do |user|
    payload[user[:id]] = user
  end
  post('users', data: {'users': payload})
end

#verify_webhook(request_body, x_signature) ⇒ Object



196
197
198
199
# File 'lib/stream-chat/client.rb', line 196

def verify_webhook(request_body, x_signature)
  signature = OpenSSL::HMAC.hexdigest("SHA256", @api_secret, request_body)
  signature == x_signature
end