Class: Api::ReviewsIo::ProductReviewsImporter

Inherits:
Object
  • Object
show all
Defined in:
app/services/api/reviews_io/product_reviews_importer.rb

Overview

Imports all product reviews from Reviews.io API and stores them in the database.
Handles pagination, upserts, and marks deleted reviews.

Usage:
result = Api::ReviewsIo::ProductReviewsImporter.new.process

=> { created: 150, updated: 25, deleted: 3, total_pages: 10, errors: [] }

Can also be run with a custom client for testing:
Api::ReviewsIo::ProductReviewsImporter.new(client: mock_client).process

Constant Summary collapse

PER_PAGE =
100
MAX_PAGES =

Safety limit to prevent infinite loops

1000

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client: nil, logger: Rails.logger) ⇒ ProductReviewsImporter

Returns a new instance of ProductReviewsImporter.



20
21
22
23
24
# File 'app/services/api/reviews_io/product_reviews_importer.rb', line 20

def initialize(client: nil, logger: Rails.logger)
  @client = client
  @logger = logger
  @import_time = Time.current
end

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



18
19
20
# File 'app/services/api/reviews_io/product_reviews_importer.rb', line 18

def client
  @client
end

#import_timeObject (readonly)

Returns the value of attribute import_time.



18
19
20
# File 'app/services/api/reviews_io/product_reviews_importer.rb', line 18

def import_time
  @import_time
end

#loggerObject (readonly)

Returns the value of attribute logger.



18
19
20
# File 'app/services/api/reviews_io/product_reviews_importer.rb', line 18

def logger
  @logger
end

Instance Method Details

#processHash

Run the full import process

Returns:

  • (Hash)

    Import statistics



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'app/services/api/reviews_io/product_reviews_importer.rb', line 29

def process
  log_info('Starting Reviews.io product reviews import')

  stats = {
    created: 0,
    updated: 0,
    changed: 0,
    deleted: 0,
    total_pages: 0,
    total_reviews: 0,
    errors: []
  }

  begin
    ensure_client!
    stats = import_all_reviews(stats)
    stats[:deleted] = mark_stale_reviews_deleted
  rescue Client::MissingCredentialsError => e
    log_error("Reviews.io credentials missing: #{e.message}")
    stats[:errors] << "Credentials missing: #{e.message}"
  rescue Client::Error => e
    log_error("Reviews.io API error: #{e.message}")
    stats[:errors] << "API error: #{e.message}"
  rescue StandardError => e
    log_error("Unexpected error during import: #{e.class} - #{e.message}")
    log_error(e.backtrace.first(10).join("\n"))
    stats[:errors] << "Unexpected error: #{e.message}"
  end

  log_info("Import completed: created=#{stats[:created]}, updated=#{stats[:updated]}, " \
           "changed=#{stats[:changed]}, deleted=#{stats[:deleted]}, pages=#{stats[:total_pages]}, errors=#{stats[:errors].size}")

  stats
end