Module: Knock::Users

Extended by:
Base, Client
Defined in:
lib/knock/users.rb

Overview

Provides convienience methods for working with users

Constant Summary collapse

DEFAULT_PREFERENCE_SET_ID =
"default"

Instance Attribute Summary

Attributes included from Base

#key

Class Method Summary collapse

Methods included from Client

client, delete_request, execute_request, get_request, handle_error_response, post_request, put_request, user_agent

Class Method Details

.bulk_delete(user_ids: []) ⇒ Hash

Bulk deletes users

Parameters:

  • user_ids (Array<String>) (defaults to: [])

    The users to delete

Returns:

  • (Hash)

    the BulkOperation



77
78
79
80
81
82
83
84
85
# File 'lib/knock/users.rb', line 77

def bulk_delete(user_ids: [])
  request = post_request(
    auth: true,
    path: "/v1/users/bulk/delete",
    body: {user_ids: user_ids}
  )

  execute_request(request: request)
end

.bulk_identify(users: []) ⇒ Hash

Bulk identifies users

Parameters:

  • users (Array<Hash>) (defaults to: [])

    The users to upsert

Returns:

  • (Hash)

    the BulkOperation



34
35
36
37
38
39
40
41
42
# File 'lib/knock/users.rb', line 34

def bulk_identify(users: [])
  request = post_request(
    auth: true,
    path: "/v1/users/bulk/identify",
    body: {users: users}
  )

  execute_request(request: request)
end

.bulk_set_preferences(user_ids: [], preferences: {}, preference_set: DEFAULT_PREFERENCE_SET_ID) ⇒ Hash

Bulk sets the preference object for the users.

Parameters:

  • user_ids (Array<String>) (defaults to: [])

    The ID of the user to set preferences for

  • preferences (Hash) (defaults to: {})

    The preferences hash to set

  • preference_set (String) (defaults to: DEFAULT_PREFERENCE_SET_ID)

    The preference set ID (defaults to `default`)

Returns:

  • (Hash)

    BulkOperation



196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
# File 'lib/knock/users.rb', line 196

def bulk_set_preferences(
  user_ids: [],
  preferences: {},
  preference_set: DEFAULT_PREFERENCE_SET_ID
)
  endpoint = "/v1/users/bulk/preferences"

  # Put the preference set id if it doesn't already exist
  unless preferences.has_key("id")
    preferences["id"] = preference_set
  end

  request = put_request(
    auth: true,
    path: endpoint,
    body: {
      user_ids: user_ids,
      preferences: preferences
    }
  )

  execute_request(request: request)
end

.delete(id:) ⇒ Hash

Deletes the user

Parameters:

  • id (String)

    the user ID

Returns:

  • (Hash)

    the user



63
64
65
66
67
68
69
70
# File 'lib/knock/users.rb', line 63

def delete(id:)
  request = delete_request(
    auth: true,
    path: "/v1/users/#{id}"
  )

  execute_request(request: request)
end

.get(id:) ⇒ Hash

Retrieves the given user

Parameters:

  • id (String)

    The user ID

Returns:

  • (Hash)

    The user



49
50
51
52
53
54
55
56
# File 'lib/knock/users.rb', line 49

def get(id:)
  request = get_request(
    auth: true,
    path: "/v1/users/#{id}"
  )

  execute_request(request: request)
end

.get_all_preferences(user_id:) ⇒ Hash

Returns all preference sets for the user

Parameters:

  • user_id (String)

    The ID of the user to retrieve preferences for

Returns:

  • (Hash)

    The preference sets



129
130
131
132
133
134
135
136
137
138
# File 'lib/knock/users.rb', line 129

def get_all_preferences(user_id:)
  endpoint = "/v1/users/#{user_id}/preferences"

  request = get_request(
    auth: true,
    path: endpoint
  )

  execute_request(request: request)
end

.get_channel_data(id:, channel_id:) ⇒ Hash

Get user's channel data for the given channel id

Parameters:

  • id (String)

    the user ID

  • channel_id (String)

    target channel ID

Returns:

  • (Hash)

    channel data



294
295
296
297
298
299
300
301
# File 'lib/knock/users.rb', line 294

def get_channel_data(id:, channel_id:)
  request = get_request(
    auth: true,
    path: "/v1/users/#{id}/channel_data/#{channel_id}"
  )

  execute_request(request: request)
end

.get_feed(id:, channel_id:, options: {}) ⇒ Hash

Gets a feed for a user

Parameters:

  • id (String)

    The user id

  • channel_id (String)

    The feed id to retrieve

  • options (Hash) (defaults to: {})

    Options to pass to the feed query

Returns:

  • (Hash)

    the feed response



94
95
96
97
98
99
100
101
102
# File 'lib/knock/users.rb', line 94

def get_feed(id:, channel_id:, options: {})
  request = get_request(
    auth: true,
    path: "/v1/users/#{id}/feeds/#{channel_id}",
    params: options
  )

  execute_request(request: request)
end

.get_messages(id:, options: {}) ⇒ Hash

Get user's messages

Parameters:

  • id (String)

    the user ID

  • options (Hash) (defaults to: {})

    Options to pass to the messages endpoint query

Returns:

  • (Hash)

    Paginated messages response



330
331
332
333
334
335
336
337
338
# File 'lib/knock/users.rb', line 330

def get_messages(id:, options: {})
  request = get_request(
    auth: true,
    path: "/v1/users/#{id}/messages",
    params: options
  )

  execute_request(request: request)
end

.get_preferences(user_id:, preference_set: DEFAULT_PREFERENCE_SET_ID) ⇒ Hash

Gets a single preference set, defaults to the 'default' set for the user given.

Parameters:

  • user_id (String)

    The ID of the user to retrieve preferences for

  • preference_set (String) (defaults to: DEFAULT_PREFERENCE_SET_ID)

    The preference set ID (defaults to `default`)

Returns:

  • (Hash)

    The preference set (if it exists)



147
148
149
150
151
152
153
154
155
156
# File 'lib/knock/users.rb', line 147

def get_preferences(user_id:, preference_set: DEFAULT_PREFERENCE_SET_ID)
  endpoint = "/v1/users/#{user_id}/preferences/#{preference_set}"

  request = get_request(
    auth: true,
    path: endpoint
  )

  execute_request(request: request)
end

.identify(id:, data: {}) ⇒ Hash

Identifies the user

Parameters:

  • id (String)

    The user ID

  • data (Hash) (defaults to: {})

    The traits to attach to the user

Returns:

  • (Hash)

    The user



19
20
21
22
23
24
25
26
27
# File 'lib/knock/users.rb', line 19

def identify(id:, data: {})
  request = put_request(
    auth: true,
    path: "/v1/users/#{id}",
    body: data
  )

  execute_request(request: request)
end

.merge(id:, from_user_id:) ⇒ Hash

Merges the user specified with `from_user_id` into the user specified with `user_id`.

Parameters:

  • id (String)

    The user id to merge into.

  • from_user_id (String)

    The user id to merge from

Returns:

  • (Hash)

    the merged User



110
111
112
113
114
115
116
117
118
# File 'lib/knock/users.rb', line 110

def merge(id:, from_user_id:)
  request = post_request(
    auth: true,
    path: "/v1/users/#{id}/merge",
    body: { from_user_id: from_user_id }
  )

  execute_request(request: request)
end

.set_category_preferences(user_id:, category:, setting:, preference_set: DEFAULT_PREFERENCE_SET_ID) ⇒ Hash

Sets preferences for the given category

Parameters:

  • user_id (String)

    The ID of the user to set preferences for

  • preference_set (String) (defaults to: DEFAULT_PREFERENCE_SET_ID)

    The preference set ID (defaults to `default`)

  • category (String)

    The category to set preferences for

  • setting (Bool | Hash)

    Either a boolean to indicate if the type is enabled or a hash containing channel types and settings

Returns:

  • (Hash)

    The preference set



271
272
273
274
275
276
277
278
279
280
281
282
# File 'lib/knock/users.rb', line 271

def set_category_preferences(user_id:, category:, setting:, preference_set: DEFAULT_PREFERENCE_SET_ID)
  params = setting.is_a?(Hash) ? setting : {subscribed: setting}
  endpoint = "/v1/users/#{user_id}/preferences/#{preference_set}/categories/#{category}"

  request = put_request(
    auth: true,
    path: endpoint,
    body: params
  )

  execute_request(request: request)
end

.set_channel_data(id:, channel_id:, channel_data:) ⇒ Hash

Upserts user's channel data for the given channel id

Parameters:

  • id (String)

    the user ID

  • channel_id (String)

    target channel ID

  • channel_data (Hash)

    channel data

Returns:

  • (Hash)

    channel data



310
311
312
313
314
315
316
317
318
# File 'lib/knock/users.rb', line 310

def set_channel_data(id:, channel_id:, channel_data:)
  request = put_request(
    auth: true,
    path: "/v1/users/#{id}/channel_data/#{channel_id}",
    body: {data: channel_data}
  )

  execute_request(request: request)
end

.set_channel_type_preferences(user_id:, channel_type:, setting:, preference_set: DEFAULT_PREFERENCE_SET_ID) ⇒ Hash

Sets preferences for the given channel type

Parameters:

  • user_id (String)

    The ID of the user to set preferences for

  • preference_set (String) (defaults to: DEFAULT_PREFERENCE_SET_ID)

    The preference set ID (defaults to `default`)

  • channel_type (String)

    The channel type to set

  • setting (Bool)

    Whether the channel type is enabled or not

Returns:

  • (Hash)

    The preference set



228
229
230
231
232
233
234
235
236
237
238
# File 'lib/knock/users.rb', line 228

def set_channel_type_preferences(user_id:, channel_type:, setting:, preference_set: DEFAULT_PREFERENCE_SET_ID)
  endpoint = "/v1/users/#{user_id}/preferences/#{preference_set}/channel_types/#{channel_type}"

  request = put_request(
    auth: true,
    path: endpoint,
    body: {subscribed: setting}
  )

  execute_request(request: request)
end

.set_preferences(user_id:, channel_types: nil, workflows: nil, categories: nil, preference_set: DEFAULT_PREFERENCE_SET_ID) ⇒ Hash

Sets multiple preferences at once for the preference set.

Parameters:

  • user_id (String)

    The ID of the user to set preferences for

  • preference_set (String) (defaults to: DEFAULT_PREFERENCE_SET_ID)

    The preference set ID (defaults to `default`)

  • channel_types (Hash) (defaults to: nil)

    The channel_types hash to set

  • workflows (Hash) (defaults to: nil)

    The workflows hash to set

  • categories (Hash) (defaults to: nil)

    The categories hash to set

Returns:

  • (Hash)

    The preference set



167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# File 'lib/knock/users.rb', line 167

def set_preferences(
  user_id:,
  channel_types: nil,
  workflows: nil,
  categories: nil,
  preference_set: DEFAULT_PREFERENCE_SET_ID
)
  endpoint = "/v1/users/#{user_id}/preferences/#{preference_set}"

  request = put_request(
    auth: true,
    path: endpoint,
    body: {
      channel_types: channel_types,
      workflows: workflows,
      categories: categories
    }
  )

  execute_request(request: request)
end

.set_workflow_preferences(user_id:, workflow:, setting:, preference_set: DEFAULT_PREFERENCE_SET_ID) ⇒ Hash

Sets preferences for the given workflow

Parameters:

  • user_id (String)

    The ID of the user to set preferences for

  • preference_set (String) (defaults to: DEFAULT_PREFERENCE_SET_ID)

    The preference set ID (defaults to `default`)

  • workflow (String)

    The workflow to set preferences for

  • setting (Bool | Hash)

    Either a boolean to indicate if the type is enabled or a hash containing channel types and settings

Returns:

  • (Hash)

    The preference set



249
250
251
252
253
254
255
256
257
258
259
260
# File 'lib/knock/users.rb', line 249

def set_workflow_preferences(user_id:, workflow:, setting:, preference_set: DEFAULT_PREFERENCE_SET_ID)
  params = setting.is_a?(Hash) ? setting : {subscribed: setting}
  endpoint = "/v1/users/#{user_id}/preferences/#{preference_set}/workflows/#{workflow}"

  request = put_request(
    auth: true,
    path: endpoint,
    body: params
  )

  execute_request(request: request)
end