Class: Bloomy::User

Inherits:
Object
  • Object
show all
Includes:
Bloomy::Utilities::UserIdUtility
Defined in:
lib/bloomy/operations/users.rb

Overview

Class to handle all the operations related to users

Instance Method Summary collapse

Methods included from Bloomy::Utilities::UserIdUtility

#user_id

Constructor Details

#initialize(conn) ⇒ User

Initializes a new User instance

Parameters:

  • conn (Object)

    the connection object to interact with the API



13
14
15
# File 'lib/bloomy/operations/users.rb', line 13

def initialize(conn)
  @conn = conn
end

Instance Method Details

#all(include_placeholders: false) ⇒ Array<Hash>

Retrieves all users in the system

Parameters:

  • include_placeholders (Boolean) (defaults to: false)

    whether to include placeholder users (default: false)

Returns:

  • (Array<Hash>)

    an array of hashes containing user details



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/bloomy/operations/users.rb', line 88

def all(include_placeholders: false)
  users = @conn.get("search/all", term: "%").body
  users
    .select { |user| user["ResultType"] == "User" }
    .reject { |user| !include_placeholders && user["ImageUrl"] == "/i/userplaceholder" }
    .map do |user|
      {
        id: user["Id"],
        name: user["Name"],
        email: user["Email"],
        position: user["Description"],
        image_url: user["ImageUrl"]
      }
    end
end

#details(user_id = self.user_id, direct_reports: false, positions: false, all: false) ⇒ Hash

Retrieves details of a specific user

Parameters:

  • user_id (Integer) (defaults to: self.user_id)

    the ID of the user (default: the current user ID)

  • direct_reports (Boolean) (defaults to: false)

    whether to include direct reports (default: false)

  • positions (Boolean) (defaults to: false)

    whether to include positions (default: false)

  • all (Boolean) (defaults to: false)

    whether to include both direct reports and positions (default: false)

Returns:

  • (Hash)

    a hash containing user details



24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/bloomy/operations/users.rb', line 24

def details(user_id = self.user_id, direct_reports: false, positions: false, all: false)
  response = @conn.get("users/#{user_id}").body
  user_details = {
    id: response["Id"],
    name: response["Name"],
    image_url: response["ImageUrl"]
  }

  user_details[:direct_reports] = direct_reports(user_id) if direct_reports || all
  user_details[:positions] = positions(user_id) if positions || all
  user_details
end

#direct_reports(user_id = self.user_id) ⇒ Array<Hash>

Retrieves direct reports of a specific user

Parameters:

  • user_id (Integer) (defaults to: self.user_id)

    the ID of the user (default: the current user ID)

Returns:

  • (Array<Hash>)

    an array of hashes containing direct report details



41
42
43
44
45
46
47
48
49
50
# File 'lib/bloomy/operations/users.rb', line 41

def direct_reports(user_id = self.user_id)
  direct_reports_response = @conn.get("users/#{user_id}/directreports").body
  direct_reports_response.map do |report|
    {
      name: report["Name"],
      id: report["Id"],
      image_url: report["ImageUrl"]
    }
  end
end

#positions(user_id = self.user_id) ⇒ Array<Hash>

Retrieves positions of a specific user

Parameters:

  • user_id (Integer) (defaults to: self.user_id)

    the ID of the user (default: the current user ID)

Returns:

  • (Array<Hash>)

    an array of hashes containing position details



56
57
58
59
60
61
62
63
64
# File 'lib/bloomy/operations/users.rb', line 56

def positions(user_id = self.user_id)
  position_response = @conn.get("users/#{user_id}/seats").body
  position_response.map do |position|
    {
      name: position["Group"]["Position"]["Name"],
      id: position["Group"]["Position"]["Id"]
    }
  end
end

#search(term) ⇒ Array<Hash>

Searches for users based on a search term

Parameters:

  • term (String)

    the search term

Returns:

  • (Array<Hash>)

    an array of hashes containing search results



70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/bloomy/operations/users.rb', line 70

def search(term)
  response = @conn.get("search/user", term: term).body
  response.map do |user|
    {
      id: user["Id"],
      name: user["Name"],
      description: user["Description"],
      email: user["Email"],
      organization_id: user["OrganizationId"],
      image_url: user["ImageUrl"]
    }
  end
end