Class: Bloomy::User

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

Overview

Class to handle all the operations related to users

Constant Summary

Constants included from Bloomy::Utilities::Transform

Bloomy::Utilities::Transform::DATE_FIELDS

Instance Method Summary collapse

Methods included from Bloomy::Utilities::Transform

#transform_array, #transform_response

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



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

def initialize(conn)
  @conn = conn
end

Instance Method Details

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

Retrieves all users in the system

Parameters:

  • include_placeholders (Boolean) (defaults to: false)

    whether to include placeholder users (default: false)

Returns:

  • (Array<HashWithIndifferentAccess>)

    an array of hashes containing user details

Raises:

  • (ApiError)

    when the API request fails



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/bloomy/operations/users.rb', line 105

def all(include_placeholders: false)
  response = @conn.get("search/all", term: "%")
  data = handle_response(response, context: "get all users")

  transform_array(
    data
      .select { |user| user.dig("ResultType") == "User" }
      .select { |user| include_placeholders || user.dig("ImageUrl") != "/i/userplaceholder" }
      .map do |user|
        {
          id: user.dig("Id"),
          name: user.dig("Name"),
          email: user.dig("Email"),
          position: user.dig("Description"),
          image_url: user.dig("ImageUrl")
        }
      end
  )
end

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

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:

  • (HashWithIndifferentAccess)

    a hash containing user details

Raises:



27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/bloomy/operations/users.rb', line 27

def details(user_id = self.user_id, direct_reports: false, positions: false, all: false)
  response = @conn.get("users/#{user_id}")
  data = handle_response(response, context: "get user details")

  user_details = {
    id: data.dig("Id"),
    name: data.dig("Name"),
    image_url: data.dig("ImageUrl")
  }

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

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

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<HashWithIndifferentAccess>)

    an array of hashes containing direct report details

Raises:



48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/bloomy/operations/users.rb', line 48

def direct_reports(user_id = self.user_id)
  response = @conn.get("users/#{user_id}/directreports")
  data = handle_response(response, context: "get direct reports")

  transform_array(data.map do |report|
    {
      name: report.dig("Name"),
      id: report.dig("Id"),
      image_url: report.dig("ImageUrl")
    }
  end)
end

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

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<HashWithIndifferentAccess>)

    an array of hashes containing position details

Raises:



67
68
69
70
71
72
73
74
75
76
77
# File 'lib/bloomy/operations/users.rb', line 67

def positions(user_id = self.user_id)
  response = @conn.get("users/#{user_id}/seats")
  data = handle_response(response, context: "get user positions")

  transform_array(data.map do |position|
    {
      name: position.dig("Group", "Position", "Name"),
      id: position.dig("Group", "Position", "Id")
    }
  end)
end

#search(term) ⇒ Array<HashWithIndifferentAccess>

Searches for users based on a search term

Parameters:

  • term (String)

    the search term

Returns:

  • (Array<HashWithIndifferentAccess>)

    an array of hashes containing search results

Raises:

  • (ApiError)

    when the API request fails



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/bloomy/operations/users.rb', line 84

def search(term)
  response = @conn.get("search/user", term: term)
  data = handle_response(response, context: "search users")

  transform_array(data.map do |user|
    {
      id: user.dig("Id"),
      name: user.dig("Name"),
      description: user.dig("Description"),
      email: user.dig("Email"),
      organization_id: user.dig("OrganizationId"),
      image_url: user.dig("ImageUrl")
    }
  end)
end