Class: SimpleRSS::EntryNormalizer

Inherits:
Object
  • Object
show all
Defined in:
lib/simple-rss/entry_normalizer.rb

Constant Summary collapse

ATOM =
SimpleRSS::ATOM_NAMESPACE
CONTENT =
"http://purl.org/rss/1.0/modules/content/".freeze
DUBLIN_CORE =
"http://purl.org/dc/elements/1.1/".freeze
MEDIA =
"http://search.yahoo.com/mrss/".freeze
ITUNES =
"http://www.itunes.com/dtds/podcast-1.0.dtd".freeze
XHTML =
"http://www.w3.org/1999/xhtml".freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(element, raw, options) ⇒ EntryNormalizer

Returns a new instance of EntryNormalizer.



35
36
37
38
39
40
41
42
43
# File 'lib/simple-rss/entry_normalizer.rb', line 35

def initialize(element, raw, options)
  @element = element
  @children = element.children
  @source_url = options[:source_url] #: String?
  @mappings = options.fetch(:mappings) #: Hash[Symbol, untyped]
  @feed_authors = options.fetch(:feed_authors) #: Array[SimpleRSS::XmlElement]
  @values = { raw: raw, raw_xml: options[:raw_xml], field_sources: {}, issues: [] } #: Hash[Symbol, untyped]
  @field_elements = {} #: Hash[Symbol, SimpleRSS::XmlElement]
end

Class Method Details

.validate_mappings(mappings) ⇒ Object

Raises:

  • (ArgumentError)


14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/simple-rss/entry_normalizer.rb', line 14

def self.validate_mappings(mappings)
  allowed = %i[content_html content_text categories]
  raise ArgumentError, "mappings must be a Hash with keys #{allowed.join(", ")}" unless mappings.is_a?(Hash) && (mappings.keys - allowed).empty?

  %i[content_html content_text].each do |field|
    next unless mappings.key?(field)
    raise ArgumentError, "#{field} mapping must be a nonempty XML tag name" unless mappings[field].is_a?(String) && !mappings[field].strip.empty?
  end
  return unless mappings.key?(:categories)

  categories = mappings[:categories]
  raise ArgumentError, "categories mapping must be an array of tag/separator records" unless categories.is_a?(Array)

  categories.each do |mapping|
    valid = mapping.is_a?(Hash) && (mapping.keys - %i[tag separator]).empty? && mapping[:tag].is_a?(String) && !mapping[:tag].strip.empty?
    valid &&= !mapping.key?(:separator) || (mapping[:separator].is_a?(String) && !mapping[:separator].empty?)
    raise ArgumentError, "category mappings require a tag and an optional nonempty separator" unless valid
  end
end

Instance Method Details

#entryObject



46
47
48
49
50
51
52
53
54
55
# File 'lib/simple-rss/entry_normalizer.rb', line 46

def entry
  read_identity
  read_links
  read_dates
  read_content
  read_categories
  read_attachments
  read_authors
  SimpleRSS::NormalizedEntry.new(@values)
end