Class: User

Inherits:
Object
  • Object
show all
Defined in:
lib/bloomy/operations/users.rb

Overview

Class to handle all the operations related to users

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(conn) ⇒ User

Initializes a new User instance

Parameters:

  • conn (Object)

    the connection object to interact with the API



10
11
12
13
# File 'lib/bloomy/operations/users.rb', line 10

def initialize(conn)
  @conn = conn
  @default_user_id = current_user_id
end

Instance Attribute Details

#default_user_idObject (readonly)

Returns the value of attribute default_user_id.



5
6
7
# File 'lib/bloomy/operations/users.rb', line 5

def default_user_id
  @default_user_id
end

Instance Method Details

#current_user_idInteger

Retrieves the current user’s ID

Examples:

client.user.current_user_id
#=> 1

Returns:

  • (Integer)

    the ID of the current user



21
22
23
24
# File 'lib/bloomy/operations/users.rb', line 21

def current_user_id
  response = @conn.get("users/mine").body
  response["Id"]
end

#details(user_id: @default_user_id, direct_reports: false, positions: false, all: false) ⇒ Hash

Retrieves details of a specific user

Examples:

client.user.details
#=> {name: "John Doe", id: 1, image_url: "http://example.com/image.jpg", ...}

Parameters:

  • user_id (Integer) (defaults to: @default_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



36
37
38
39
40
41
42
43
44
# File 'lib/bloomy/operations/users.rb', line 36

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

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

  user_details
end

#direct_reports(user_id: @default_user_id) ⇒ Array<Hash>

Retrieves direct reports of a specific user

Examples:

client.user.direct_reports
#=> [{name: "Jane Smith", id: 2, image_url: "http://example.com/image.jpg"}, ...]

Parameters:

  • user_id (Integer) (defaults to: @default_user_id)

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

Returns:

  • (Array<Hash>)

    an array of hashes containing direct report details



53
54
55
56
# File 'lib/bloomy/operations/users.rb', line 53

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

#positions(user_id: @default_user_id) ⇒ Array<Hash>

Retrieves positions of a specific user

Examples:

user.positions
#=> [{name: "Manager", id: 3}, ...]

Parameters:

  • user_id (Integer) (defaults to: @default_user_id)

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

Returns:

  • (Array<Hash>)

    an array of hashes containing position details



65
66
67
68
69
70
# File 'lib/bloomy/operations/users.rb', line 65

def positions(user_id: @default_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

Examples:

user.search("John")
#=> [{id: 1, name: "John Doe", description: "Developer", ...}, ...]

Parameters:

  • term (String)

    the search term

Returns:

  • (Array<Hash>)

    an array of hashes containing search results



79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/bloomy/operations/users.rb', line 79

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