Class: Bloomy::Goal

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

Overview

Note:

This class is already initialized via the client and usable as ‘client.goal.method`

Class to handle all the operations related to goals (also known as “rocks”)

Instance Method Summary collapse

Methods included from Utilities::UserIdUtility

#user_id

Constructor Details

#initialize(conn) ⇒ Goal

Initializes a new Goal instance

Parameters:

  • conn (Object)

    the connection object to interact with the API



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

def initialize(conn)
  @conn = conn
end

Instance Method Details

#archive(goal_id) ⇒ Boolean

Archives a rock with the specified goal ID.

Examples:

goals.archive(123) #=> true

Parameters:

  • goal_id (Integer)

    The ID of the goal/rock to archive

Returns:

  • (Boolean)

    Returns true if the archival was successful, false otherwise



121
122
123
124
# File 'lib/bloomy/operations/goals.rb', line 121

def archive(goal_id)
  response = @conn.put("rocks/#{goal_id}/archive")
  response.success?
end

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

Creates a new goal

Examples:

client.goal.create(title: "New Goal", meeting_id: 1)
#=> { goal_id: 1, title: "New Goal", meeting_id: 1, ... }

Parameters:

  • title (String)

    the title of the new goal

  • meeting_id (Integer)

    the ID of the meeting associated with the goal

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

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

Returns:

  • (Hash)

    the newly created goal



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

def create(title:, meeting_id:, user_id: self.user_id)
  payload = {title: title, accountableUserId: user_id}.to_json
  response = @conn.post("L10/#{meeting_id}/rocks", payload).body

  {
    id: response["Id"],
    user_id: user_id,
    user_name: response["Owner"]["Name"],
    title: title,
    meeting_id: meeting_id,
    meeting_title: response["Origins"][0]["Name"],
    status: {complete: 2, on: 1, off: 0}.key(response["Completion"]).to_s,
    created_at: response["CreateTime"]
  }
end

#delete(goal_id) ⇒ Hash

Deletes a goal

Examples:

client.goal.delete(1)
#=> { status: 200 }

Parameters:

  • goal_id (Integer)

    the ID of the goal to delete

Returns:

  • (Hash)

    a hash containing the status of the delete operation



85
86
87
88
# File 'lib/bloomy/operations/goals.rb', line 85

def delete(goal_id)
  response = @conn.delete("rocks/#{goal_id}")
  response.success?
end

#list(user_id = self.user_id, archived: false) ⇒ Array<Hash>, Hash

Lists all goals for a specific user

Examples:

List active goals

client.goal.list
#=> [{ id: 1, title: "Complete project", ... }]

List both active and archived goals

client.goal.list(archived: true)
#=> {
  active: [{ id: 1, ... }],
  archived: [{ id: 2, ... }]
}

Parameters:

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

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

  • archived (Boolean) (defaults to: false)

    whether to include archived goals (default: false)

Returns:

  • (Array<Hash>, Hash)

    Returns either:

    • An array of goal hashes if archived is false

    • A hash with :active and :archived arrays of goal hashes if archived is true



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/bloomy/operations/goals.rb', line 35

def list(user_id = self.user_id, archived: false)
  active_goals = @conn.get("rocks/user/#{user_id}?include_origin=true").body.map do |goal|
    {
      id: goal["Id"],
      user_id: goal["Owner"]["Id"],
      user_name: goal["Owner"]["Name"],
      title: goal["Name"],
      created_at: goal["CreateTime"],
      due_date: goal["DueDate"],
      status: goal["Complete"] ? "Completed" : "Incomplete",
      meeting_id: goal["Origins"].empty? ? nil : goal["Origins"][0]["Id"],
      meeting_title: goal["Origins"].empty? ? nil : goal["Origins"][0]["Name"]
    }
  end

  archived ? {active: active_goals, archived: get_archived_goals(user_id)} : active_goals
end

#restore(goal_id) ⇒ Boolean

Restores a previously archived goal identified by the provided goal ID.

Examples:

Restoring a goal

goals.restore("123") #=> true

Parameters:

  • goal_id (String, Integer)

    The unique identifier of the goal to restore

Returns:

  • (Boolean)

    true if the restore operation was successful, false otherwise



132
133
134
135
# File 'lib/bloomy/operations/goals.rb', line 132

def restore(goal_id)
  response = @conn.put("rocks/#{goal_id}/restore")
  response.success?
end

#update(goal_id:, title: nil, accountable_user: user_id, status: nil) ⇒ Boolean

Updates a goal

Examples:

client.goal.update(goal_id: 1, title: "Updated Goal", status: 'on')
#=> true

Parameters:

  • goal_id (Integer)

    the ID of the goal to update

  • title (String) (defaults to: nil)

    the new title of the goal

  • accountable_user (Integer) (defaults to: user_id)

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

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

    the status value (‘on’, ‘off’, or ‘complete’)

Returns:

  • (Boolean)

    true if the update was successful

Raises:

  • (ArgumentError)

    if an invalid status value is provided



101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/bloomy/operations/goals.rb', line 101

def update(goal_id:, title: nil, accountable_user: user_id, status: nil)
  if status
    valid_status = {on: "OnTrack", off: "AtRisk", complete: "Complete"}
    status_key = status.downcase.to_sym
    unless valid_status.key?(status_key)
      raise ArgumentError, "Invalid status value. Must be 'on', 'off', or 'complete'."
    end
    status = valid_status[status_key]
  end
  payload = {title: title, accountableUserId: accountable_user, completion: status}.to_json
  response = @conn.put("rocks/#{goal_id}", payload)
  response.success?
end