Skip to content

Commit

Permalink
feat: add brand score parser
Browse files Browse the repository at this point in the history
  • Loading branch information
benzend committed Oct 15, 2024
1 parent a638557 commit 6dd129c
Show file tree
Hide file tree
Showing 5 changed files with 136 additions and 2 deletions.
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
glare-ux-metrics-rb (0.2.0)
glare-ux-metrics-rb (0.2.1)

GEM
remote: https://rubygems.org/
Expand Down
79 changes: 79 additions & 0 deletions lib/glare/ux-metrics/ux_metrics.rb
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,85 @@ def correct_data
end
end

module BrandScore
class Parser
CHOICE_KEYS = %w[
helpful
clear
engaging
motivating
skeptical
confusing
uninteresting
overwhelming
].freeze

def initialize(questions:)
@questions = questions
end

attr_reader :questions

def valid?
return false unless questions.is_a?(Array)

choices = questions.first
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
choices = questions.first
result = choices[:helpful].to_f +
choices[:clear].to_f +
choices[:engaging].to_f +
choices[:motivating].to_f -
choices[:skeptical].to_f -
choices[:confusing].to_f -
choices[:uninteresting].to_f -
choices[:overwhelming].to_f

sentiment_status = if result > 1.5
"positive"
elsif result > 1
"neutral"
else
"negative"
end

sentiment_label = if sentiment_status == "positive"
"High Sentiment"
elsif sentiment_status == "neutral"
"Average Sentiment"
else
"Low Sentiment"
end

Result.new(result: result, threshold: sentiment_status, label: sentiment_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
2 changes: 1 addition & 1 deletion lib/glare/ux-metrics/version.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

module Glare
module UxMetrics
VERSION = "0.2.0"
VERSION = "0.2.1"
end
end
18 changes: 18 additions & 0 deletions sig/glare/ux_metrics/ux_metrics.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,24 @@ module Glare
end
end

module BrandScore
class Parser
CHOICE_KEYS: Array[String]
attr_reader questions: Array[Hash[::Symbol | ::String, ::String | ::Float | ::Integer] | Array[::String | ::Float | ::Integer]]

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

def valid?: -> bool

def parse: -> Result

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

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

describe Glare::UxMetrics::BrandScore do
let(:brand_score_data) do
[
{
helpful: 0.1,
clear: 0.1,
engaging: 0.1,
motivating: 0.1,
skeptical: 0.1,
confusing: 0.1,
uninteresting: 0.1,
overwhelming: 0.1
},
[0.2, 0.3, 0.4, 0.4, 0.2, 0.2, 0.2, 0.2, 0.0],
]
end

it "validates valid brand score data" do
data = Glare::UxMetrics::BrandScore::Parser.new(
questions: brand_score_data,
)
expect(data.valid?).to eq(true)
end

it "invalidates invalid brand score data" do
data = Glare::UxMetrics::BrandScore::Parser.new(questions: [{ hi: "yooo" }])
expect(data.valid?).to eq(false)
end

it "returns valid data" do
data = Glare::UxMetrics::BrandScore::Parser.new(
questions: brand_score_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 6dd129c

Please sign in to comment.