Module: Feedjira::FeedEntryUtilities

Instance Method Summary collapse

Instance Method Details

#[](field) ⇒ Object



68
69
70
# File 'lib/feedjira/feed_entry_utilities.rb', line 68

def [](field)
  instance_variable_get(:"@#{field}")
end

#[]=(field, value) ⇒ Object



72
73
74
# File 'lib/feedjira/feed_entry_utilities.rb', line 72

def []=(field, value)
  instance_variable_set(:"@#{field}", value)
end

#eachObject



55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/feedjira/feed_entry_utilities.rb', line 55

def each
  @rss_fields ||= instance_variables.map do |ivar|
    ivar.to_s.sub("@", "")
  end.select do |field| # rubocop:disable Style/MultilineBlockChain
    # select callable (public) methods only
    respond_to?(field)
  end

  @rss_fields.each do |field|
    yield(field, instance_variable_get(:"@#{field}"))
  end
end

#idObject

Returns the id of the entry or its url if not id is present, as some formats don’t support it rubocop:disable Naming/MemoizedInstanceVariableName



23
24
25
# File 'lib/feedjira/feed_entry_utilities.rb', line 23

def id
  @entry_id ||= @url
end

#parse_datetime(string) ⇒ Object



11
12
13
14
15
16
17
# File 'lib/feedjira/feed_entry_utilities.rb', line 11

def parse_datetime(string)
  DateTime.parse(string).to_time.utc
rescue StandardError => e
  Feedjira.logger.debug("Failed to parse date #{string.inspect}")
  Feedjira.logger.debug(e)
  nil
end

#publishedObject Also known as: last_modified



7
8
9
# File 'lib/feedjira/feed_entry_utilities.rb', line 7

def published
  @published ||= @updated
end

#published=(val) ⇒ Object

Writer for published. By default, we keep the “oldest” publish time found.



30
31
32
33
# File 'lib/feedjira/feed_entry_utilities.rb', line 30

def published=(val)
  parsed = parse_datetime(val)
  @published = parsed if parsed && (!@published || parsed < @published)
end

#sanitize!Object



42
43
44
45
46
47
48
49
50
51
# File 'lib/feedjira/feed_entry_utilities.rb', line 42

def sanitize!
  %w[title author summary content image].each do |name|
    next unless respond_to?(name)

    current_value = send(name)
    if current_value.is_a?(String)
      send(:"#{name}=", Loofah.scrub_fragment(current_value, :prune).to_s)
    end
  end
end

#updated=(val) ⇒ Object

Writer for updated. By default, we keep the most recent update time found.



37
38
39
40
# File 'lib/feedjira/feed_entry_utilities.rb', line 37

def updated=(val)
  parsed = parse_datetime(val)
  @updated = parsed if parsed && (!@updated || parsed > @updated)
end