Class: Courier::Users::TokensClient

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(request_client:) ⇒ Users::TokensClient

Parameters:



17
18
19
20
# File 'lib/trycourier/users/tokens/client.rb', line 17

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

Instance Attribute Details

#request_clientObject (readonly)

Returns the value of attribute request_client.



13
14
15
# File 'lib/trycourier/users/tokens/client.rb', line 13

def request_client
  @request_client
end

Instance Method Details

#add(user_id:, token:, request:, request_options: nil) ⇒ Void

Adds a single token to a user and overwrites a matching existing token.

Parameters:

  • user_id (String)

    The user’s ID. This can be any uniquely identifiable string.

  • token (String)

    The full token string.

  • request (Hash)

    Request of type Users::Tokens::UserToken, as a Hash

    • :token (String)

    • :provider_key (Users::Tokens::ProviderKey)

    • :expiry_date (Hash)

    • :properties (Object)

    • :device (Hash)

      • :app_id (String)

      • :ad_id (String)

      • :device_id (String)

      • :platform (String)

      • :manufacturer (String)

      • :model (String)

    • :tracking (Hash)

      • :os_version (String)

      • :ip (String)

      • :lat (String)

      • :long (String)

  • request_options (RequestOptions) (defaults to: nil)

Returns:

  • (Void)


61
62
63
64
65
66
67
68
69
70
71
# File 'lib/trycourier/users/tokens/client.rb', line 61

def add(user_id:, token:, request:, request_options: nil)
  @request_client.conn.put("/users/#{user_id}/tokens/#{token}") 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

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

Adds multiple tokens to a user and overwrites matching existing tokens.

Parameters:

  • user_id (String)

    The user’s ID. This can be any uniquely identifiable string.

  • request_options (RequestOptions) (defaults to: nil)

Returns:

  • (Void)


27
28
29
30
31
32
33
34
35
36
# File 'lib/trycourier/users/tokens/client.rb', line 27

def add_multiple(user_id:, request_options: nil)
  @request_client.conn.put("/users/#{user_id}/tokens") 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(user_id:, token:, request_options: nil) ⇒ Users::Tokens::GetUserTokenResponse

Get single token available for a ‘:token`

Parameters:

  • user_id (String)

    The user’s ID. This can be any uniquely identifiable string.

  • token (String)

    The full token string.

  • request_options (RequestOptions) (defaults to: nil)

Returns:



99
100
101
102
103
104
105
106
107
108
109
# File 'lib/trycourier/users/tokens/client.rb', line 99

def get(user_id:, token:, request_options: nil)
  response = @request_client.conn.get("/users/#{user_id}/tokens/#{token}") 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
  Users::Tokens::GetUserTokenResponse.from_json(json_object: response.body)
end

#list(user_id:, request_options: nil) ⇒ Users::Tokens::GET_ALL_TOKENS_RESPONSE

Gets all tokens available for a :user_id

Parameters:

  • user_id (String)

    The user’s ID. This can be any uniquely identifiable string.

  • request_options (RequestOptions) (defaults to: nil)

Returns:



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/trycourier/users/tokens/client.rb', line 116

def list(user_id:, request_options: nil)
  response = @request_client.conn.get("/users/#{user_id}/tokens") 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
  return if response.body.nil?

  response.body.map do |v|
    v = v.to_json
    Users::Tokens::UserToken.from_json(json_object: v)
  end
end

#update(user_id:, token:, request:, request_options: nil) ⇒ Void

Apply a JSON Patch (RFC 6902) to the specified token.

Parameters:

  • user_id (String)

    The user’s ID. This can be any uniquely identifiable string.

  • token (String)

    The full token string.

  • request (Hash)

    Request of type Users::Tokens::PatchUserTokenOpts, as a Hash

    • :patch (Array<Users::Tokens::PatchOperation>)

  • request_options (RequestOptions) (defaults to: nil)

Returns:

  • (Void)


81
82
83
84
85
86
87
88
89
90
91
# File 'lib/trycourier/users/tokens/client.rb', line 81

def update(user_id:, token:, request:, request_options: nil)
  @request_client.conn.patch("/users/#{user_id}/tokens/#{token}") 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