Class: LigaMagicScraper::AlertSystem
- Inherits:
-
Object
- Object
- LigaMagicScraper::AlertSystem
- Includes:
- Loggable
- Defined in:
- lib/ligamagic_scraper/alerts/alert_system.rb
Instance Attribute Summary collapse
-
#alerts ⇒ Object
readonly
Returns the value of attribute alerts.
-
#config ⇒ Object
readonly
Returns the value of attribute config.
Attributes included from Loggable
Instance Method Summary collapse
- #detect_changes(previous_data, current_data) ⇒ Object
-
#initialize(config = {}) ⇒ AlertSystem
constructor
A new instance of AlertSystem.
- #process(current_data:, previous_file: nil) ⇒ Object
Methods included from Loggable
#clear_logs, #formatted_logs, #initialize_logs, #log, #log_debug, #log_error, #log_info, #log_warning
Constructor Details
#initialize(config = {}) ⇒ AlertSystem
Returns a new instance of AlertSystem.
12 13 14 15 16 17 18 19 20 21 22 |
# File 'lib/ligamagic_scraper/alerts/alert_system.rb', line 12 def initialize(config = {}) initialize_logs @config = { enabled: config[:enabled] || false, alert_types: config[:alert_types] || [:file], compare_previous: config[:compare_previous] != false } @alerts = [] setup_alerts if @config[:enabled] end |
Instance Attribute Details
#alerts ⇒ Object (readonly)
Returns the value of attribute alerts.
10 11 12 |
# File 'lib/ligamagic_scraper/alerts/alert_system.rb', line 10 def alerts @alerts end |
#config ⇒ Object (readonly)
Returns the value of attribute config.
10 11 12 |
# File 'lib/ligamagic_scraper/alerts/alert_system.rb', line 10 def config @config end |
Instance Method Details
#detect_changes(previous_data, current_data) ⇒ Object
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
# File 'lib/ligamagic_scraper/alerts/alert_system.rb', line 46 def detect_changes(previous_data, current_data) changes = { has_changes: false, timestamp: Time.now.iso8601, search_type: current_data[:search_type], search_term: current_data[:search_term] || current_data[:store_domain], new_products: [], removed_products: [], price_changes: [], quantity_changes: [], availability_changes: [] } prev_products = index_products(previous_data[:products]) curr_products = index_products(current_data[:products]) # Detect new products curr_products.each do |id, product| unless prev_products.key?(id) changes[:new_products] << product changes[:has_changes] = true end end # Detect removed products prev_products.each do |id, product| unless curr_products.key?(id) changes[:removed_products] << product changes[:has_changes] = true end end # Detect price changes curr_products.each do |id, curr_product| if prev_products.key?(id) prev_product = prev_products[id] # Check price changes (works for both global and store scrapers) price_change = detect_price_change(prev_product, curr_product) if price_change changes[:price_changes] << price_change changes[:has_changes] = true end # Check quantity changes (store scraper) if curr_product.key?(:qtd) && prev_product.key?(:qtd) prev_qtd = prev_product[:qtd] curr_qtd = curr_product[:qtd] if prev_qtd != curr_qtd changes[:quantity_changes] << { id:, name: curr_product[:name], previous_qtd: prev_qtd, current_qtd: curr_qtd, change: curr_qtd.to_i - prev_qtd.to_i } changes[:has_changes] = true end end # Check availability changes (store scraper) if curr_product.key?(:available) && prev_product.key?(:available) if curr_product[:available] != prev_product[:available] changes[:availability_changes] << { id:, name: curr_product[:name], previous: prev_product[:available], current: curr_product[:available] } changes[:has_changes] = true end end end end changes end |
#process(current_data:, previous_file: nil) ⇒ Object
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/ligamagic_scraper/alerts/alert_system.rb', line 24 def process(current_data:, previous_file: nil) return unless @config[:enabled] log_info("đ Alert system enabled") previous_data = load_previous_data(previous_file) if @config[:compare_previous] if previous_data log_info("đ Comparing with previous scrape...") changes = detect_changes(previous_data, current_data) if changes[:has_changes] log_info("⨠Changes detected!") notify_all(changes) else log_info("â No changes detected") end else log_info("âšī¸ No previous data to compare") end end |