Class: Courier::ListsClient

Inherits:
Object
  • Object
show all
Defined in:
lib/trycourier/lists/client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(request_client:) ⇒ ListsClient

Parameters:



18
19
20
21
# File 'lib/trycourier/lists/client.rb', line 18

def initialize(request_client:)
  # @type [RequestClient]
  @request_client = request_client
end

Instance Attribute Details

#request_clientObject (readonly)

Returns the value of attribute request_client.



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

def request_client
  @request_client
end

Instance Method Details

#add_subscribers(list_id:, request:, request_options: nil) ⇒ Void

Subscribes additional users to the list, without modifying existing subscriptions. If the list does not exist, it will be automatically created.

Parameters:

  • list_id (String)

    A unique identifier representing the list you wish to retrieve.

  • request (Array<Hash>)

    Request of type Array<Lists::PutSubscriptionsRecipient>, as a Hash

    • :recipient_id (String)

    • :preferences (Hash)

      • :categories (Commons::NOTIFICATION_PREFERENCES)

      • :notifications (Commons::NOTIFICATION_PREFERENCES)

  • request_options (IdempotencyRequestOptions) (defaults to: nil)

Returns:

  • (Void)


169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
# File 'lib/trycourier/lists/client.rb', line 169

def add_subscribers(list_id:, request:, request_options: nil)
  @request_client.conn.post("/lists/#{list_id}/subscriptions") do |req|
    req.options.timeout = request_options.timeout_in_seconds unless request_options&.timeout_in_seconds.nil?
    unless request_options&.authorization_token.nil?
      req.headers["Authorization"] =
        request_options.authorization_token
    end
    req.headers["Idempotency-Key"] = request_options.idempotency_key unless request_options&.idempotency_key.nil?
    unless request_options&.idempotency_expiry.nil?
      req.headers["X-Idempotency-Expiration"] = request_options.idempotency_expiry
    end
    req.headers = { **req.headers, **(request_options&.additional_headers || {}) }.compact
    req.body = { **(request || {}), **(request_options&.additional_body_parameters || {}) }.compact
  end
end

#delete(list_id:, request_options: nil) ⇒ Void

Delete a list by list ID.

Parameters:

  • list_id (String)

    A unique identifier representing the list you wish to retrieve.

  • request_options (RequestOptions) (defaults to: nil)

Returns:

  • (Void)


91
92
93
94
95
96
97
98
99
100
# File 'lib/trycourier/lists/client.rb', line 91

def delete(list_id:, request_options: nil)
  @request_client.conn.delete("/lists/#{list_id}") do |req|
    req.options.timeout = request_options.timeout_in_seconds unless request_options&.timeout_in_seconds.nil?
    unless request_options&.authorization_token.nil?
      req.headers["Authorization"] =
        request_options.authorization_token
    end
    req.headers = { **req.headers, **(request_options&.additional_headers || {}) }.compact
  end
end

#get(list_id:, request_options: nil) ⇒ Lists::List

Returns a list based on the list ID provided.

Parameters:

  • list_id (String)

    A unique identifier representing the list you wish to retrieve.

  • request_options (RequestOptions) (defaults to: nil)

Returns:



51
52
53
54
55
56
57
58
59
60
61
# File 'lib/trycourier/lists/client.rb', line 51

def get(list_id:, request_options: nil)
  response = @request_client.conn.get("/lists/#{list_id}") do |req|
    req.options.timeout = request_options.timeout_in_seconds unless request_options&.timeout_in_seconds.nil?
    unless request_options&.authorization_token.nil?
      req.headers["Authorization"] =
        request_options.authorization_token
    end
    req.headers = { **req.headers, **(request_options&.additional_headers || {}) }.compact
  end
  Lists::List.from_json(json_object: response.body)
end

#get_subscribers(list_id:, cursor: nil, request_options: nil) ⇒ Lists::ListGetSubscriptionsResponse

Get the list’s subscriptions.

Parameters:

  • list_id (String)

    A unique identifier representing the list you wish to retrieve.

  • cursor (String) (defaults to: nil)

    A unique identifier that allows for fetching the next set of list subscriptions

  • request_options (RequestOptions) (defaults to: nil)

Returns:



124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/trycourier/lists/client.rb', line 124

def get_subscribers(list_id:, cursor: nil, request_options: nil)
  response = @request_client.conn.get("/lists/#{list_id}/subscriptions") do |req|
    req.options.timeout = request_options.timeout_in_seconds unless request_options&.timeout_in_seconds.nil?
    unless request_options&.authorization_token.nil?
      req.headers["Authorization"] =
        request_options.authorization_token
    end
    req.headers = { **req.headers, **(request_options&.additional_headers || {}) }.compact
    req.params = { **(request_options&.additional_query_parameters || {}), "cursor": cursor }.compact
  end
  Lists::ListGetSubscriptionsResponse.from_json(json_object: response.body)
end

#list(cursor: nil, pattern: nil, request_options: nil) ⇒ Lists::ListGetAllResponse

Returns all of the lists, with the ability to filter based on a pattern.

Parameters:

  • cursor (String) (defaults to: nil)

    A unique identifier that allows for fetching the next page of lists.

  • pattern (String) (defaults to: nil)

    “A pattern used to filter the list items returned. Pattern types supported: exact match on ‘list_id` or a pattern of one or more pattern parts. you may replace a part with either: `*` to match all parts in that position, or `**` to signify a wildcard `endsWith` pattern match.”

  • request_options (RequestOptions) (defaults to: nil)

Returns:



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/trycourier/lists/client.rb', line 29

def list(cursor: nil, pattern: nil, request_options: nil)
  response = @request_client.conn.get("/lists") do |req|
    req.options.timeout = request_options.timeout_in_seconds unless request_options&.timeout_in_seconds.nil?
    unless request_options&.authorization_token.nil?
      req.headers["Authorization"] =
        request_options.authorization_token
    end
    req.headers = { **req.headers, **(request_options&.additional_headers || {}) }.compact
    req.params = {
      **(request_options&.additional_query_parameters || {}),
      "cursor": cursor,
      "pattern": pattern
    }.compact
  end
  Lists::ListGetAllResponse.from_json(json_object: response.body)
end

#restore(list_id:, request_options: nil) ⇒ Void

Restore a previously deleted list.

Parameters:

  • list_id (String)

    A unique identifier representing the list you wish to retrieve.

  • request_options (RequestOptions) (defaults to: nil)

Returns:

  • (Void)


107
108
109
110
111
112
113
114
115
116
# File 'lib/trycourier/lists/client.rb', line 107

def restore(list_id:, request_options: nil)
  @request_client.conn.put("/lists/#{list_id}/restore") do |req|
    req.options.timeout = request_options.timeout_in_seconds unless request_options&.timeout_in_seconds.nil?
    unless request_options&.authorization_token.nil?
      req.headers["Authorization"] =
        request_options.authorization_token
    end
    req.headers = { **req.headers, **(request_options&.additional_headers || {}) }.compact
  end
end

#subscribe(list_id:, user_id:, preferences: nil, request_options: nil) ⇒ Void

Subscribe a user to an existing list (note: if the List does not exist, it will be automatically created).

Parameters:

  • list_id (String)

    A unique identifier representing the list you wish to retrieve.

  • user_id (String)

    A unique identifier representing the recipient associated with the list

  • preferences (Hash) (defaults to: nil)

    Request of type Commons::RecipientPreferences, as a Hash

    • :categories (Commons::NOTIFICATION_PREFERENCES)

    • :notifications (Commons::NOTIFICATION_PREFERENCES)

  • request_options (RequestOptions) (defaults to: nil)

Returns:

  • (Void)


194
195
196
197
198
199
200
201
202
203
204
# File 'lib/trycourier/lists/client.rb', line 194

def subscribe(list_id:, user_id:, preferences: nil, request_options: nil)
  @request_client.conn.put("/lists/#{list_id}/subscriptions/#{user_id}") do |req|
    req.options.timeout = request_options.timeout_in_seconds unless request_options&.timeout_in_seconds.nil?
    unless request_options&.authorization_token.nil?
      req.headers["Authorization"] =
        request_options.authorization_token
    end
    req.headers = { **req.headers, **(request_options&.additional_headers || {}) }.compact
    req.body = { **(request_options&.additional_body_parameters || {}), preferences: preferences }.compact
  end
end

#unsubscribe(list_id:, user_id:, request_options: nil) ⇒ Void

Delete a subscription to a list by list ID and user ID.

Parameters:

  • list_id (String)

    A unique identifier representing the list you wish to retrieve.

  • user_id (String)

    A unique identifier representing the recipient associated with the list

  • request_options (RequestOptions) (defaults to: nil)

Returns:

  • (Void)


212
213
214
215
216
217
218
219
220
221
# File 'lib/trycourier/lists/client.rb', line 212

def unsubscribe(list_id:, user_id:, request_options: nil)
  @request_client.conn.delete("/lists/#{list_id}/subscriptions/#{user_id}") do |req|
    req.options.timeout = request_options.timeout_in_seconds unless request_options&.timeout_in_seconds.nil?
    unless request_options&.authorization_token.nil?
      req.headers["Authorization"] =
        request_options.authorization_token
    end
    req.headers = { **req.headers, **(request_options&.additional_headers || {}) }.compact
  end
end

#update(list_id:, request:, request_options: nil) ⇒ Lists::List

Create or replace an existing list with the supplied values.

Parameters:

  • list_id (String)

    A unique identifier representing the list you wish to retrieve.

  • request (Hash)

    Request of type Lists::ListPutParams, as a Hash

    • :name (String)

    • :preferences (Hash)

      • :categories (Commons::NOTIFICATION_PREFERENCES)

      • :notifications (Commons::NOTIFICATION_PREFERENCES)

  • request_options (RequestOptions) (defaults to: nil)

Returns:



73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/trycourier/lists/client.rb', line 73

def update(list_id:, request:, request_options: nil)
  response = @request_client.conn.put("/lists/#{list_id}") do |req|
    req.options.timeout = request_options.timeout_in_seconds unless request_options&.timeout_in_seconds.nil?
    unless request_options&.authorization_token.nil?
      req.headers["Authorization"] =
        request_options.authorization_token
    end
    req.headers = { **req.headers, **(request_options&.additional_headers || {}) }.compact
    req.body = { **(request || {}), **(request_options&.additional_body_parameters || {}) }.compact
  end
  Lists::List.from_json(json_object: response.body)
end

#update_subscribers(list_id:, request:, request_options: nil) ⇒ Void

Subscribes the users to the list, overwriting existing subscriptions. If the list does not exist, it will be automatically created.

Parameters:

  • list_id (String)

    A unique identifier representing the list you wish to retrieve.

  • request (Array<Hash>)

    Request of type Array<Lists::PutSubscriptionsRecipient>, as a Hash

    • :recipient_id (String)

    • :preferences (Hash)

      • :categories (Commons::NOTIFICATION_PREFERENCES)

      • :notifications (Commons::NOTIFICATION_PREFERENCES)

  • request_options (RequestOptions) (defaults to: nil)

Returns:

  • (Void)


147
148
149
150
151
152
153
154
155
156
157
# File 'lib/trycourier/lists/client.rb', line 147

def update_subscribers(list_id:, request:, request_options: nil)
  @request_client.conn.put("/lists/#{list_id}/subscriptions") do |req|
    req.options.timeout = request_options.timeout_in_seconds unless request_options&.timeout_in_seconds.nil?
    unless request_options&.authorization_token.nil?
      req.headers["Authorization"] =
        request_options.authorization_token
    end
    req.headers = { **req.headers, **(request_options&.additional_headers || {}) }.compact
    req.body = { **(request || {}), **(request_options&.additional_body_parameters || {}) }.compact
  end
end