Class: Bloomy::Goal

Inherits:
Object
  • Object
show all
Includes:
Utilities::Transform, Utilities::UserIdUtility, Utilities::Validation
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”)

Constant Summary collapse

COMPLETION_VALUES =

Returns Maps status symbols to API completion values.

Returns:

  • (Hash)

    Maps status symbols to API completion values

{complete: 2, on: 1, off: 0}.freeze
STATUS_MAPPINGS =

Returns Maps status symbols to API status strings.

Returns:

  • (Hash)

    Maps status symbols to API status strings

{on: "OnTrack", off: "AtRisk", complete: "Complete"}.freeze

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) ⇒ Goal

Initializes a new Goal instance

Parameters:

  • conn (Object)

    the connection object to interact with the API



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

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

Raises:



159
160
161
162
# File 'lib/bloomy/operations/goals.rb', line 159

def archive(goal_id)
  response = @conn.put("rocks/#{goal_id}/archive")
  handle_response!(response, context: "archive goal")
end

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

Creates a new goal

Examples:

client.goal.create(title: "New Goal", meeting_id: 1)
#=> { 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:

  • (HashWithIndifferentAccess)

    the newly created goal

Raises:

  • (ArgumentError)

    if title is empty or meeting_id is invalid

  • (NotFoundError)

    when meeting is not found

  • (ApiError)

    when the API request fails



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/bloomy/operations/goals.rb', line 83

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

  payload = {title: title, accountableUserId: user_id}.to_json
  response = @conn.post("L10/#{meeting_id}/rocks", payload)
  data = handle_response(response, context: "create goal")

  origins = data.dig("Origins") || []
  transform_response({
    id: data.dig("Id"),
    user_id: user_id,
    user_name: data.dig("Owner", "Name"),
    title: title,
    meeting_id: meeting_id,
    meeting_title: origins.dig(0, "Name"),
    status: COMPLETION_VALUES.key(data.dig("Completion")).to_s,
    created_at: data.dig("CreateTime")
  })
end

#delete(goal_id) ⇒ Boolean

Deletes a goal

Examples:

client.goal.delete(1)
#=> true

Parameters:

  • goal_id (Integer)

    the ID of the goal to delete

Returns:

  • (Boolean)

    true if deletion was successful

Raises:



113
114
115
116
# File 'lib/bloomy/operations/goals.rb', line 113

def delete(goal_id)
  response = @conn.delete("rocks/#{goal_id}")
  handle_response!(response, context: "delete goal")
end

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

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, ... }]
}

List goals for specific user

client.goal.list(user_id: 42)

Parameters:

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

    the ID of the user (default: current user)

  • archived (Boolean) (defaults to: false)

    whether to include archived goals (default: false)

Returns:

  • (Array<HashWithIndifferentAccess>, HashWithIndifferentAccess)

    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

Raises:



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/bloomy/operations/goals.rb', line 48

def list(user_id: nil, archived: false)
  user_id ||= self.user_id
  response = @conn.get("rocks/user/#{user_id}?include_origin=true")
  data = handle_response(response, context: "list goals")

  active_goals = transform_array(data.map do |goal|
    origins = goal.dig("Origins") || []
    {
      id: goal.dig("Id"),
      user_id: goal.dig("Owner", "Id"),
      user_name: goal.dig("Owner", "Name"),
      title: goal.dig("Name"),
      created_at: goal.dig("CreateTime"),
      due_date: goal.dig("DueDate"),
      status: goal.dig("Complete") ? "Completed" : "Incomplete",
      meeting_id: origins.empty? ? nil : origins.dig(0, "Id"),
      meeting_title: origins.empty? ? nil : origins.dig(0, "Name")
    }
  end)

  archived ? transform_response({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

Raises:



172
173
174
175
# File 'lib/bloomy/operations/goals.rb', line 172

def restore(goal_id)
  response = @conn.put("rocks/#{goal_id}/restore")
  handle_response!(response, context: "restore goal")
end

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

Updates a goal

Examples:

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

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:

  • (HashWithIndifferentAccess)

    the updated goal

Raises:

  • (ArgumentError)

    if an invalid status value is provided

  • (NotFoundError)

    when goal is not found

  • (ApiError)

    when the API request fails



131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/bloomy/operations/goals.rb', line 131

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

  transform_response({
    id: goal_id,
    title: title,
    user_id: accountable_user,
    status: status
  })
end