Class: Api::ReviewsIo::StoreReviewsImporter
- Inherits:
-
Object
- Object
- Api::ReviewsIo::StoreReviewsImporter
- Defined in:
- app/services/api/reviews_io/store_reviews_importer.rb
Overview
Imports all store reviews from Reviews.io API and stores them in the database.
Handles pagination, upserts, and marks deleted reviews.
Store reviews are company-level reviews (not tied to a specific product/SKU).
Uses the /reviews endpoint with type=store_review parameter.
Usage:
result = Api::ReviewsIo::StoreReviewsImporter.new.process
=> { created: 50, updated: 10, deleted: 2, total_pages: 5, errors: [] }
Can also be run with a custom client for testing:
Api::ReviewsIo::StoreReviewsImporter.new(client: mock_client).process
Constant Summary collapse
- PER_PAGE =
100- MAX_PAGES =
Safety limit to prevent infinite loops
1000- REVIEW_TYPE =
'store_review'
Instance Attribute Summary collapse
-
#client ⇒ Object
readonly
Returns the value of attribute client.
-
#import_time ⇒ Object
readonly
Returns the value of attribute import_time.
-
#logger ⇒ Object
readonly
Returns the value of attribute logger.
Instance Method Summary collapse
-
#initialize(client: nil, logger: Rails.logger) ⇒ StoreReviewsImporter
constructor
A new instance of StoreReviewsImporter.
-
#process ⇒ Hash
Run the full import process.
Constructor Details
#initialize(client: nil, logger: Rails.logger) ⇒ StoreReviewsImporter
Returns a new instance of StoreReviewsImporter.
24 25 26 27 28 |
# File 'app/services/api/reviews_io/store_reviews_importer.rb', line 24 def initialize(client: nil, logger: Rails.logger) @client = client @logger = logger @import_time = Time.current end |
Instance Attribute Details
#client ⇒ Object (readonly)
Returns the value of attribute client.
22 23 24 |
# File 'app/services/api/reviews_io/store_reviews_importer.rb', line 22 def client @client end |
#import_time ⇒ Object (readonly)
Returns the value of attribute import_time.
22 23 24 |
# File 'app/services/api/reviews_io/store_reviews_importer.rb', line 22 def import_time @import_time end |
#logger ⇒ Object (readonly)
Returns the value of attribute logger.
22 23 24 |
# File 'app/services/api/reviews_io/store_reviews_importer.rb', line 22 def logger @logger end |
Instance Method Details
#process ⇒ Hash
Run the full import process
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 63 64 65 66 |
# File 'app/services/api/reviews_io/store_reviews_importer.rb', line 33 def process log_info('Starting Reviews.io store 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.}") stats[:errors] << "Credentials missing: #{e.}" rescue Client::Error => e log_error("Reviews.io API error: #{e.}") stats[:errors] << "API error: #{e.}" rescue StandardError => e log_error("Unexpected error during import: #{e.class} - #{e.}") log_error(e.backtrace.first(10).join("\n")) stats[:errors] << "Unexpected error: #{e.}" 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 |