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 =

Default options.

{ fallback: true }.freeze
EMPTY_RESULT =

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.



18
19
20
21
22
# File 'app/services/reviews/rating_snapshot.rb', line 18

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.



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

def fallback
  @fallback
end

#loggerObject (readonly)

Returns the value of attribute logger.



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

def logger
  @logger
end

#skuObject (readonly)

Returns the value of attribute sku.



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

def sku
  @sku
end

Class Method Details

.for_sku(sku) ⇒ Object



14
15
16
# File 'app/services/reviews/rating_snapshot.rb', line 14

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

Instance Method Details

#callObject



24
25
26
27
28
29
30
31
32
33
34
35
# File 'app/services/reviews/rating_snapshot.rb', line 24

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