Class: Bloomy::Goal
- Inherits:
-
Object
- Object
- Bloomy::Goal
- Defined in:
- lib/bloomy/operations/goals.rb
Overview
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.
{complete: 2, on: 1, off: 0}.freeze
- STATUS_MAPPINGS =
Returns 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
-
#archive(goal_id) ⇒ Boolean
Archives a rock with the specified goal ID.
-
#create(title:, meeting_id:, user_id: self.user_id) ⇒ HashWithIndifferentAccess
Creates a new goal.
-
#delete(goal_id) ⇒ Boolean
Deletes a goal.
-
#initialize(conn) ⇒ Goal
constructor
Initializes a new Goal instance.
-
#list(user_id: nil, archived: false) ⇒ Array<HashWithIndifferentAccess>, HashWithIndifferentAccess
Lists all goals for a specific user.
-
#restore(goal_id) ⇒ Boolean
Restores a previously archived goal identified by the provided goal ID.
-
#update(goal_id:, title: nil, accountable_user: user_id, status: nil) ⇒ HashWithIndifferentAccess
Updates a goal.
Methods included from Utilities::Validation
#validate_id!, #validate_title!
Methods included from Utilities::Transform
#transform_array, #transform_response
Methods included from Utilities::UserIdUtility
Constructor Details
#initialize(conn) ⇒ Goal
Initializes a new Goal instance
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.
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
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
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
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.
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
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 |