Class: Bloomy::Scorecard

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

Overview

Note:

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

Class to handle all the operations related to scorecards

Instance Method Summary collapse

Methods included from Utilities::UserIdUtility

#user_id

Constructor Details

#initialize(conn) ⇒ Scorecard

Initializes a new Scorecard instance

Parameters:

  • conn (Object)

    the connection object to interact with the API



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

def initialize(conn)
  @conn = conn
end

Instance Method Details

#current_weekHash

Retrieves the current week details

Examples:

client.scorecard.current_week
#=> { id: 123, week_number: 24, week_start: "2024-06-10", week_end: "2024-06-16" }

Returns:

  • (Hash)

    a hash containing current week details



26
27
28
29
30
31
32
33
34
# File 'lib/bloomy/operations/scorecard.rb', line 26

def current_week
  response = @conn.get("weeks/current").body
  {
    id: response["Id"],
    week_number: response["ForWeekNumber"],
    week_start: response["LocalDate"]["Date"],
    week_end: response["ForWeek"]
  }
end

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

Note:

The ‘week_offset` parameter is useful when fetching scores for previous or future weeks. For example, to fetch scores for the previous week, you can set `week_offset` to -1. To fetch scores for a future week, you can set `week_offset` to a positive value.

Retrieves the scorecards for a user or a meeting.

Examples:

# Fetch scorecards for the current user
client.scorecard.list

# Fetch scorecards for a specific user
client.scorecard.list(user_id: 42)

# Fetch scorecards for a specific meeting
client.scorecard.list(meeting_id: 99)

Parameters:

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

    the ID of the user (defaults to initialized user_id)

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

    the ID of the meeting

  • show_empty (Boolean) (defaults to: false)

    whether to include scores with nil values (default: false)

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

    offset for the week number to filter scores

Returns:

  • (Array<Hash>)

    an array of scorecard hashes

Raises:

  • (ArgumentError)

    if both ‘user_id` and `meeting_id` are provided



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/bloomy/operations/scorecard.rb', line 57

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

  if meeting_id
    response = @conn.get("scorecard/meeting/#{meeting_id}").body
  else
    user_id ||= self.user_id
    response = @conn.get("scorecard/user/#{user_id}").body
  end

  scorecards = response["Scores"].map do |scorecard|
    {
      id: scorecard["Id"],
      measurable_id: scorecard["MeasurableId"],
      accountable_user_id: scorecard["AccountableUserId"],
      title: scorecard["MeasurableName"],
      target: scorecard["Target"],
      value: scorecard["Measured"],
      week: scorecard["Week"],
      week_id: scorecard["ForWeek"],
      updated_at: scorecard["DateEntered"]
    }
  end

  if week_offset
    week_data = current_week
    week_id = week_data[:week_number] + week_offset
    scorecards.select! { |scorecard| scorecard[:week_id] == week_id }
  end

  scorecards.select! { |scorecard| scorecard[:value] || show_empty } unless show_empty
  scorecards
end

#score(measurable_id:, score:, week_offset: 0) ⇒ Boolean

Updates the score for a measurable item for a specific week.

Examples:

client.scorecard.score(measurable_id: 123, score: 5)
#=> true

Parameters:

  • measurable_id (Integer)

    the ID of the measurable item

  • score (Numeric)

    the score to be assigned to the measurable item

  • week_offset (Integer) (defaults to: 0)

    the number of weeks to offset from the current week (default: 0)

Returns:

  • (Boolean)

    true if the score was successfully updated



100
101
102
103
104
105
106
# File 'lib/bloomy/operations/scorecard.rb', line 100

def score(measurable_id:, score:, week_offset: 0)
  week_data = current_week
  week_id = week_data[:week_number] + week_offset

  response = @conn.put("measurables/#{measurable_id}/week/#{week_id}", {value: score}.to_json)
  response.success?
end