Class: Courier::ProfilesClient

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(request_client:) ⇒ ProfilesClient

Parameters:



19
20
21
22
# File 'lib/trycourier/profiles/client.rb', line 19

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

Instance Attribute Details

#request_clientObject (readonly)

Returns the value of attribute request_client.



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

def request_client
  @request_client
end

Instance Method Details

#create(user_id:, profile:, request_options: nil) ⇒ Profiles::MergeProfileResponse

Merge the supplied values with an existing profile or create a new profile if one doesn’t already exist.

Parameters:

  • user_id (String)

    A unique identifier representing the user associated with the requested profile.

  • profile (Hash{String => String})
  • request_options (IdempotencyRequestOptions) (defaults to: nil)

Returns:



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/trycourier/profiles/client.rb', line 47

def create(user_id:, profile:, request_options: nil)
  response = @request_client.conn.post("/profiles/#{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["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_options&.additional_body_parameters || {}), profile: profile }.compact
  end
  Profiles::MergeProfileResponse.from_json(json_object: response.body)
end

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

Deletes the specified user profile.

Parameters:

  • user_id (String)

    A unique identifier representing the user associated with the requested profile.

  • request_options (RequestOptions) (defaults to: nil)

Returns:

  • (Void)


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

def delete(user_id:, request_options: nil)
  @request_client.conn.delete("/profiles/#{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

#delete_list_subscription(user_id:, request_options: nil) ⇒ Profiles::DeleteListSubscriptionResponse

Removes all list subscriptions for given user.

Parameters:

  • user_id (String)

    A unique identifier representing the user associated with the requested profile.

  • request_options (RequestOptions) (defaults to: nil)

Returns:



150
151
152
153
154
155
156
157
158
159
160
# File 'lib/trycourier/profiles/client.rb', line 150

def delete_list_subscription(user_id:, request_options: nil)
  response = @request_client.conn.delete("/profiles/#{user_id}/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
  end
  Profiles::DeleteListSubscriptionResponse.from_json(json_object: response.body)
end

#get(user_id:, request_options: nil) ⇒ Profiles::ProfileGetResponse

Returns the specified user profile.

Parameters:

  • user_id (String)

    A unique identifier representing the user associated with the requested profile.

  • request_options (RequestOptions) (defaults to: nil)

Returns:



29
30
31
32
33
34
35
36
37
38
39
# File 'lib/trycourier/profiles/client.rb', line 29

def get(user_id:, request_options: nil)
  response = @request_client.conn.get("/profiles/#{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
  Profiles::ProfileGetResponse.from_json(json_object: response.body)
end

#get_list_subscriptions(user_id:, cursor: nil, request_options: nil) ⇒ Profiles::GetListSubscriptionsResponse

Returns the subscribed lists for a specified user.

Parameters:

  • user_id (String)

    A unique identifier representing the user associated with the requested profile.

  • cursor (String) (defaults to: nil)

    A unique identifier that allows for fetching the next set of message statuses.

  • request_options (RequestOptions) (defaults to: nil)

Returns:



108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/trycourier/profiles/client.rb', line 108

def get_list_subscriptions(user_id:, cursor: nil, request_options: nil)
  response = @request_client.conn.get("/profiles/#{user_id}/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 }.compact
  end
  Profiles::GetListSubscriptionsResponse.from_json(json_object: response.body)
end

#replace(user_id:, profile:, request_options: nil) ⇒ Profiles::ReplaceProfileResponse

When using ‘PUT`, be sure to include all the key-value pairs required by the recipient’s profile. Any key-value pairs that exist in the profile but fail to be included in the ‘PUT` request will be removed from the profile. Remember, a `PUT` update is a full replacement of the data. For partial updates, use the [Patch](www.courier.com/docs/reference/profiles/patch/) request.

Parameters:

  • user_id (String)

    A unique identifier representing the user associated with the requested profile.

  • profile (Hash{String => String})
  • request_options (RequestOptions) (defaults to: nil)

Returns:



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

def replace(user_id:, profile:, request_options: nil)
  response = @request_client.conn.put("/profiles/#{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 || {}), profile: profile }.compact
  end
  Profiles::ReplaceProfileResponse.from_json(json_object: response.body)
end

#subscribe_to_lists(user_id:, request:, request_options: nil) ⇒ Profiles::SubscribeToListsResponse

Subscribes the given user to one or more lists. If the list does not exist, it will be created.

Parameters:

  • user_id (String)

    A unique identifier representing the user associated with the requested profile.

  • request (Hash)

    Request of type Profiles::SubscribeToListsRequest, as a Hash

    • :lists (Array<Profiles::SubscribeToListsRequestListObject>)

  • request_options (IdempotencyRequestOptions) (defaults to: nil)

Returns:



128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/trycourier/profiles/client.rb', line 128

def subscribe_to_lists(user_id:, request:, request_options: nil)
  response = @request_client.conn.post("/profiles/#{user_id}/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["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
  Profiles::SubscribeToListsResponse.from_json(json_object: response.body)
end