Class: SourceMonitor::Scraping::ItemScraper::Persistence

Inherits:
Object
  • Object
show all
Defined in:
lib/source_monitor/scraping/item_scraper/persistence.rb

Overview

Persists scrape outcomes to the database and builds a Result object.

Instance Method Summary collapse

Constructor Details

#initialize(item:, source:, adapter_name:) ⇒ Persistence

Returns a new instance of Persistence.



12
13
14
15
16
# File 'lib/source_monitor/scraping/item_scraper/persistence.rb', line 12

def initialize(item:, source:, adapter_name:)
  @item = item
  @source = source
  @adapter_name = adapter_name
end

Instance Method Details

#persist_failure(error:, started_at:, message_override: nil) ⇒ Object

Raises:

  • (ArgumentError)


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
# File 'lib/source_monitor/scraping/item_scraper/persistence.rb', line 51

def persist_failure(error:, started_at:, message_override: nil)
  raise ArgumentError, "Item does not belong to a source" unless source

  completed_at = Time.current
  message = message_override.presence || error.message.presence || "Scrape failed"
  http_status = extract_http_status(error)
   = (error)

  log = nil
  item.class.transaction do
    item.update!(scrape_status: "failed", scraped_at: completed_at)
    log = build_log(
      started_at:,
      completed_at: completed_at,
      duration_ms: duration_ms(started_at:, completed_at:),
      success: false,
      http_status: http_status,
      content_length: nil,
      metadata: ,
      error_class: error.class.name,
      error_message: message
    )
  end

  SourceMonitor::Scraping::ItemScraper::Result.new(
    status: :failed,
    item: item,
    log: log,
    message: "Scrape failed: #{message}",
    error: error
  )
end

#persist_success(adapter_result:, started_at:) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/source_monitor/scraping/item_scraper/persistence.rb', line 18

def persist_success(adapter_result:, started_at:)
  completed_at = Time.current
  success = adapter_result.status.to_s != "failed"
  status = normalize_status(adapter_result.status, success)
   = (adapter_result.)
  http_status = &.[](:http_status)
  error_info = success ? {} : extract_error_info()
  content_length = adapter_result.html.to_s.presence && adapter_result.html.to_s.bytesize

  log = nil
  item.class.transaction do
    apply_item_success(status:, success:, completed_at:, adapter_result:)
    log = build_log(
      started_at:,
      completed_at:,
      duration_ms: duration_ms(started_at:, completed_at:),
      success: success,
      http_status: http_status,
      content_length: content_length,
      metadata: ,
      error_class: error_info[:class],
      error_message: error_info[:message]
    )
  end

  SourceMonitor::Scraping::ItemScraper::Result.new(
    status: status,
    item: item,
    log: log,
    message: message_for(status, success, error_info[:message], )
  )
end