Skip to content

Commit

Permalink
feat(post-task satisfaction): create parser and validation
Browse files Browse the repository at this point in the history
  • Loading branch information
benzend committed Oct 14, 2024
1 parent f7346e5 commit c7b7677
Show file tree
Hide file tree
Showing 3 changed files with 127 additions and 12 deletions.
87 changes: 75 additions & 12 deletions lib/glare/ux-metrics/ux_metrics.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,13 @@ def valid?

def parse
result = choices[:helpful].to_f +
choices[:innovative].to_f +
choices[:simple].to_f +
choices[:joyful].to_f -
choices[:complicated].to_f -
choices[:confusing].to_f -
choices[:overwhelming].to_f -
choices[:annoying].to_f
choices[:innovative].to_f +
choices[:simple].to_f +
choices[:joyful].to_f -
choices[:complicated].to_f -
choices[:confusing].to_f -
choices[:overwhelming].to_f -
choices[:annoying].to_f

threshold = if result > 1.5
'positive'
Expand Down Expand Up @@ -88,10 +88,10 @@ def valid?

def parse
result = choices[:very_easy].to_f +
choices[:somewhat_easy].to_f -
choices[:neutral].to_f -
choices[:somewhat_difficult].to_f -
choices[:very_difficult].to_f
choices[:somewhat_easy].to_f -
choices[:neutral].to_f -
choices[:somewhat_difficult].to_f -
choices[:very_difficult].to_f

threshold = if result > 0.3
'positive'
Expand Down Expand Up @@ -162,7 +162,7 @@ def parse
didnt_match_at_all = choices['didnt_match_at_all']

result = (matched_very_well.to_f + somewhat_matched.to_f) -
(neutral_match.to_f + somewhat_didnt_match.to_f + didnt_match_at_all.to_f)
(neutral_match.to_f + somewhat_didnt_match.to_f + didnt_match_at_all.to_f)

threshold = if result > 0.3
'positive'
Expand Down Expand Up @@ -303,6 +303,69 @@ def correct_data
end
end

module PostTaskSatisfaction
class Data
CHOICE_KEYS = %w[very_satisfied somewhat_satisfied neutral somewhat_dissatisfied very_dissatisfied].freeze

def initialize(choices:)
@choices = choices
end

attr_reader :choices

def valid?
return false unless choices.is_a?(Hash) && choices.size

missing_attributes = CHOICE_KEYS - choices.keys.map(&:to_s)
return false unless missing_attributes.empty?

true
end

def parse
result = choices[:very_satisfied].to_f +
choices[:somewhat_satisfied].to_f -
choices[:neutral].to_f -
choices[:somewhat_dissatisfied].to_f -
choices[:very_dissatisfied].to_f

threshold = if result >= 0.7
'positive'
elsif result > 0.5
'neutral'
else
'negative'
end

label = if threshold == 'positive'
'High Satisfaction'
elsif threshold == 'neutral'
'Average Satisfaction'
else
'Low Satisfaction'
end

Result.new(result: result, threshold: threshold, label: label)
end

class InvalidDataError < Error
def initialize(msg = "Data not valid. Correct data format is: \n\n#{correct_data}")
super(msg)
end

def correct_data
{
very_satisfied: "string|integer|float",
somewhat_satisfied: "string|integer|float",
neutral: "string|integer|float",
somewhat_dissatisfied: "string|integer|float",
very_dissatisfied: "string|integer|float",
}.to_json
end
end
end
end

class Result
def initialize(result:, threshold:, label:)
@result = result
Expand Down
21 changes: 21 additions & 0 deletions sig/glare/ux_metrics/ux_metrics.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,27 @@ module Glare
end
end

module PostTaskSatisfaction
class Data
CHOICE_KEYS: Array[String]
attr_reader choices: Hash[::Symbol | ::String, ::String | ::Float | ::Integer]

def initialize: (
choices: Hash[::Symbol | ::String, ::String | ::Float | ::Integer],
) -> void

def valid?: -> bool

def parse: -> Result

def calculate_question: (Hash[::Symbol | ::String, ::String | ::Float | ::Integer]) -> float

class InvalidDataError < Error
def correct_data: -> String
end
end
end


class Result
attr_reader result: Float
Expand Down
31 changes: 31 additions & 0 deletions spec/glare/ux-metrics/ux_metrics.rb
Original file line number Diff line number Diff line change
Expand Up @@ -138,4 +138,35 @@
expect(data.result.is_a?(Float) && data.label.is_a?(String) && data.threshold.is_a?(String)).to eq(true)
end
end

describe Glare::UxMetrics::PostTaskSatisfaction do
let(:post_task_satisfaction_data) do
{
very_satisfied: 0.1,
somewhat_satisfied: 0.1,
neutral: 0.1,
somewhat_dissatisfied: 0.1,
very_dissatisfied: 0.1,
}
end

it "validates valid post-task satisfaction data" do
data = Glare::UxMetrics::PostTaskSatisfaction::Data.new(
choices: post_task_satisfaction_data,
)
expect(data.valid?).to eq(true)
end

it "invalidates invalid post-task satisfaction data" do
data = Glare::UxMetrics::PostTaskSatisfaction::Data.new(choices: { ha: "bla" })
expect(data.valid?).to eq(false)
end

it "returns valid data" do
data = Glare::UxMetrics::PostTaskSatisfaction::Data.new(
choices: post_task_satisfaction_data,
).parse
expect(data.result.is_a?(Float) && data.label.is_a?(String) && data.threshold.is_a?(String)).to eq(true)
end
end
end

0 comments on commit c7b7677

Please sign in to comment.