Class: Reviews::RatingSnapshot

Inherits:
Object
  • Object
show all
Defined in:
app/services/reviews/rating_snapshot.rb

Overview

Resolves the best available rating/count for a SKU from the local database.

Constant Summary collapse

DEFAULT_OPTIONS =
{ fallback: true }.freeze
EMPTY_RESULT =
{ average: nil, count: 0, source: :none }.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(sku:, fallback: DEFAULT_OPTIONS[:fallback], logger: Rails.logger, **_options) ⇒ RatingSnapshot

Returns a new instance of RatingSnapshot.



15
16
17
18
19
# File 'app/services/reviews/rating_snapshot.rb', line 15

def initialize(sku:, fallback: DEFAULT_OPTIONS[:fallback], logger: Rails.logger, **_options)
  @sku = sku.to_s.strip
  @fallback = fallback
  @logger = logger
end

Instance Attribute Details

#fallbackObject (readonly)

Returns the value of attribute fallback.



9
10
11
# File 'app/services/reviews/rating_snapshot.rb', line 9

def fallback
  @fallback
end

#loggerObject (readonly)

Returns the value of attribute logger.



9
10
11
# File 'app/services/reviews/rating_snapshot.rb', line 9

def logger
  @logger
end

#skuObject (readonly)

Returns the value of attribute sku.



9
10
11
# File 'app/services/reviews/rating_snapshot.rb', line 9

def sku
  @sku
end

Class Method Details

.for_sku(sku) ⇒ Object



11
12
13
# File 'app/services/reviews/rating_snapshot.rb', line 11

def self.for_sku(sku, **)
  new(sku:, **).call
end

Instance Method Details

#callObject



21
22
23
24
25
26
27
28
29
30
31
32
# File 'app/services/reviews/rating_snapshot.rb', line 21

def call
  return EMPTY_RESULT if sku.blank?

  stats = ReviewsIo.stats_for_skus([sku])
  return EMPTY_RESULT unless stats[:num].to_i.positive? || stats[:star_avg].present?

  {
    average: stats[:star_avg]&.to_f,
    count: stats[:num].to_i,
    source: :reviews_io
  }
end