Class: Bloomy::Todo

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

Overview

Class to handle all the operations related to todos

Constant Summary

Constants included from Utilities::Transform

Utilities::Transform::DATE_FIELDS

Instance Method Summary collapse

Methods included from Utilities::Validation

#validate_id!, #validate_title!

Methods included from Utilities::Transform

#transform_array, #transform_response

Methods included from Utilities::UserIdUtility

#user_id

Constructor Details

#initialize(conn) ⇒ Todo

Initializes a new Todo instance

Parameters:

  • conn (Object)

    the connection object to interact with the API



16
17
18
# File 'lib/bloomy/operations/todos.rb', line 16

def initialize(conn)
  @conn = conn
end

Instance Method Details

#complete(todo_id) ⇒ Boolean

Marks a todo as complete

Examples:

todo.complete(1)
#=> true

Parameters:

  • todo_id (Integer)

    the ID of the todo to complete

Returns:

  • (Boolean)

    true if the operation was successful

Raises:



89
90
91
92
# File 'lib/bloomy/operations/todos.rb', line 89

def complete(todo_id)
  response = @conn.post("todo/#{todo_id}/complete?status=true")
  handle_response!(response, context: "complete todo")
end

#create(title:, meeting_id:, due_date: nil, user_id: self.user_id, notes: nil) ⇒ HashWithIndifferentAccess

Creates a new todo

Examples:

client.todo.create(title: "New Todo", meeting_id: 1, due_date: "2024-06-15")
#=> { id: 1, title: "New Todo", due_date: "2024-06-15", ... }

Parameters:

  • title (String)

    the title of the new todo

  • meeting_id (Integer)

    the ID of the meeting associated with the todo

  • due_date (String, nil) (defaults to: nil)

    the due date of the todo (optional)

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

    the ID of the user responsible for the todo (default: initialized user ID)

  • notes (String, nil) (defaults to: nil)

    additional notes for the todo (optional)

Returns:

  • (HashWithIndifferentAccess)

    the newly created todo hash

Raises:

  • (ArgumentError)

    if title is empty or meeting_id is invalid

  • (NotFoundError)

    when meeting is not found

  • (ApiError)

    when the API request fails



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/bloomy/operations/todos.rb', line 61

def create(title:, meeting_id:, due_date: nil, user_id: self.user_id, notes: nil)
  validate_title!(title)
  validate_id!(meeting_id, context: "meeting_id")

  payload = {title: title, accountableUserId: user_id, notes: notes}
  payload[:dueDate] = due_date if due_date
  response = @conn.post("L10/#{meeting_id}/todos", payload.to_json)
  data = handle_response(response, context: "create todo")

  transform_response({
    id: data.dig("Id"),
    title: data.dig("Name"),
    notes_url: data.dig("DetailsUrl"),
    due_date: data.dig("DueDate"),
    created_at: DateTime.now.to_s,
    status: "Incomplete"
  })
end

#details(todo_id) ⇒ HashWithIndifferentAccess

Retrieves the details of a specific todo item by its ID.

Examples:

client.todo.details(1)
#=> { id: 1, title: "Updated Todo", due_date: "2024-11-01", ... }

Parameters:

  • todo_id (Integer)

    The ID of the todo item to retrieve.

Returns:

  • (HashWithIndifferentAccess)

    The requested todo hash

Raises:



134
135
136
137
138
139
# File 'lib/bloomy/operations/todos.rb', line 134

def details(todo_id)
  response = @conn.get("todo/#{todo_id}")
  data = handle_response(response, context: "get todo details")

  transform_response(build_todo_hash(data))
end

#list(user_id: nil, meeting_id: nil) ⇒ Array<HashWithIndifferentAccess>

Lists all todos for a specific user or meeting

Examples:

# Fetch todos for the current user
client.todo.list
#=> [{ id: 1, title: "New Todo", due_date: "2024-06-15", ... }]

Parameters:

  • user_id (Integer, nil) (defaults to: nil)

    the ID of the user (default is the initialized user ID)

  • meeting_id (Integer, nil) (defaults to: nil)

    the ID of the meeting

Returns:

  • (Array<HashWithIndifferentAccess>)

    an array of todo hashes

Raises:

  • (ArgumentError)

    if both ‘user_id` and `meeting_id` are provided

  • (NotFoundError)

    when user or meeting is not found

  • (ApiError)

    when the API request fails



32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/bloomy/operations/todos.rb', line 32

def list(user_id: nil, meeting_id: nil)
  raise ArgumentError, "Please provide either `user_id` or `meeting_id`, not both." if user_id && meeting_id

  if meeting_id
    response = @conn.get("l10/#{meeting_id}/todos")
    data = handle_response(response, context: "list meeting todos")
  else
    user_id ||= self.user_id
    response = @conn.get("todo/user/#{user_id}")
    data = handle_response(response, context: "list user todos")
  end

  transform_array(data.map { |todo| build_todo_hash(todo) })
end

#update(todo_id:, title: nil, due_date: nil) ⇒ HashWithIndifferentAccess

Updates an existing todo

Examples:

todo.update(todo_id: 1, title: "Updated Todo", due_date: "2024-11-01")
#=> { id: 1, title: "Updated Todo", due_date: "2024-11-01", ... }

Parameters:

  • todo_id (Integer)

    the ID of the todo to update

  • title (String, nil) (defaults to: nil)

    the new title of the todo (optional)

  • due_date (String, nil) (defaults to: nil)

    the new due date of the todo (optional)

Returns:

  • (HashWithIndifferentAccess)

    the updated todo hash

Raises:

  • (ArgumentError)

    if no update fields are provided

  • (NotFoundError)

    when todo is not found

  • (ApiError)

    when the API request fails



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

def update(todo_id:, title: nil, due_date: nil)
  payload = {}
  payload[:title] = title if title
  payload[:dueDate] = due_date if due_date

  raise ArgumentError, "At least one field must be provided" if payload.empty?

  response = @conn.put("todo/#{todo_id}", payload.to_json)
  handle_response!(response, context: "update todo")

  transform_response({
    id: todo_id,
    title: title,
    due_date: due_date,
    created_at: nil,
    status: "Incomplete"
  })
end