Class: Urss::Feed::Rss::Entry

Inherits:
Entry
  • Object
show all
Defined in:
lib/urss/feed/rss_entry.rb

Instance Attribute Summary

Attributes inherited from Entry

#author, #categories, #comments_url, #content, #created_at, #medias, #title, #url

Class Method Summary collapse

Methods inherited from Entry

#initialize

Constructor Details

This class inherits a constructor from Urss::Feed::Entry

Class Method Details

.build(nokogiri_instance, namespace = nil) ⇒ Object

~~~~ Class methods ~~~~



6
7
8
9
10
11
12
13
14
15
16
17
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
# File 'lib/urss/feed/rss_entry.rb', line 6

def self.build(nokogiri_instance, namespace=nil)
  raise Urss::NotANokogiriInstance unless nokogiri_instance.is_a?(Nokogiri::XML::Element)

  entry = self.new
  entry.title = nokogiri_instance.xpath("./#{namespace}title").text
  entry.url = nokogiri_instance.xpath("./#{namespace}link").text
  entry.comments_url = nokogiri_instance.xpath("./comments").text
  entry.created_at = nokogiri_instance.xpath("./pubDate").text
  if entry.created_at.nil? || entry.created_at.empty?
    entry.created_at = nokogiri_instance.xpath("./dc:date").text
  end
  entry.author = nokogiri_instance.xpath("./dc:creator", nokogiri_instance.namespaces).text
  entry.categories = nokogiri_instance.search("category").collect(&:text).join(", ")
  entry.content = nokogiri_instance.xpath("./description").text

  begin
    # When having only one media:content then all media:* nodes are used to create one Urss::Media
    # Otherwise each media:* are different Urss::Media
    single_media = nokogiri_instance.xpath("./media:content").size == 1
    media = nil
    nokogiri_instance.xpath("./media:*").each do |media_attributes|
      if single_media
        media = Urss::Media.new if media.nil?
        media.update(media_attributes)
      else
        media = Urss::Media.new
        media.update(media_attributes)
        media_attributes.children.select{|child| child.class == Nokogiri::XML::Element}.each do |element|
          media.update(element)
        end
        entry.medias << media
      end
    end
    entry.medias << media if single_media
  rescue Nokogiri::XML::XPath::SyntaxError
    # No media element
  end

  entry
end