Class: Measurable

Inherits:
Object
  • Object
show all
Defined in:
lib/bloomy/operations/measurables.rb

Overview

Note:

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

Class to handle all the operations related to measurables

Instance Method Summary collapse

Constructor Details

#initialize(conn, user_id) ⇒ Measurable

Initializes a new Measurable instance

Parameters:

  • conn (Object)

    the connection object to interact with the API

  • user_id (Integer)

    the ID of the user



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

def initialize(conn, user_id)
  @conn = conn
  @user_id = user_id
end

Instance Method Details

#current_weekHash

Retrieves the current week details

Examples:

client.measurable.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



24
25
26
27
28
29
30
31
32
# File 'lib/bloomy/operations/measurables.rb', line 24

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

#scorecard(current_week_only: true, show_empty: true) ⇒ Array<Hash>

Retrieves the scorecard for the user

Examples:

client.measurable.scorecard
#=> [{ id: 123, title: "Sales", target: 100, value: 80, updated_at: "2024-06-12", week_number: 24 }, ...]

Parameters:

  • current_week_only (Boolean) (defaults to: true)

    whether to include only the current week’s scores (default: true)

  • show_empty (Boolean) (defaults to: true)

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

Returns:

  • (Array<Hash>)

    an array of hashes containing scorecard details



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/bloomy/operations/measurables.rb', line 42

def scorecard(current_week_only: true, show_empty: true)
  response = @conn.get("scorecard/user/mine").body
  scorecards = response["Scores"].map do |scorecard|
    {
      id: scorecard["Id"],
      title: scorecard["MeasurableName"],
      target: scorecard["Target"],
      value: scorecard["Measured"],
      updated_at: scorecard["DateEntered"],
      week_number: scorecard["ForWeek"]
    }
  end

  if current_week_only
    week_id = current_week[:week_number]
    scorecards.select do |scorecard|
      scorecard[:week_number] == week_id && (show_empty || scorecard[:value].nil?)
    end
  else
    scorecards.select { |scorecard| show_empty || scorecard[:value].nil? }
  end
end

#update(scorecard_id, measured) ⇒ Boolean

Updates a scorecard with a new measured value

Examples:

client.measurable.update(1, 85)
#=> true

Parameters:

  • scorecard_id (Integer)

    the ID of the scorecard to update

  • measured (Numeric)

    the new measured value

Returns:

  • (Boolean)

    true if the operation was successful, false otherwise



73
74
75
76
# File 'lib/bloomy/operations/measurables.rb', line 73

def update(scorecard_id, measured)
  response = @conn.put("scores/#{scorecard_id}", {value: measured}.to_json).status
  response == 200
end