Class: Bloomy::Todo

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

Overview

Class to handle all the operations related to todos

Instance Method Summary collapse

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



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

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



84
85
86
87
# File 'lib/bloomy/operations/todos.rb', line 84

def complete(todo_id)
  response = @conn.post("/api/v1/todo/#{todo_id}/complete?status=true")
  response.success?
end

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

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:

  • (Hash)

    the newly created todo hash



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

def create(title:, meeting_id:, due_date: nil, user_id: self.user_id, notes: nil)
  payload = {title: title, accountableUserId: user_id, notes: notes}
  payload[:dueDate] = due_date if due_date
  response = @conn.post("/api/v1/L10/#{meeting_id}/todos", payload.to_json).body

  {
    id: response["Id"],
    title: response["Name"],
    notes_url: response["DetailsUrl"],
    due_date: response["DueDate"],
    created_at: DateTime.now.to_s,
    status: "Incomplete"
  }
end

#details(todo_id) ⇒ Hash

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:

  • (Hash)

    The requested todo hash

Raises:

  • (RuntimeError)

    If the request to retrieve the todo details fails.



127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/bloomy/operations/todos.rb', line 127

def details(todo_id)
  response = @conn.get("/api/v1/todo/#{todo_id}")
  raise "Failed to get todo details. Status: #{response.status}" unless response.success?

  todo = response.body
  {
    id: todo["Id"],
    title: todo["Name"],
    notes_url: todo["DetailsUrl"],
    due_date: todo["DueDate"],
    created_at: todo["CreateTime"],
    completed_at: todo["CompleteTime"],
    status: todo["Complete"] ? "Complete" : "Incomplete"
  }
end

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

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

    an array of todo hashes

Raises:

  • (ArgumentError)

    if both ‘user_id` and `meeting_id` are provided



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/bloomy/operations/todos.rb', line 28

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").body
  else
    user_id ||= self.user_id
    response = @conn.get("todo/user/#{user_id}").body
  end

  response.map do |todo|
    {
      id: todo["Id"],
      title: todo["Name"],
      notes_url: todo["DetailsUrl"],
      due_date: todo["DueDate"],
      created_at: todo["CreateTime"],
      completed_at: todo["CompleteTime"],
      status: todo["Complete"] ? "Complete" : "Incomplete"
    }
  end
end

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

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:

  • (Hash)

    the updated todo hash

Raises:

  • (ArgumentError)

    if no update fields are provided

  • (RuntimeError)

    if the update request fails



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/bloomy/operations/todos.rb', line 100

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("/api/v1/todo/#{todo_id}", payload.to_json)
  raise "Failed to update todo. Status: #{response.status}" unless response.status == 200

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