Class: Talis::User

Inherits:
Resource show all
Defined in:
lib/talis/user.rb

Overview

Represents a user known by Talis.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Resource

handle_response, new_req_id

Constructor Details

#initialize(guid:, first_name:, surname:, email:, token: nil) ⇒ User

Creates a new user object. For internal use only, use build.

Parameters:

  • guid (String)

    the globally unique identifier for the user.

  • first_name (String)

    the user’s first name.

  • surname (String)

    the user’s surname.

  • email (String)

    the user’s E-mail address.

  • token (Talis::Authentication::Token) (defaults to: nil)

    (nil) valid user token.



28
29
30
31
32
33
34
# File 'lib/talis/user.rb', line 28

def initialize(guid:, first_name:, surname:, email:, token: nil)
  @guid = guid
  @first_name = first_name
  @surname = surname
  @email = email
  @token = token
end

Instance Attribute Details

#emailString (readonly)

Returns the user’s E-mail address.

Returns:

  • (String)

    the user’s E-mail address.



15
16
17
# File 'lib/talis/user.rb', line 15

def email
  @email
end

#first_nameString (readonly)

Returns the user’s first name.

Returns:

  • (String)

    the user’s first name.



11
12
13
# File 'lib/talis/user.rb', line 11

def first_name
  @first_name
end

#guidString (readonly)

Returns the globally unique identifier for the user.

Returns:

  • (String)

    the globally unique identifier for the user.



9
10
11
# File 'lib/talis/user.rb', line 9

def guid
  @guid
end

#surnameString (readonly)

Returns the user’s surname.

Returns:

  • (String)

    the user’s surname.



13
14
15
# File 'lib/talis/user.rb', line 13

def surname
  @surname
end

#tokenTalis::Authentication::Token (readonly)

Returns to use for user authentication with other Talis primitive services.

Returns:



18
19
20
# File 'lib/talis/user.rb', line 18

def token
  @token
end

Class Method Details

.build(guid:, first_name:, surname:, email:, access_token: nil) ⇒ User

Builds a new user object. This is for creating user objects as a result of a successful login request.

Parameters:

  • guid (String)

    the globally unique identifier for the user.

  • first_name (String)

    the user’s first name.

  • surname (String)

    the user’s surname.

  • email (String)

    the user’s E-mail address.

  • access_token (String) (defaults to: nil)

    (nil) valid user JWT.

Returns:



88
89
90
91
92
93
94
95
96
97
# File 'lib/talis/user.rb', line 88

def build(guid:, first_name:, surname:, email:, access_token: nil)
  options = {
    guid: guid,
    first_name: first_name,
    surname: surname,
    email: email,
    token: Talis::Authentication::Token.new(jwt: access_token)
  }
  new(options)
end

.find(request_id: new_req_id, guid:) ⇒ User

Find a single user given the search criterion. In order to perform this search, the client must be configured with a valid OAuth client that is allowed to search for users:

Talis::Authentication.client_id = 'client_id'
Talis::Authentication.client_secret = 'client_secret'

Parameters:

  • request_id (String) (defaults to: new_req_id)

    (‘uuid’) unique ID for the remote request.

  • guid (String)

    the globally unique ID of the user to find.

Returns:



69
70
71
72
73
74
75
76
77
78
# File 'lib/talis/user.rb', line 69

def find(request_id: new_req_id, guid:)
  response = get("/users/#{guid}",
                 headers: {
                   'X-Request-Id' => request_id,
                   'Authorization' => "Bearer #{token}"
                 })
  new(extract_user_data(handle_response(response)))
rescue Talis::NotFoundError
  nil
end

Instance Method Details

#avatar_url(size: nil, colour: nil) ⇒ String

Returns a URL that will resolve to an avatar image belonging to the user when fetched. This URL is cached by browsers when called.

Parameters:

  • size (Integer) (defaults to: nil)

    (nil) size in pixels (defaults to 70x70).

  • colour (String) (defaults to: nil)

    (nil) background hex colour (defaults to 000000).

Returns:

  • (String)

    the URL of the user’s avatar.



47
48
49
50
51
52
53
54
55
56
# File 'lib/talis/user.rb', line 47

def avatar_url(size: nil, colour: nil)
  if size.present? && colour.present?
    params = "?#{URI.encode_www_form(size: size, colour: colour)}"
  elsif size.present?
    params = "?#{URI.encode_www_form(size: size)}"
  elsif colour.present?
    params = "?#{URI.encode_www_form(colour: colour)}"
  end
  "#{self.class.base_uri}/users/#{guid}/avatar#{params}"
end

#full_nameString

Return the combination of the user’s first and surname.

Returns:

  • (String)

    the user’s full name



38
39
40
# File 'lib/talis/user.rb', line 38

def full_name
  "#{@first_name} #{@surname}"
end