Class: LandingPageReviewAutoTagger

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

Overview

Picks a deduplicated, recent set of product reviews from one or more product lines
and appends a canonical page tag (e.g. +for-radiant-heat-panels-page+) to each.

Used to bootstrap reviews_io.tags when hand-curated IDs are not yet available.
Refine or replace tags in CRM after reviewing live copy.

See Also:

  • doc/plans/reviews_landing_page_unificationdoc/plans/reviews_landing_page_unification.md

Constant Summary collapse

DEFAULT_MIN_RATING =
4
DEFAULT_MIN_COMMENT_LENGTH =
80
DEFAULT_LIMIT =
12

Class Method Summary collapse

Class Method Details

.append_tag!(tag, ids) ⇒ Integer

Appends +tag+ to +tags+ JSONB when missing.

Returns:

  • (Integer)

    rows updated



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'app/services/landing_page_review_auto_tagger.rb', line 33

def append_tag!(tag, ids)
  return 0 if tag.blank? || ids.blank?

  id_list = ids.map(&:to_i).uniq
  return 0 if id_list.empty?

  json_payload = [tag].to_json
  conn = ::ReviewsIo.connection
  quoted_json = conn.quote(json_payload)
  sql = <<~SQL.squish
    UPDATE reviews_io
    SET    tags = COALESCE(tags, '[]'::jsonb) || #{quoted_json}::jsonb
    WHERE  id IN (#{id_list.join(',')})
      AND  NOT (COALESCE(tags, '[]'::jsonb) @> #{quoted_json}::jsonb)
  SQL

  conn.update(sql)
end

.pick_review_ids(product_line_urls:, limit: DEFAULT_LIMIT, min_rating: DEFAULT_MIN_RATING, min_comment_length: DEFAULT_MIN_COMMENT_LENGTH) ⇒ Array<Integer>

Returns review primary keys.

Parameters:

  • product_line_urls (Array<String>)

    ProductLine#url values (e.g. +radiant-panel-ember+)

  • limit (Integer) (defaults to: DEFAULT_LIMIT)

    max reviews after deduplication

  • min_rating (Integer) (defaults to: DEFAULT_MIN_RATING)

    minimum star rating

  • min_comment_length (Integer) (defaults to: DEFAULT_MIN_COMMENT_LENGTH)

    minimum trimmed comment length

Returns:

  • (Array<Integer>)

    review primary keys



21
22
23
24
25
26
27
28
29
# File 'app/services/landing_page_review_auto_tagger.rb', line 21

def pick_review_ids(product_line_urls:, limit: DEFAULT_LIMIT,
                    min_rating: DEFAULT_MIN_RATING,
                    min_comment_length: DEFAULT_MIN_COMMENT_LENGTH)
  skus = Array(product_line_urls).flat_map { |url| ::ReviewsIo.skus_for_product_line(url) }.uniq.compact_blank
  return [] if skus.blank?

  scope = base_review_scope(skus, min_rating, min_comment_length)
  deduped_recent_ids(scope, limit)
end