Class: SourceMonitor::Items::ItemCreator

Inherits:
Object
  • Object
show all
Defined in:
lib/source_monitor/items/item_creator.rb,
lib/source_monitor/items/item_creator/entry_parser.rb,
lib/source_monitor/items/item_creator/content_extractor.rb,
lib/source_monitor/items/item_creator/entry_parser/media_extraction.rb

Defined Under Namespace

Classes: ContentExtractor, EntryParser, Result

Constant Summary collapse

FINGERPRINT_SEPARATOR =
"\u0000".freeze
CONTENT_METHODS =
%i[content content_encoded summary].freeze
TIMESTAMP_METHODS =
%i[published updated].freeze
KEYWORD_SEPARATORS =
/[,;]+/.freeze
METADATA_ROOT_KEY =
"feedjira_entry".freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source:, entry:, existing_items_index: nil) ⇒ ItemCreator

Returns a new instance of ItemCreator.



46
47
48
49
50
# File 'lib/source_monitor/items/item_creator.rb', line 46

def initialize(source:, entry:, existing_items_index: nil)
  @source = source
  @entry = entry
  @existing_items_index = existing_items_index
end

Class Method Details

.call(source:, entry:, existing_items_index: nil) ⇒ Object

Process a single feed entry, creating or updating the corresponding item.

Parameters:

  • existing_items_index (Hash, nil) (defaults to: nil)

    Optional pre-fetched lookup of existing items keyed by guid and content_fingerprint. When provided, skips per-entry SELECT queries (used by BatchItemCreator).



42
43
44
# File 'lib/source_monitor/items/item_creator.rb', line 42

def self.call(source:, entry:, existing_items_index: nil)
  new(source:, entry:, existing_items_index: existing_items_index).call
end

Instance Method Details

#callObject



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/source_monitor/items/item_creator.rb', line 52

def call
  normalized_entry = build_normalized_entry
  attributes = normalized_entry.item_attributes

  existing_item, matched_by = existing_item_for(attributes, raw_guid_present: normalized_entry.raw_guid_present?)

  if existing_item
    apply_attributes(existing_item, attributes)
    instrument_duplicate(existing_item, matched_by)
    if significant_changes?(existing_item)
      existing_item.save!
      return Result.new(item: existing_item, status: :updated, matched_by: matched_by)
    else
      existing_item.reload if existing_item.changed?
      return Result.new(item: existing_item, status: :unchanged, matched_by: matched_by)
    end
  end

  create_new_item(attributes, raw_guid_present: normalized_entry.raw_guid_present?)
end