Class: FeedTools::Feed

Inherits:
Object
  • Object
show all
Defined in:
lib/feed_tools/feed.rb

Overview

The FeedTools::Feed class represents a web feed’s structure.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeFeed

Initialize the feed object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/feed_tools/feed.rb', line 36

def initialize
  super
  @cache_object = nil
  @http_headers = nil
  @xml_document = nil
  @feed_data = nil
  @feed_data_type = :xml
  @root_node = nil
  @channel_node = nil
  @href = nil
  @id = nil
  @title = nil
  @subtitle = nil
  @link = nil
  @last_retrieved = nil
  @time_to_live = nil
  @entries = nil
  @live = false
  @encoding = nil
  @options = nil
  @version = FeedTools::FEED_TOOLS_VERSION::STRING
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(msg, *params) ⇒ Object

passes missing methods to the cache_object



2876
2877
2878
2879
2880
2881
# File 'lib/feed_tools/feed.rb', line 2876

def method_missing(msg, *params)
  if self.cache_object.nil?
    raise NoMethodError, "Invalid method #{msg.to_s}"
  end
  return self.cache_object.send(msg, params)
end

Class Method Details

.method_missing(msg, *params) ⇒ Object

passes missing methods to the FeedTools.feed_cache



2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
# File 'lib/feed_tools/feed.rb', line 2884

def Feed.method_missing(msg, *params)
  if FeedTools.feed_cache.nil?
    raise NoMethodError, "Invalid method Feed.#{msg.to_s}"
  end
  result = FeedTools.feed_cache.send(msg, params)
  if result.kind_of? FeedTools.feed_cache
    result = Feed.open(result.url)
  end
  return result
end

.open(href, options = {}) ⇒ Object

Loads the feed specified by the url, pulling the data from the cache if it hasn’t expired. Options supplied will override the default options.



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/feed_tools/feed.rb', line 97

def Feed.open(href, options={})
  FeedTools::GenericHelper.validate_options(
    FeedTools.configurations.keys, options.keys)

  # clean up the url
  href = FeedTools::UriHelper.normalize_url(href)

  feed_configurations = FeedTools.configurations.merge(options)
  cache_object = nil
  deserialized_feed = nil
  
  if feed_configurations[:feed_cache] != nil && FeedTools.feed_cache.nil?
    raise(ArgumentError, "There is currently no caching mechanism set. " +
      "Cannot retrieve cached feeds.")
  elsif feed_configurations[:serialization_enabled] == true
    # We've got a caching mechanism available
    cache_object = FeedTools.feed_cache.find_by_href(href)
    begin
      if cache_object != nil && cache_object.serialized != nil
        # If we've got a cache hit, deserialize
        expired = true
        if cache_object.time_to_live == nil
          cache_object.time_to_live =
            feed_configurations[:default_ttl].to_i
          cache_object.save
        end
        if (cache_object.last_retrieved == nil)
          expired = true
        elsif (cache_object.time_to_live < 30.minutes)
          expired =
            (cache_object.last_retrieved + 30.minutes) < Time.now.gmtime
        else
          expired =
            (cache_object.last_retrieved + cache_object.time_to_live) <
              Time.now.gmtime
        end
        if !expired
          require 'yaml'
          deserialized_feed = YAML.load(cache_object.serialized)
          deserialized_feed.cache_object = cache_object
          Thread.pass
        end
      end
    rescue Exception
    end
  end
  
  if deserialized_feed == nil
    # create the new feed
    feed = FeedTools::Feed.new

    feed.configurations = feed_configurations

    # load the new feed
    feed.href = href
    if cache_object != nil
      feed.cache_object = cache_object
    end
    feed.update! unless feed.configurations[:disable_update_from_remote]
    Thread.pass
  
    return feed
  else
    return deserialized_feed
  end
end

Instance Method Details

#<<(new_entry) ⇒ Object

Syntactic sugar for appending feed items to a feed.



2471
2472
2473
2474
2475
2476
2477
2478
# File 'lib/feed_tools/feed.rb', line 2471

def <<(new_entry)
  @entries ||= []
  unless new_entry.kind_of? FeedTools::FeedItem
    raise ArgumentError,
      "You should only add FeedItem objects to the entries array."
  end
  @entries << new_entry
end

#<=>(other_feed) ⇒ Object

Allows sorting feeds by title



2901
2902
2903
# File 'lib/feed_tools/feed.rb', line 2901

def <=>(other_feed)
  return self.title.to_s <=> other_feed.title.to_s
end

#authorObject

Returns the feed author



1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
# File 'lib/feed_tools/feed.rb', line 1608

def author
  if @author.nil?
    @author = FeedTools::Author.new
    author_node = FeedTools::XmlHelper.try_xpaths(self.channel_node, [
      "atom10:author",
      "atom03:author",
      "atom:author",
      "author",
      "managingEditor",
      "dc:author",
      "dc:creator"
    ])
    unless author_node.nil?
      @author.raw = FeedTools::XmlHelper.try_xpaths(
        author_node, ["text()"], :select_result_value => true)
      @author.raw = FeedTools::HtmlHelper.unescape_entities(@author.raw)
      unless @author.raw.nil?
        raw_scan = @author.raw.scan(
          /(.*)\((\b[A-Z0-9._%-\+]+@[A-Z0-9._%-]+\.[A-Z]{2,4}\b)\)/i)
        if raw_scan.nil? || raw_scan.size == 0
          raw_scan = @author.raw.scan(
            /(\b[A-Z0-9._%-\+]+@[A-Z0-9._%-]+\.[A-Z]{2,4}\b)\s*\((.*)\)/i)
          unless raw_scan.size == 0
            author_raw_pair = raw_scan.first.reverse
          end
        else
          author_raw_pair = raw_scan.first
        end
        if raw_scan.nil? || raw_scan.size == 0
          email_scan = @author.raw.scan(
            /\b[A-Z0-9._%-\+]+@[A-Z0-9._%-]+\.[A-Z]{2,4}\b/i)
          if email_scan != nil && email_scan.size > 0
            @author.email = email_scan.first.strip
          end
        end
        unless author_raw_pair.nil? || author_raw_pair.size == 0
          @author.name = author_raw_pair.first.strip
          @author.email = author_raw_pair.last.strip
        else
          unless @author.raw.include?("@")
            # We can be reasonably sure we are looking at something
            # that the creator didn't intend to contain an email address
            # if it got through the preceeding regexes and it doesn't
            # contain the tell-tale '@' symbol.
            @author.name = @author.raw
          end
        end
      end
      if @author.name.blank?
        @author.name = FeedTools::HtmlHelper.unescape_entities(
          FeedTools::XmlHelper.try_xpaths(author_node, [
            "atom10:name/text()",
            "atom03:name/text()",
            "atom:name/text()",
            "name/text()",
            "@name"
          ], :select_result_value => true)
        )
      end
      if @author.email.blank?
        @author.email = FeedTools::HtmlHelper.unescape_entities(
          FeedTools::XmlHelper.try_xpaths(author_node, [
            "atom10:email/text()",
            "atom03:email/text()",
            "atom:email/text()",
            "email/text()",
            "@email"
          ], :select_result_value => true)
        )
      end
      if @author.url.blank?
        @author.url = FeedTools::HtmlHelper.unescape_entities(
          FeedTools::XmlHelper.try_xpaths(author_node, [
            "atom10:url/text()",
            "atom03:url/text()",
            "atom:url/text()",
            "url/text()",
            "atom10:uri/text()",
            "atom03:uri/text()",
            "atom:uri/text()",
            "uri/text()",
            "@href",
            "@uri",
            "@href"
          ], :select_result_value => true)
        )
      end
      if @author.name.blank? && !@author.raw.blank? &&
          !@author.email.blank?
        name_scan = @author.raw.scan(
          /"?([^"]*)"? ?[\(<].*#{@author.email}.*[\)>].*/)
        if name_scan.flatten.size == 1
          @author.name = name_scan.flatten[0].strip
        end
        if @author.name.blank?
          name_scan = @author.raw.scan(
            /.*#{@author.email} ?[\(<]"?([^"]*)"?[\)>].*/)
          if name_scan.flatten.size == 1
            @author.name = name_scan.flatten[0].strip
          end
        end
      end
      @author.name = nil if @author.name.blank?
      @author.raw = nil if @author.raw.blank?
      @author.email = nil if @author.email.blank?
      @author.url = nil if @author.url.blank?
      if @author.url != nil
        begin
          if !(@author.url =~ /^file:/) &&
              !FeedTools::UriHelper.is_uri?(@author.url)
            @author.url = FeedTools::UriHelper.resolve_relative_uri(
              @author.url, [author_node.base_uri, self.base_uri])
          end
        rescue
        end
      end
      if FeedTools::XmlHelper.try_xpaths(author_node,
          ["@gr:unknown-author"], :select_result_value => true) == "true"
        if @author.name == "(author unknown)"
          @author.name = nil
        end
      end
    end
    # Fallback on the itunes module if we didn't find an author name
    begin
      @author.name = self.itunes_author if @author.name.nil?
    rescue
      @author.name = nil
    end
  end
  return @author
end

#author=(new_author) ⇒ Object

Sets the feed author



1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
# File 'lib/feed_tools/feed.rb', line 1742

def author=(new_author)
  if new_author.respond_to?(:name) &&
      new_author.respond_to?(:email) &&
      new_author.respond_to?(:url)
    # It's a complete author object, just set it.
    @author = new_author
  else
    # We're not looking at an author object, this is probably a string,
    # default to setting the author's name.
    if @author.nil?
      @author = FeedTools::Author.new
    end
    @author.name = new_author
  end
end

#base_uriObject

Returns the base uri for the feed, used for resolving relative paths



1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
# File 'lib/feed_tools/feed.rb', line 1487

def base_uri
  if @base_uri.nil?
    @base_uri = FeedTools::XmlHelper.try_xpaths(self.channel_node, [
      "@base",
      "base/@href",
      "base/text()",
      "@xml:base"
    ], :select_result_value => true)
    if @base_uri.blank?
      begin
        @base_uri =
          FeedTools::GenericHelper.recursion_trap(:feed_base_uri) do
            self.href
          end
      rescue Exception
      end
    end
    if @base_uri.blank?
      @base_uri = FeedTools::XmlHelper.try_xpaths(self.root_node, [
        "@xml:base"
      ], :select_result_value => true)
    end
    if !@base_uri.blank?
      @base_uri = FeedTools::UriHelper.normalize_url(@base_uri)
    end
    if !@base_uri.blank?
      parsed_uri = FeedTools::URI.parse(@base_uri)
      # Feedburner is almost never the base uri that was intended
      # Use the actual site instead
      if parsed_uri.host =~ /feedburner/
        site_uri =
          FeedTools::GenericHelper.recursion_trap(:feed_base_uri) do
            FeedTools::UriHelper.normalize_url(self.link)
          end
        @base_uri = site_uri if !site_uri.blank?
      end
    end
  end
  return @base_uri
end

#base_uri=(new_base_uri) ⇒ Object

Sets the base uri for the feed



1529
1530
1531
# File 'lib/feed_tools/feed.rb', line 1529

def base_uri=(new_base_uri)
  @base_uri = new_base_uri
end

#build_xml(feed_type = (self.feed_type or "atom"), feed_version = nil, xml_builder = Builder::XmlMarkup.new( :indent => 2, :escape_attrs => false)) ⇒ Object

Generates xml based on the content of the feed



2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
# File 'lib/feed_tools/feed.rb', line 2550

def build_xml(feed_type=(self.feed_type or "atom"), feed_version=nil,
    xml_builder=Builder::XmlMarkup.new(
      :indent => 2, :escape_attrs => false))
  
  if self.find_node("access:restriction/@relationship").to_s == "deny"
    raise StandardError,
      "Operation not permitted.  This feed denies redistribution."
  elsif self.find_node("@indexing:index").to_s == "no"
    raise StandardError,
      "Operation not permitted.  This feed denies redistribution."
  end
  
  self.full_parse()
  
  xml_builder.instruct! :xml, :version => "1.0",
    :encoding => (self.configurations[:output_encoding] or "utf-8")
  if feed_type.nil?
    feed_type = self.feed_type
  end
  if feed_version.nil?
    feed_version = self.feed_version
  end
  if feed_type == "rss" &&
      (feed_version == nil || feed_version <= 0.0)
    feed_version = 1.0
  elsif feed_type == "atom" &&
      (feed_version == nil || feed_version <= 0.0)
    feed_version = 1.0
  end
  if feed_type == "rss" &&
      (feed_version == 0.9 || feed_version == 1.0 || feed_version == 1.1)
    # RDF-based rss format
    return xml_builder.tag!("rdf:RDF",
        "xmlns" => FEED_TOOLS_NAMESPACES['rss10'],
        "xmlns:content" => FEED_TOOLS_NAMESPACES['content'],
        "xmlns:rdf" => FEED_TOOLS_NAMESPACES['rdf'],
        "xmlns:dc" => FEED_TOOLS_NAMESPACES['dc'],
        "xmlns:syn" => FEED_TOOLS_NAMESPACES['syn'],
        "xmlns:admin" => FEED_TOOLS_NAMESPACES['admin'],
        "xmlns:taxo" => FEED_TOOLS_NAMESPACES['taxo'],
        "xmlns:itunes" => FEED_TOOLS_NAMESPACES['itunes'],
        "xmlns:media" => FEED_TOOLS_NAMESPACES['media']) do
      channel_attributes = {}
      unless self.link.nil?
        channel_attributes["rdf:about"] =
          FeedTools::HtmlHelper.escape_entities(self.link)
      end
      xml_builder.channel(channel_attributes) do
        unless self.title.blank?
          xml_builder.title(
            FeedTools::HtmlHelper.strip_html_tags(self.title))
        else
          xml_builder.title
        end
        unless self.link.blank?
          xml_builder.link(self.link)
        else
          xml_builder.link
        end
        unless images.blank?
          xml_builder.image("rdf:resource" =>
            FeedTools::HtmlHelper.escape_entities(
              images.first.url))
        end
        unless description.nil? || description == ""
          xml_builder.description(description)
        else
          xml_builder.description
        end
        unless self.language.blank?
          xml_builder.tag!("dc:language", self.language)
        end
        unless self.rights.blank?
          xml_builder.tag!("dc:rights", self.rights)
        end
        xml_builder.tag!("syn:updatePeriod", "hourly")
        xml_builder.tag!("syn:updateFrequency",
          (self.time_to_live / 1.hour).to_s)
        xml_builder.tag!("syn:updateBase", Time.mktime(1970).iso8601)
        xml_builder.items do
          xml_builder.tag!("rdf:Seq") do
            unless items.nil?
              for item in items
                if item.link.nil?
                  raise "Cannot generate an rdf-based feed with a nil " +
                    "item link field."
                end
                xml_builder.tag!("rdf:li", "rdf:resource" =>
                  FeedTools::HtmlHelper.escape_entities(item.link))
              end
            end
          end
        end
        xml_builder.tag!(
          "admin:generatorAgent",
          "rdf:resource" => self.configurations[:generator_href])
        build_xml_hook(feed_type, feed_version, xml_builder)
      end
      unless self.images.blank?
        best_image = nil
        for image in self.images
          if image.link != nil
            best_image = image
            break
          end
        end
        best_image = self.images.first if best_image.nil?
        xml_builder.image("rdf:about" =>
            FeedTools::HtmlHelper.escape_entities(best_image.url)) do
          if !best_image.title.blank?
            xml_builder.title(best_image.title)
          elsif !self.title.blank?
            xml_builder.title(self.title)
          else
            xml_builder.title
          end
          unless best_image.url.blank?
            xml_builder.url(best_image.url)
          end
          if !best_image.link.blank?
            xml_builder.link(best_image.link)
          elsif !self.link.blank?
            xml_builder.link(self.link)
          else
            xml_builder.link
          end
        end
      end
      unless items.nil?
        for item in items
          item.build_xml(feed_type, feed_version, xml_builder)
        end
      end
    end
  elsif feed_type == "rss"
    # normal rss format
    return xml_builder.rss("version" => "2.0",
        "xmlns:content" => FEED_TOOLS_NAMESPACES['content'],
        "xmlns:rdf" => FEED_TOOLS_NAMESPACES['rdf'],
        "xmlns:dc" => FEED_TOOLS_NAMESPACES['dc'],
        "xmlns:taxo" => FEED_TOOLS_NAMESPACES['taxo'],
        "xmlns:trackback" => FEED_TOOLS_NAMESPACES['trackback'],
        "xmlns:itunes" => FEED_TOOLS_NAMESPACES['itunes'],
        "xmlns:media" => FEED_TOOLS_NAMESPACES['media']) do
      xml_builder.channel do
        unless self.title.blank?
          xml_builder.title(
            FeedTools::HtmlHelper.strip_html_tags(self.title))
        end
        unless self.link.blank?
          xml_builder.link(link)
        end
        unless self.description.blank?
          xml_builder.description(description)
        else
          xml_builder.description
        end
        unless self.author.email.blank?
          xml_builder.managingEditor(self.author.email)
        end
        unless self.publisher.email.blank?
          xml_builder.webMaster(self.publisher.email)
        end
        unless self.published.blank?
          xml_builder.pubDate(self.published.rfc822)
        end
        unless self.updated.blank?
          xml_builder.lastBuildDate(self.updated.rfc822)
        end
        unless self.copyright.blank?
          xml_builder.copyright(self.copyright)
        end
        unless self.language.blank?
          xml_builder.language(self.language)
        end
        xml_builder.ttl((time_to_live / 1.minute).to_s)
        xml_builder.generator(
          self.configurations[:generator_href])
        build_xml_hook(feed_type, feed_version, xml_builder)
        unless items.nil?
          for item in items
            item.build_xml(feed_type, feed_version, xml_builder)
          end
        end
      end
    end
  elsif feed_type == "atom" && feed_version == 0.3
    raise "Atom 0.3 is obsolete."
  elsif feed_type == "atom" && feed_version == 1.0
    # normal atom format
    return xml_builder.feed("xmlns" => FEED_TOOLS_NAMESPACES['atom10'],
        "xml:lang" => language) do
      unless title.blank?
        xml_builder.title(title,
            "type" => "html")
      end
      xml_builder.author do
        unless self.author.nil? || self.author.name.nil?
          xml_builder.name(self.author.name)
        else
          xml_builder.name("n/a")
        end
        unless self.author.nil? || self.author.email.nil?
          xml_builder.email(self.author.email)
        end
        unless self.author.nil? || self.author.url.nil?
          xml_builder.uri(self.author.url)
        end
      end
      unless self.href.blank?
        xml_builder.link("href" => self.href,
            "rel" => "self",
            "type" => "application/atom+xml")
      end
      unless self.link.blank?
        xml_builder.link(
          "href" =>
            FeedTools::HtmlHelper.escape_entities(self.link),
          "rel" => "alternate")
      end
      unless self.subtitle.blank?
        xml_builder.subtitle(self.subtitle,
            "type" => "html")
      end
      if self.updated != nil
        xml_builder.updated(self.updated.iso8601)
      elsif self.time != nil
        # Not technically correct, but a heck of a lot better
        # than the Time.now fall-back.
        xml_builder.updated(self.time.iso8601)
      else
        xml_builder.updated(Time.now.gmtime.iso8601)
      end
      unless self.rights.blank?
        xml_builder.rights(self.rights)
      end
      xml_builder.generator(self.configurations[:generator_name] +
        " - " + self.configurations[:generator_href])
      if self.id != nil
        unless FeedTools::UriHelper.is_uri? self.id
          if self.link != nil
            xml_builder.id(FeedTools::UriHelper.build_urn_uri(self.link))
          else
            raise "The unique id must be a valid URI."
          end
        else
          xml_builder.id(self.id)
        end
      elsif self.link != nil
        xml_builder.id(FeedTools::UriHelper.build_urn_uri(self.link))
      elsif self.url != nil
        xml_builder.id(FeedTools::UriHelper.build_urn_uri(self.url))
      else
        raise "Cannot build feed, missing feed unique id."
      end
      build_xml_hook(feed_type, feed_version, xml_builder)
      unless items.nil?
        for item in items
          item.build_xml(feed_type, feed_version, xml_builder)
        end
      end
    end
  else
    raise "Unsupported feed format/version."
  end
end

#build_xml_hook(feed_type, version, xml_builder) ⇒ Object

A hook method that is called during the feed generation process. Overriding this method will enable additional content to be inserted into the feed.



2545
2546
2547
# File 'lib/feed_tools/feed.rb', line 2545

def build_xml_hook(feed_type, version, xml_builder)
  return nil
end

#cache_objectObject

The cache object that handles the feed persistence.



787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
# File 'lib/feed_tools/feed.rb', line 787

def cache_object
  if !@href.nil? && @href =~ /^file:\/\//
    return nil
  end
  unless FeedTools.feed_cache.nil?
    if @cache_object.nil?
      begin
        if @href != nil
          begin
            @cache_object = FeedTools.feed_cache.find_by_href(@href)
          rescue RuntimeError => error
            if error.message =~ /sorry, too many clients already/
              warn("There are too many connections to the database open.")
              raise error
            else
              raise error
            end
          rescue => error
            warn("The feed cache seems to be having trouble with the " +
              "find_by_href method.  This may cause unexpected results.")
            raise error
          end
        end
        if @cache_object.nil?
          @cache_object = FeedTools.feed_cache.new
        end
      rescue
      end      
    end
  end
  return @cache_object
end

#cache_object=(new_cache_object) ⇒ Object

Sets the cache object for this feed.

This can be any object, but it must accept the following messages: href href= title title= link link= feed_data feed_data= feed_data_type feed_data_type= etag etag= last_modified last_modified= save



838
839
840
# File 'lib/feed_tools/feed.rb', line 838

def cache_object=(new_cache_object)
  @cache_object = new_cache_object
end

#categoriesObject

Returns a list of the feed’s categories



1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
# File 'lib/feed_tools/feed.rb', line 1965

def categories
  if @categories.nil?
    @categories = []
    category_nodes =
      FeedTools::XmlHelper.try_xpaths_all(self.channel_node, [
        "category",
        "dc:subject"
      ])
    unless category_nodes.nil?
      for category_node in category_nodes
        category = FeedTools::Category.new
        category.term = FeedTools::XmlHelper.try_xpaths(category_node, [
          "@term",
          "text()"
        ], :select_result_value => true)
        category.term.strip! unless category.term.blank?
        category.label = FeedTools::XmlHelper.try_xpaths(
          category_node, ["@label"],
          :select_result_value => true)
        category.label.strip! unless category.label.blank?
        category.scheme = FeedTools::XmlHelper.try_xpaths(category_node, [
          "@scheme",
          "@domain"
        ], :select_result_value => true)
        category.scheme.strip! unless category.scheme.blank?
        @categories << category
      end
    end
  end
  return @categories
end

#channel_nodeObject

Returns the channel node of the feed.



771
772
773
774
775
776
777
778
779
780
781
782
783
784
# File 'lib/feed_tools/feed.rb', line 771

def channel_node
  if @channel_node.nil? && self.root_node != nil
    @channel_node = FeedTools::XmlHelper.try_xpaths(self.root_node, [
      "channel",
      "CHANNEL",
      "feedinfo",
      "news"
    ])
    if @channel_node == nil
      @channel_node = self.root_node
    end
  end
  return @channel_node
end

#cloudObject

Returns the feed’s cloud



2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
# File 'lib/feed_tools/feed.rb', line 2223

def cloud
  if @cloud.nil?
    @cloud = FeedTools::Cloud.new
    @cloud.domain = FeedTools::XmlHelper.try_xpaths(
      self.channel_node, ["cloud/@domain"],
      :select_result_value => true)
    @cloud.port = FeedTools::XmlHelper.try_xpaths(
      self.channel_node, ["cloud/@port"],
      :select_result_value => true)
    @cloud.path = FeedTools::XmlHelper.try_xpaths(
      self.channel_node, ["cloud/@path"],
      :select_result_value => true)
    @cloud.register_procedure =
      FeedTools::XmlHelper.try_xpaths(
        self.channel_node, ["cloud/@registerProcedure"],
        :select_result_value => true)
    @cloud.protocol =
      FeedTools::XmlHelper.try_xpaths(
        self.channel_node, ["cloud/@protocol"],
        :select_result_value => true)
    @cloud.protocol.downcase unless @cloud.protocol.nil?
    @cloud.port = @cloud.port.to_s.to_i
    @cloud.port = nil if @cloud.port == 0
  end
  return @cloud
end

#cloud=(new_cloud) ⇒ Object

Sets the feed’s cloud



2251
2252
2253
# File 'lib/feed_tools/feed.rb', line 2251

def cloud=(new_cloud)
  @cloud = new_cloud
end

#configurationsObject

Returns the load options for this feed.



165
166
167
168
169
170
# File 'lib/feed_tools/feed.rb', line 165

def configurations
  if @configurations.blank?
    @configurations = FeedTools.configurations.dup
  end
  return @configurations
end

#configurations=(new_configurations) ⇒ Object

Sets the load options for this feed.



173
174
175
# File 'lib/feed_tools/feed.rb', line 173

def configurations=(new_configurations)
  @configurations = new_configurations
end

#disposeObject

Breaks any references that the feed may be keeping around, thus making the job of the garbage collector much, much easier. Call this method prior to feeds going out of scope to prevent memory leaks.



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/feed_tools/feed.rb', line 62

def dispose()
  self.entries.each do |entry|
    entry.instance_variable_set("@root_node", nil)
    entry.instance_variable_set("@feed", nil)
    entry.instance_variable_set("@parent_feed", nil)
    entry.dispose if entry.respond_to?(:dispose)
  end
  self.entries = []
  
  @cache_object = nil
  @http_headers = nil
  @xml_document = nil
  @feed_data = nil
  @feed_data_type = nil
  @root_node = nil
  @channel_node = nil
  @href = nil
  @id = nil
  @title = nil
  @subtitle = nil
  @link = nil
  @last_retrieved = nil
  @time_to_live = nil
  @entries = nil
  @live = false
  @encoding = nil
  @options = nil

  GC.start()
  self
end

#docsObject

Returns the feed docs



2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
# File 'lib/feed_tools/feed.rb', line 2308

def docs
  if @docs.nil?
    @docs = FeedTools::XmlHelper.try_xpaths(
      self.channel_node, ["docs/text()"],
      :select_result_value => true)
    begin
      if !(@docs =~ /^file:/) &&
          !FeedTools::UriHelper.is_uri?(@docs)
        channel_base_uri = nil
        unless self.channel_node.nil?
          channel_base_uri = self.channel_node.base_uri
        end
        @docs = FeedTools::UriHelper.resolve_relative_uri(
          @docs, [channel_base_uri, self.base_uri])
      end
    rescue
    end
    if self.configurations[:url_normalization_enabled]
      @docs = FeedTools::UriHelper.normalize_url(@docs)
    end
  end
  return @docs
end

#docs=(new_docs) ⇒ Object

Sets the feed docs



2333
2334
2335
# File 'lib/feed_tools/feed.rb', line 2333

def docs=(new_docs)
  @docs = new_docs
end

#encodingObject

Returns the encoding that the feed was parsed with



547
548
549
550
551
552
553
554
555
556
557
558
559
560
# File 'lib/feed_tools/feed.rb', line 547

def encoding
  if @encoding.blank?
    if !self.http_headers.blank?
      if self.http_headers['content-type'] =~ /charset=([\w\d-]+)/
        @encoding = $1.downcase
      else
        @encoding = self.encoding_from_feed_data
      end          
    else
      @encoding = self.encoding_from_feed_data
    end
  end
  return @encoding
end

#encoding_from_feed_dataObject

Returns the encoding of feed calculated only from the xml data. I.e., the encoding we would come up with if we ignore RFC 3023.



564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
# File 'lib/feed_tools/feed.rb', line 564

def encoding_from_feed_data
  if @encoding_from_feed_data.blank?
    raw_data = self.feed_data
    return nil if raw_data.nil?
    encoding_from_xml_instruct = 
      raw_data.scan(
        /^<\?xml [^>]*encoding="([^\"]*)"[^>]*\?>/
      ).flatten.first
    unless encoding_from_xml_instruct.blank?
      encoding_from_xml_instruct.downcase!
    end
    if encoding_from_xml_instruct.blank?
      doc = REXML::Document.new(raw_data)
      encoding_from_xml_instruct = doc.encoding.downcase
      if encoding_from_xml_instruct == "utf-8"
        # REXML has a tendency to report utf-8 overzealously, take with
        # grain of salt
        encoding_from_xml_instruct = nil
      end
    else
      @encoding_from_feed_data = encoding_from_xml_instruct
    end
    if encoding_from_xml_instruct.blank?
      sniff_table = {
        "Lo\247\224" => "ebcdic-cp-us",
        "<?xm" => "utf-8"
      }
      sniff = self.feed_data[0..3]
      if sniff_table[sniff] != nil
        @encoding_from_feed_data = sniff_table[sniff].downcase
      end
    else
      @encoding_from_feed_data = encoding_from_xml_instruct
    end
    if @encoding_from_feed_data.blank?
      # Safest assumption
      @encoding_from_feed_data = "utf-8"
    end
  end
  return @encoding_from_feed_data
end

#entriesObject Also known as: items

Returns the feed entries



2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
# File 'lib/feed_tools/feed.rb', line 2394

def entries
  if @entries.nil?
    raw_entries = FeedTools::XmlHelper.select_not_blank([
      FeedTools::XmlHelper.try_xpaths_all(self.channel_node, [
        "atom10:entry",
        "atom03:entry",
        "atom:entry",
        "entry"
      ]),
      FeedTools::XmlHelper.try_xpaths_all(self.root_node, [
        "rss10:item",
        "rss11:items/rss11:item",
        "rss11:items/item",
        "items/rss11:item",
        "items/item",
        "item",
        "atom10:entry",
        "atom03:entry",
        "atom:entry",
        "entry",
        "story"
      ]),
      FeedTools::XmlHelper.try_xpaths_all(self.channel_node, [
        "rss10:item",
        "rss11:items/rss11:item",
        "rss11:items/item",
        "items/rss11:item",
        "items/item",
        "item",
        "story"
      ])
    ])

    # create the individual feed items
    @entries = []
    unless raw_entries.blank?
      for entry_node in raw_entries.reverse
        new_entry = FeedItem.new
        new_entry.feed_data = entry_node.to_s
        new_entry.feed_data_type = self.feed_data_type
        new_entry.root_node = entry_node
        if new_entry.root_node.namespace.blank?
          new_entry.root_node.add_namespace(self.root_node.namespace)
        end
        @entries << new_entry
      end
    end
  end

  # Sort the items
  if self.configurations[:entry_sorting_property] == "time"
    @entries = @entries.sort do |a, b|
      (b.time or Time.utc(1970)) <=> (a.time or Time.utc(1970))
    end
  elsif self.configurations[:entry_sorting_property] != nil
    sorting_property = self.configurations[:entry_sorting_property]
    @entries = @entries.sort do |a, b|
      eval("a.#{sorting_property}") <=> eval("b.#{sorting_property}")
    end
  else
    return @entries.reverse
  end
  return @entries
end

#entries=(new_entries) ⇒ Object Also known as: items=

Sets the entries array to a new array.



2460
2461
2462
2463
2464
2465
2466
2467
2468
# File 'lib/feed_tools/feed.rb', line 2460

def entries=(new_entries)
  for entry in new_entries
    unless entry.kind_of? FeedTools::FeedItem
      raise ArgumentError,
        "You should only add FeedItem objects to the entries array."
    end
  end
  @entries = new_entries
end

#expire!Object

Forces this feed to expire.



2537
2538
2539
2540
# File 'lib/feed_tools/feed.rb', line 2537

def expire!
  self.last_retrieved = Time.mktime(1970).gmtime
  self.save
end

#expired?Boolean

True if the feed has expired and must be reacquired from the remote server.

Returns:

  • (Boolean)


2526
2527
2528
2529
2530
2531
2532
2533
2534
# File 'lib/feed_tools/feed.rb', line 2526

def expired?
  if (self.last_retrieved == nil)
    return true
  elsif (self.time_to_live < 30.minutes)
    return (self.last_retrieved + 30.minutes) < Time.now.gmtime
  else
    return (self.last_retrieved + self.time_to_live) < Time.now.gmtime
  end
end

#explicit=(new_explicit) ⇒ Object

Sets whether or not the feed contains explicit material



2389
2390
2391
# File 'lib/feed_tools/feed.rb', line 2389

def explicit=(new_explicit)
  @explicit = (new_explicit ? true : false)
end

#explicit?Boolean

Returns true if this feed contains explicit material.

Returns:

  • (Boolean)


2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
# File 'lib/feed_tools/feed.rb', line 2373

def explicit?
  if @explicit.nil?
    explicit_string = FeedTools::XmlHelper.try_xpaths(self.channel_node, [
      "media:adult/text()",
      "itunes:explicit/text()"
    ], :select_result_value => true)
    if explicit_string == "true" || explicit_string == "yes"
      @explicit = true
    else
      @explicit = false
    end
  end
  return @explicit
end

#faviconObject

Returns the favicon url for this feed. This method first tries to use the url from the link field instead of the feed url, in order to avoid grabbing the favicon for services like feedburner.



1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
# File 'lib/feed_tools/feed.rb', line 1575

def favicon
  if @favicon.nil?
    if !self.link.blank?
      begin
        link_uri = URI.parse(
          FeedTools::UriHelper.normalize_url(self.link))
        if link_uri.scheme == "http"
          @favicon =
            "http://" + link_uri.host + "/favicon.ico"
        end
      rescue
        @favicon = nil
      end
      if @favicon.nil? && !self.href.blank?
        begin
          feed_uri = URI.parse(
            FeedTools::UriHelper.normalize_url(self.href))
          if feed_uri.scheme == "http"
            @favicon =
              "http://" + feed_uri.host + "/favicon.ico"
          end
        rescue
          @favicon = nil
        end
      end
    else
      @favicon = nil
    end
  end
  return @favicon
end

#feed_dataObject

Returns the feed’s raw data.



607
608
609
610
611
612
613
614
# File 'lib/feed_tools/feed.rb', line 607

def feed_data
  if @feed_data.nil?
    unless self.cache_object.nil?
      @feed_data = self.cache_object.feed_data
    end
  end
  return @feed_data
end

#feed_data=(new_feed_data) ⇒ Object

Sets the feed’s data.



617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
# File 'lib/feed_tools/feed.rb', line 617

def feed_data=(new_feed_data)
  for var in self.instance_variables
    self.instance_variable_set(var, nil)
  end
  @http_headers = {}
  @feed_data = new_feed_data
  unless self.cache_object.nil?
    self.cache_object.feed_data = new_feed_data
  end
  ugly_redirect = FeedTools::XmlHelper.try_xpaths(self.xml_document, [
    "redirect/newLocation/text()"
  ], :select_result_value => true)
  if !ugly_redirect.blank?
    for var in self.instance_variables
      self.instance_variable_set(var, nil)
    end
    @http_headers = {}
    @feed_data = nil
    self.href = ugly_redirect
    if FeedTools.feed_cache.nil?
      self.cache_object = nil
    else
      begin
        self.cache_object =
          FeedTools.feed_cache.find_by_href(ugly_redirect)
      rescue RuntimeError => error
        if error.message =~ /sorry, too many clients already/
          warn("There are too many connections to the database open.")
        end
        raise error
      end
    end
    self.update!
  end
  
  # Get these things parsed in the correct order to avoid the retardedly
  # painful corecursion issues.
  self.href
  @links = nil
  @link = nil
  self.links
  self.link
end

#feed_data_typeObject

Returns the data type of the feed Possible values:

  • :xml

  • :yaml

  • :text



689
690
691
692
693
694
695
# File 'lib/feed_tools/feed.rb', line 689

def feed_data_type
  if @feed_data_type.nil?
    # Right now, nothing else is supported
    @feed_data_type = :xml
  end
  return @feed_data_type
end

#feed_data_type=(new_feed_data_type) ⇒ Object

Sets the feed’s data type.



698
699
700
701
702
703
704
705
706
# File 'lib/feed_tools/feed.rb', line 698

def feed_data_type=(new_feed_data_type)
  @feed_data_type = new_feed_data_type
  unless self.cache_object.nil?
    self.cache_object.feed_data_type = new_feed_data_type
  end
  if self.feed_data_type != :xml
    @xml_document = nil
  end
end

#feed_data_utf_8(force_encoding = nil) ⇒ Object

Returns the feed’s raw data as utf-8.



662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
# File 'lib/feed_tools/feed.rb', line 662

def feed_data_utf_8(force_encoding=nil)
  if @feed_data_utf_8.nil?
    raw_data = self.feed_data
    if force_encoding.nil?
      use_encoding = self.encoding
    else
      use_encoding = force_encoding
    end
    if use_encoding != "utf-8" && use_encoding != nil && raw_data != nil
      begin
        @feed_data_utf_8 =
          Iconv.new('utf-8', use_encoding).iconv(raw_data)
      rescue Exception => error
        return raw_data
      end
    else
      return self.feed_data
    end
  end
  return @feed_data_utf_8
end

#feed_typeObject

Returns the type of feed Possible values: “rss”, “atom”, “cdf”, “!okay/news”



845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
# File 'lib/feed_tools/feed.rb', line 845

def feed_type
  if @feed_type.nil?
    if self.root_node.nil?
      return nil
    end
    case self.root_node.name.downcase
    when "feed"
      @feed_type = "atom"
    when "rdf:rdf"
      @feed_type = "rss"
    when "rdf"
      @feed_type = "rss"
    when "rss"
      @feed_type = "rss"
    when "channel"
      if self.root_node.namespace == FEED_TOOLS_NAMESPACES['rss11']
        @feed_type = "rss"
      else
        @feed_type = "cdf"
      end
    end
  end
  return @feed_type
end

#feed_type=(new_feed_type) ⇒ Object

Sets the default feed type



871
872
873
# File 'lib/feed_tools/feed.rb', line 871

def feed_type=(new_feed_type)
  @feed_type = new_feed_type
end

#feed_versionObject

Returns the version number of the feed type. Intentionally does not differentiate between the Netscape and Userland versions of RSS 0.91.



878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
# File 'lib/feed_tools/feed.rb', line 878

def feed_version
  if @feed_version.nil?
    if self.root_node.nil?
      return nil
    end
    version = nil
    begin
      version_string = FeedTools::XmlHelper.try_xpaths(self.root_node, [
        "@version"
      ], :select_result_value => true)
      unless version_string.nil?
        version = version_string.to_f
      end
    rescue
    end
    version = nil if version == 0.0
    default_namespace = FeedTools::XmlHelper.try_xpaths(self.root_node, [
      "@xmlns"
    ], :select_result_value => true)
    case self.feed_type
    when "atom"
      if default_namespace == FEED_TOOLS_NAMESPACES['atom10']
        @feed_version = 1.0
      elsif version != nil
        @feed_version = version
      elsif default_namespace == FEED_TOOLS_NAMESPACES['atom03']
        @feed_version = 0.3
      end
    when "rss"
      if default_namespace == FEED_TOOLS_NAMESPACES['rss09']
        @feed_version = 0.9
      elsif default_namespace == FEED_TOOLS_NAMESPACES['rss10']
        @feed_version = 1.0
      elsif default_namespace == FEED_TOOLS_NAMESPACES['rss11']
        @feed_version = 1.1
      elsif version != nil
        case version
        when 2.1
          @feed_version = 2.0
        when 2.01
          @feed_version = 2.0
        else
          @feed_version = version
        end
      end
    when "cdf"
      @feed_version = 0.4
    when "!okay/news"
      @feed_version = 1.0
    end
  end
  return @feed_version
end

#feed_version=(new_feed_version) ⇒ Object

Sets the default feed version



933
934
935
# File 'lib/feed_tools/feed.rb', line 933

def feed_version=(new_feed_version)
  @feed_version = new_feed_version
end

#find_all_nodes(xpath, select_result_value = false) ⇒ Object

Returns all nodes within the channel_node that match the xpath query.



742
743
744
745
746
747
748
# File 'lib/feed_tools/feed.rb', line 742

def find_all_nodes(xpath, select_result_value=false)
  if self.feed_data_type != :xml
    raise "The feed data type is not xml."
  end
  return FeedTools::XmlHelper.try_xpaths_all(self.channel_node, [xpath],
    :select_result_value => select_result_value)
end

#find_node(xpath, select_result_value = false) ⇒ Object

Returns the first node within the channel_node that matches the xpath query.



733
734
735
736
737
738
739
# File 'lib/feed_tools/feed.rb', line 733

def find_node(xpath, select_result_value=false)
  if self.feed_data_type != :xml
    raise "The feed data type is not xml."
  end
  return FeedTools::XmlHelper.try_xpaths(self.channel_node, [xpath],
    :select_result_value => select_result_value)
end

#full_parseObject

Does a full parse of the feed.



435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
# File 'lib/feed_tools/feed.rb', line 435

def full_parse
  self.href

  self.cache_object
  
  self.http_headers
  self.encoding
  self.feed_data_utf_8
  self.xml_document
  self.root_node
  self.channel_node
  
  self.base_uri
  self.feed_type
  self.feed_version

  self.entries

  self.id
  self.title
  self.subtitle
  self.links
  self.link
  self.icon
  self.favicon
  self.author
  self.publisher
  self.time
  self.updated
  self.published
  self.categories
  self.images
  self.rights
  self.time_to_live
  self.generator
  self.language

  self.docs
  self.text_input
  self.cloud

  self.itunes_summary
  self.itunes_subtitle
  self.itunes_author

  self.media_text

  self.explicit?
  
  self.entries.each do |entry|
    entry.full_parse()
  end

  nil
end

#generatorObject

Returns the feed generator



2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
# File 'lib/feed_tools/feed.rb', line 2284

def generator
  if @generator.nil?
    @generator = FeedTools::XmlHelper.try_xpaths(
      self.channel_node, ["generator/text()"],
      :select_result_value => true)
    unless @generator.nil?
      @generator =
        FeedTools::HtmlHelper.convert_html_to_plain_text(@generator)
    end
  end
  return @generator
end

#generator=(new_generator) ⇒ Object

Sets the feed generator

Note: Setting this variable will NOT cause this to appear in any generated output. The generator string is created from the :generator_name and :generator_href configuration variables.



2303
2304
2305
# File 'lib/feed_tools/feed.rb', line 2303

def generator=(new_generator)
  @generator = new_generator
end

#hrefObject Also known as: url

Returns the feed url.



966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
# File 'lib/feed_tools/feed.rb', line 966

def href
  if @href_overridden != true || @href.nil?
    original_href = @href
  
    override_href = lambda do |current_href|
      begin
        if current_href.nil? && self.feed_data != nil
          # The current url is nil and we have feed data to go on
          true
        elsif current_href != nil && !(["http", "https"].include?(
            URI.parse(current_href.to_s).scheme))
          if self.feed_data != nil
            # The current url is set, but isn't a http/https url and
            # we have feed data to use to replace the current url with
            true
          else
            # The current url is set, but isn't a http/https url but
            # we don't have feed data to use to replace the current url
            # with so we'll have to wait until we do
            false
          end
        else
          # The current url is set to an http/https url and there's
          # no compelling reason to override it
          false
        end
      rescue
        # Something went wrong, so we should err on the side of caution
        # and attempt to override the url
        true
      end
    end
    if override_href.call(@href) && self.feed_data != nil
      begin
        links = FeedTools::GenericHelper.recursion_trap(:feed_href) do
          self.links
        end
        link = FeedTools::GenericHelper.recursion_trap(:feed_href) do
          self.link
        end
        if links != nil
          for link_object in links
            if link_object.rel == 'self'
              if link_object.href != link ||
                  (link_object.href =~ /xml/ ||
                  link_object.href =~ /atom/ ||
                  link_object.href =~ /feed/)
                @href = link_object.href
                @href_overridden = true
                @links = nil
                @link = nil
                return @href
              end
            end
          end
        end
      rescue Exception
      end
      @links = nil
      @link = nil
      
      # rdf:about is ordered last because a lot of people put the url to
      # the feed inside it instead of a link to their blog.
      # Ordering it last gives them as many chances as humanly possible
      # for them to redeem themselves.  If the link turns out to be the
      # same as the blog link, it will be reset to the original value.
      @href = FeedTools::XmlHelper.try_xpaths(self.channel_node, [
        "admin:feed/@rdf:resource",
        "admin:feed/@resource",
        "feed/@rdf:resource",
        "feed/@resource",
        "@rdf:about",
        "@about",
        "newLocation/text()",
        "atom10:link[@rel='self']/@href"
      ], :select_result_value => true) do |result|
        override_href.call(FeedTools::UriHelper.normalize_url(result))
      end
      begin
        if !(@href =~ /^file:/) &&
            !FeedTools::UriHelper.is_uri?(@href)
          @href = FeedTools::UriHelper.resolve_relative_uri(
            @href, [self.base_uri])
        end
      rescue
      end
      if self.configurations[:url_normalization_enabled]
        @href = FeedTools::UriHelper.normalize_url(@href)
      end            
      @href.strip! unless @href.nil?
      @href = nil if @href.blank?
      @href_overridden = true
      if @href == nil
        @href = original_href
        @href_overridden = false
      end
      if @href == self.link
        @href = original_href
        @href_overridden = false
      end
      if @href_overridden == true
        @links = nil
        @link = nil
      end
    end
  end
  return @href
end

#href=(new_href) ⇒ Object Also known as: url=

Sets the feed url and prepares the cache_object if necessary.



1076
1077
1078
1079
# File 'lib/feed_tools/feed.rb', line 1076

def href=(new_href)
  @href = FeedTools::UriHelper.normalize_url(new_href)
  self.cache_object.href = new_href unless self.cache_object.nil?
end

#http_headersObject

Returns a hash of the http headers from the response.



534
535
536
537
538
539
540
541
542
543
544
# File 'lib/feed_tools/feed.rb', line 534

def http_headers
  if @http_headers.blank?
    if !self.cache_object.nil? && !self.cache_object.http_headers.nil?
      @http_headers = YAML.load(self.cache_object.http_headers)
      @http_headers = {} unless @http_headers.kind_of? Hash
    else
      @http_headers = {}
    end
  end
  return @http_headers
end

#http_responseObject

Returns the relevant information from an http request.



529
530
531
# File 'lib/feed_tools/feed.rb', line 529

def http_response
  return @http_response
end

#iconObject

Returns the url to the icon file for this feed.



1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
# File 'lib/feed_tools/feed.rb', line 1534

def icon
  if @icon.nil?
    icon_node = FeedTools::XmlHelper.try_xpaths(self.channel_node, [
      "link[@rel='icon']",
      "link[@rel='shortcut icon']",
      "link[@type='image/x-icon']",
      "icon",
      "logo[@style='icon']",
      "LOGO[@STYLE='ICON']"
    ])
    unless icon_node.nil?
      @icon = FeedTools::XmlHelper.try_xpaths(icon_node, [
        "@atom10:href",
        "@atom03:href",
        "@atom:href",
        "@href",
        "text()"
      ], :select_result_value => true)
      begin
        if !(@icon =~ /^file:/) &&
            !FeedTools::UriHelper.is_uri?(@icon)
          channel_base_uri = nil
          unless self.channel_node.nil?
            channel_base_uri = self.channel_node.base_uri
          end
          @icon = FeedTools::UriHelper.resolve_relative_uri(
            @icon, [channel_base_uri, self.base_uri])
        end
      rescue
      end
      @icon = nil unless FeedTools::UriHelper.is_uri?(@icon)
      @icon = nil if @icon.blank?
    end
  end
  return @icon
end

#idObject Also known as: guid

Returns the feed’s unique id



938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
# File 'lib/feed_tools/feed.rb', line 938

def id
  if @id.nil?
    @id = FeedTools::XmlHelper.select_not_blank([
      FeedTools::XmlHelper.try_xpaths(self.channel_node, [
        "atom10:id/text()",
        "atom03:id/text()",
        "atom:id/text()",
        "id/text()",
        "guid/text()"
      ], :select_result_value => true),
      FeedTools::XmlHelper.try_xpaths(self.root_node, [
        "atom10:id/text()",
        "atom03:id/text()",
        "atom:id/text()",
        "id/text()",
        "guid/text()"
      ], :select_result_value => true)
    ])
  end
  return @id
end

#id=(new_id) ⇒ Object Also known as: guid=

Sets the feed’s unique id



961
962
963
# File 'lib/feed_tools/feed.rb', line 961

def id=(new_id)
  @id = new_id
end

#imagesObject

Returns a list of the feed’s images



1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
# File 'lib/feed_tools/feed.rb', line 1998

def images
  if @images.nil?
    @images = []
    image_nodes = FeedTools::XmlHelper.combine_xpaths_all(
      self.channel_node, [
        "image",
        "logo",
        "apple-wallpapers:image",
        "imageUrl"
      ]
    )
    unless image_nodes.blank?
      for image_node in image_nodes
        image = FeedTools::Image.new
        image.href = FeedTools::XmlHelper.try_xpaths(image_node, [
          "url/text()",
          "@rdf:resource",
          "@href",
          "text()"
        ], :select_result_value => true)
        if image.href.nil? && image_node.base_uri != nil
          image.href = ""
        end
        begin
          if !(image.href =~ /^file:/) &&
              !FeedTools::UriHelper.is_uri?(image.href)
            image.href = FeedTools::UriHelper.resolve_relative_uri(
              image.href, [image_node.base_uri, self.base_uri])
          end
        rescue
        end
        if self.configurations[:url_normalization_enabled]
          image.href = FeedTools::UriHelper.normalize_url(image.href)
        end            
        image.href.strip! unless image.href.nil?
        next if image.href.blank?
        image.title = FeedTools::XmlHelper.try_xpaths(image_node,
          ["title/text()"], :select_result_value => true)
        image.title.strip! unless image.title.nil?
        image.description = FeedTools::XmlHelper.try_xpaths(image_node,
          ["description/text()"], :select_result_value => true)
        image.description.strip! unless image.description.nil?
        image.link = FeedTools::XmlHelper.try_xpaths(image_node,
          ["link/text()"], :select_result_value => true)
        image.link.strip! unless image.link.nil?
        image.height = FeedTools::XmlHelper.try_xpaths(image_node,
          ["height/text()"], :select_result_value => true).to_i
        image.height = nil if image.height <= 0
        image.width = FeedTools::XmlHelper.try_xpaths(image_node,
          ["width/text()"], :select_result_value => true).to_i
        image.width = nil if image.width <= 0
        image.style = FeedTools::XmlHelper.try_xpaths(image_node, [
          "style/text()",
          "@style"
        ], :select_result_value => true)
        image.style.strip! unless image.style.nil?
        image.style.downcase! unless image.style.nil?
        @images << image unless image.href.nil?
      end
    end
    for link_object in self.links
      if link_object.type != nil && link_object.type =~ /^image/
        image = FeedTools::Image.new
        image.href = link_object.href
        image.title = link_object.title
        @images << image unless image.href.nil?
      end
    end
  end
  return @images
end

#inspectObject

Returns a simple representation of the feed object’s state.



2896
2897
2898
# File 'lib/feed_tools/feed.rb', line 2896

def inspect
  return "#<FeedTools::Feed:0x#{self.object_id.to_s(16)} URL:#{self.href}>"
end

#itunes_authorObject

Returns the contents of the itunes:author element

Returns any incorrectly placed channel-level itunes:author elements. They’re actually amazingly common. People don’t read specs. There is no setter for this, since this is an incorrectly placed attribute.



1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
# File 'lib/feed_tools/feed.rb', line 1846

def itunes_author
  if @itunes_author.nil?
    @itunes_author = FeedTools::HtmlHelper.unescape_entities(
      FeedTools::XmlHelper.try_xpaths(self.channel_node, [
        "itunes:author/text()"
      ], :select_result_value => true)
    )
    @itunes_author = nil if @itunes_author.blank?
  end
  return @itunes_author
end

#itunes_subtitleObject

Returns the contents of the itunes:subtitle element



1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
# File 'lib/feed_tools/feed.rb', line 1188

def itunes_subtitle
  if @itunes_subtitle.nil?
    @itunes_subtitle = FeedTools::XmlHelper.select_not_blank([
      FeedTools::XmlHelper.try_xpaths(self.channel_node, [
        "itunes:subtitle/text()"
      ], :select_result_value => true),
      FeedTools::XmlHelper.try_xpaths(self.root_node, [
        "itunes:subtitle/text()"
      ], :select_result_value => true)
    ])
    unless @itunes_subtitle.blank?
      @itunes_subtitle =
        FeedTools::HtmlHelper.unescape_entities(@itunes_subtitle)
      @itunes_subtitle =
        FeedTools::HtmlHelper.sanitize_html(@itunes_subtitle)
      @itunes_subtitle.strip!
    else
      @itunes_subtitle = nil
    end
  end
  return @itunes_subtitle
end

#itunes_subtitle=(new_itunes_subtitle) ⇒ Object

Sets the contents of the itunes:subtitle element



1212
1213
1214
# File 'lib/feed_tools/feed.rb', line 1212

def itunes_subtitle=(new_itunes_subtitle)
  @itunes_subtitle = new_itunes_subtitle
end

#itunes_summaryObject

Returns the contents of the itunes:summary element



1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
# File 'lib/feed_tools/feed.rb', line 1159

def itunes_summary
  if @itunes_summary.nil?
    @itunes_summary = FeedTools::XmlHelper.select_not_blank([
      FeedTools::XmlHelper.try_xpaths(self.channel_node, [
        "itunes:summary/text()"
      ], :select_result_value => true),
      FeedTools::XmlHelper.try_xpaths(self.root_node, [
        "itunes:summary/text()"
      ], :select_result_value => true)
    ])
    unless @itunes_summary.blank?
      @itunes_summary =
        FeedTools::HtmlHelper.unescape_entities(@itunes_summary)
      @itunes_summary =
        FeedTools::HtmlHelper.sanitize_html(@itunes_summary)
      @itunes_summary.strip!
    else
      @itunes_summary = nil
    end
  end
  return @itunes_summary
end

#itunes_summary=(new_itunes_summary) ⇒ Object

Sets the contents of the itunes:summary element



1183
1184
1185
# File 'lib/feed_tools/feed.rb', line 1183

def itunes_summary=(new_itunes_summary)
  @itunes_summary = new_itunes_summary
end

#languageObject

Returns the feed language



2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
# File 'lib/feed_tools/feed.rb', line 2338

def language
  if @language.nil?
    @language = FeedTools::XmlHelper.select_not_blank([
      FeedTools::XmlHelper.try_xpaths(self.channel_node, [
        "language/text()",
        "dc:language/text()",
        "@dc:language",
        "@xml:lang",
        "xml:lang/text()"
      ], :select_result_value => true),
      FeedTools::XmlHelper.try_xpaths(self.root_node, [
        "@xml:lang",
        "xml:lang/text()"
      ], :select_result_value => true)
    ])
    if @language.blank?
      @language = "en-us"
    end
    @language.gsub!(/_/, "-")
    @language = @language.downcase
    if @language.split('-').size > 1
      @language =
        "#{@language.split('-').first}-" +
        "#{@language.split('-').last.upcase}"
    end
  end
  return @language
end

#language=(new_language) ⇒ Object

Sets the feed language



2368
2369
2370
# File 'lib/feed_tools/feed.rb', line 2368

def language=(new_language)
  @language = new_language
end

#last_retrievedObject

The time that the feed was last requested from the remote server. Nil if it has never been pulled, or if it was created from scratch.



2482
2483
2484
2485
2486
2487
# File 'lib/feed_tools/feed.rb', line 2482

def last_retrieved
  unless self.cache_object.nil?
    @last_retrieved = self.cache_object.last_retrieved
  end
  return @last_retrieved
end

#last_retrieved=(new_last_retrieved) ⇒ Object

Sets the time that the feed was last updated.



2490
2491
2492
2493
2494
2495
# File 'lib/feed_tools/feed.rb', line 2490

def last_retrieved=(new_last_retrieved)
  @last_retrieved = new_last_retrieved
  unless self.cache_object.nil?
    self.cache_object.last_retrieved = new_last_retrieved
  end
end

#licenseObject

Returns the first license link for the feed item.



2099
2100
2101
# File 'lib/feed_tools/feed.rb', line 2099

def license
  return self.licenses.first
end

#licensesObject

Returns all licenses linked from this feed item.



2104
2105
2106
2107
2108
2109
2110
2111
# File 'lib/feed_tools/feed.rb', line 2104

def licenses
  if @licenses.nil?
    @licenses = self.links.select do |link|
      link.rel == "license"
    end
  end
  return @licenses
end

#licenses=(new_licenses) ⇒ Object

Sets the feed item’s licenses.



2114
2115
2116
# File 'lib/feed_tools/feed.rb', line 2114

def licenses=(new_licenses)
  @licenses = new_licenses
end

Returns the feed link



1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
# File 'lib/feed_tools/feed.rb', line 1244

def link
  if @link.nil?
    max_score = 0
    for link_object in self.links.reverse
      score = 0
      next if link_object.href.nil?
      if @href != nil && link_object.href == @href
        score = score - 2
      end
      if link_object.type != nil
        if (link_object.type =~ /image/ || link_object.type =~ /video/)
          score = score - 2
        end
        if FeedTools::HtmlHelper.xml_type?(link_object.type)
          score = score + 1
        end
        if FeedTools::HtmlHelper.html_type?(link_object.type)
          score = score + 2
        elsif link_object.type != nil
          score = score - 1
        end
      end
      if link_object.rel == "enclosure"
        score = score - 2
      end
      if link_object.rel == "alternate"
        score = score + 1
      end
      if link_object.rel == "self"
        score = score - 1
        if (link_object.href =~ /xml/ ||
            link_object.href =~ /atom/ ||
            link_object.href =~ /feed/)
          score = score - 1
        end
      end
      if score >= max_score
        max_score = score
        @link = link_object.href
      end
    end
    if @link.blank?
      @link = FeedTools::XmlHelper.try_xpaths(self.channel_node, [
        "@href",
        "@rdf:about",
        "@about"
      ], :select_result_value => true)
    end
    if @link.blank?
      if FeedTools::UriHelper.is_uri?(self.id) &&
          (self.id =~ /^http/)
        @link = self.id
      end
    end
    if !@link.blank?
      @link = FeedTools::HtmlHelper.unescape_entities(@link)
    end
    @link = nil if @link.blank?
    begin
      if !(@link =~ /^file:/) &&
          !FeedTools::UriHelper.is_uri?(@link)
        channel_base_uri = nil
        unless self.channel_node.nil?
          channel_base_uri = self.channel_node.base_uri
        end
        @link = FeedTools::UriHelper.resolve_relative_uri(
          @link, [channel_base_uri, self.base_uri])
      end
    rescue
    end
    if self.configurations[:url_normalization_enabled]
      @link = FeedTools::UriHelper.normalize_url(@link)
    end
    unless self.cache_object.nil?
      self.cache_object.link = @link
    end
  end
  return @link
end

#link=(new_link) ⇒ Object

Sets the feed link



1325
1326
1327
1328
1329
1330
# File 'lib/feed_tools/feed.rb', line 1325

def link=(new_link)
  @link = new_link
  unless self.cache_object.nil?
    self.cache_object.link = new_link
  end
end

Returns the links collection



1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
# File 'lib/feed_tools/feed.rb', line 1333

def links
  if @links.blank?
    @links = []
    link_nodes =
      FeedTools::XmlHelper.combine_xpaths_all(self.channel_node, [
        "atom10:link",
        "atom03:link",
        "atom:link",
        "link",
        "channelLink",
        "a",
        "url",
        "href"
      ])
    for link_node in link_nodes
      link_object = FeedTools::Link.new
      link_object.href = FeedTools::XmlHelper.try_xpaths(link_node, [
        "@atom10:href",
        "@atom03:href",
        "@atom:href",
        "@href",
        "text()"
      ], :select_result_value => true)
      if link_object.href == "atom10:" ||
          link_object.href == "atom03:" ||
          link_object.href == "atom:"
        link_object.href = FeedTools::XmlHelper.try_xpaths(link_node, [
          "@href"
        ], :select_result_value => true)
      end
      if link_object.href.nil? && link_node.base_uri != nil
        link_object.href = ""
      end
      begin
        if !(link_object.href =~ /^file:/) &&
            !FeedTools::UriHelper.is_uri?(link_object.href)
          link_object.href = FeedTools::UriHelper.resolve_relative_uri(
            link_object.href,
            [link_node.base_uri, self.base_uri])
        end
      rescue
      end
      if self.configurations[:url_normalization_enabled]
        link_object.href =
          FeedTools::UriHelper.normalize_url(link_object.href)
      end
      link_object.href.strip! unless link_object.href.nil?
      next if link_object.href.blank?
      link_object.hreflang = FeedTools::XmlHelper.try_xpaths(link_node, [
        "@atom10:hreflang",
        "@atom03:hreflang",
        "@atom:hreflang",
        "@hreflang"
      ], :select_result_value => true)
      if link_object.hreflang == "atom10:" ||
          link_object.hreflang == "atom03:" ||
          link_object.hreflang == "atom:"
        link_object.hreflang = FeedTools::XmlHelper.try_xpaths(link_node, [
          "@hreflang"
        ], :select_result_value => true)
      end
      unless link_object.hreflang.nil?
        link_object.hreflang = link_object.hreflang.downcase
      end
      link_object.rel = FeedTools::XmlHelper.try_xpaths(link_node, [
        "@atom10:rel",
        "@atom03:rel",
        "@atom:rel",
        "@rel"
      ], :select_result_value => true)
      if link_object.rel == "atom10:" ||
          link_object.rel == "atom03:" ||
          link_object.rel == "atom:"
        link_object.rel = FeedTools::XmlHelper.try_xpaths(link_node, [
          "@rel"
        ], :select_result_value => true)
      end
      unless link_object.rel.nil?
        link_object.rel = link_object.rel.downcase
      end
      if link_object.rel.nil? && self.feed_type == "atom"
        link_object.rel = "alternate"
      end
      link_object.type = FeedTools::XmlHelper.try_xpaths(link_node, [
        "@atom10:type",
        "@atom03:type",
        "@atom:type",
        "@type"
      ], :select_result_value => true)
      if link_object.type == "atom10:" ||
          link_object.type == "atom03:" ||
          link_object.type == "atom:"
        link_object.type = FeedTools::XmlHelper.try_xpaths(link_node, [
          "@type"
        ], :select_result_value => true)
      end
      unless link_object.type.nil?
        link_object.type = link_object.type.downcase
      end
      link_object.title = FeedTools::XmlHelper.try_xpaths(link_node, [
        "@atom10:title",
        "@atom03:title",
        "@atom:title",
        "@title",
        "text()"
      ], :select_result_value => true)
      if link_object.title == "atom10:" ||
          link_object.title == "atom03:" ||
          link_object.title == "atom:"
        link_object.title = FeedTools::XmlHelper.try_xpaths(link_node, [
          "@title"
        ], :select_result_value => true)
      end
      # This catches the ambiguities between atom, rss, and cdf
      if link_object.title == link_object.href
        link_object.title = nil
      end
      link_object.length = FeedTools::XmlHelper.try_xpaths(link_node, [
        "@atom10:length",
        "@atom03:length",
        "@atom:length",
        "@length"
      ], :select_result_value => true)
      if link_object.length == "atom10:" ||
          link_object.length == "atom03:" ||
          link_object.length == "atom:"
        link_object.length = FeedTools::XmlHelper.try_xpaths(link_node, [
          "@length"
        ], :select_result_value => true)
      end
      if !link_object.length.nil?
        link_object.length = link_object.length.to_i
      else
        if !link_object.type.nil? && link_object.type[0..4] != "text" &&
            link_object.type[-3..-1] != "xml" &&
            link_object.href =~ /^http:\/\//
          # Retrieve the length with an http HEAD request
        else
          link_object.length = nil
        end
      end
      @links = [] if @links.nil?
      @links << link_object
    end
  end
  return @links
end

#links=(new_links) ⇒ Object

Sets the links collection



1482
1483
1484
# File 'lib/feed_tools/feed.rb', line 1482

def links=(new_links)
  @links = new_links
end

#live?Boolean

True if the feed was not last retrieved from the cache.

Returns:

  • (Boolean)


2520
2521
2522
# File 'lib/feed_tools/feed.rb', line 2520

def live?
  return @live
end

#load_remote_feed!Object

Attempts to load the feed from the remote location. Requires the url field to be set. If an etag or the last_modified date has been set, attempts to use them to prevent unnecessary reloading of identical content.



289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
# File 'lib/feed_tools/feed.rb', line 289

def load_remote_feed!
  @live = true
  if self.http_headers.nil? && !(self.cache_object.nil?) &&
      !(self.cache_object.http_headers.nil?)
    @http_headers = YAML.load(self.cache_object.http_headers)
  end

  if (self.href =~ /^feed:/) == 0
    # Woah, Nelly, how'd that happen?  You should've already been
    # corrected.  So let's fix that url.  And please,
    # just use less crappy browsers instead of badly defined
    # pseudo-protocol hacks.
    self.href = FeedTools::UriHelper.normalize_url(self.href)
  end

  # Find out what method we're going to be using to obtain this feed.
  begin
    uri = URI.parse(self.href)
  rescue URI::InvalidURIError
    raise FeedAccessError,
      "Cannot retrieve feed using invalid URL: " + self.href.to_s
  end
  retrieval_method = "http"
  case uri.scheme
  when "http"
    retrieval_method = "http"
  when "ftp"
    retrieval_method = "ftp"
  when "file"
    retrieval_method = "file"
  when nil
    raise FeedAccessError,
      "No protocol was specified in the url."
  else
    raise FeedAccessError,
      "Cannot retrieve feed using unrecognized protocol: " + uri.scheme
  end

  # No need for http headers unless we're actually doing http
  if retrieval_method == "http"
    begin
      @http_response = (FeedTools::RetrievalHelper.http_get(
        self.href, :feed_object => self) do |url, response|
          # Find out if we've already seen the url we've been
          # redirected to.
          follow_redirect = true

          begin
            cached_feed = FeedTools::Feed.open(url,
              :disable_update_from_remote => true)
            if cached_feed.cache_object != nil &&
                cached_feed.cache_object.new_record? != true
              if !cached_feed.expired? &&
                  !cached_feed.http_headers.blank?
                # Copy the cached state
                self.href = cached_feed.href

                @feed_data = cached_feed.feed_data
                @feed_data_type = cached_feed.feed_data_type

                if @feed_data.blank?
                  raise "Invalid cache data."
                end

                @title = nil; self.title
                self.href
                @link = nil; self.link
              
                self.last_retrieved = cached_feed.last_retrieved
                self.http_headers = cached_feed.http_headers
                self.cache_object = cached_feed.cache_object
                @live = false
                follow_redirect = false
              end
            end
          rescue
            # If anything goes wrong, ignore it.
          end
          follow_redirect
        end)
      case @http_response
      when Net::HTTPSuccess
        @feed_data = self.http_response.body
        @http_headers = {}
        self.http_response.each_header do |key, value|
          self.http_headers[key.downcase] = value
        end
        self.last_retrieved = Time.now.gmtime
        @live = true
      when Net::HTTPNotModified
        @http_headers = {}
        self.http_response.each_header do |key, value|
          self.http_headers[key.downcase] = value
        end
        self.last_retrieved = Time.now.gmtime
        @live = false
      else
        @live = false
      end
    rescue Exception => error
      @live = false
      if self.feed_data.nil?
        raise error
      end
    end
  elsif retrieval_method == "https"
    # Not supported... yet
  elsif retrieval_method == "ftp"
    # Not supported... yet
    # Technically, CDF feeds are supposed to be able to be accessed
    # directly from an ftp server.  This is silly, but we'll humor
    # Microsoft.
    #
    # Eventually.  If they're lucky.  And someone demands it.
  elsif retrieval_method == "file"
    # Now that we've gone to all that trouble to ensure the url begins
    # with 'file://', strip the 'file://' off the front of the url.
    file_name = self.href.gsub(/^file:\/\//, "")
    if RUBY_PLATFORM =~ /mswin/
      file_name = file_name[1..-1] if file_name[0..0] == "/"
    end
    begin
      open(file_name) do |file|
        @http_response = nil
        @http_headers = {}
        @feed_data = file.read
        @feed_data_type = :xml
        self.last_retrieved = Time.now.gmtime
      end
    rescue
      @live = false
      # In this case, pulling from the cache is probably not going
      # to help at all, and the use should probably be immediately
      # appraised of the problem.  Raise the exception.
      raise
    end
  end
  unless self.cache_object.nil?
    begin
      self.save
    rescue
    end
  end
end

#media_textObject

Returns the contents of the media:text element



1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
# File 'lib/feed_tools/feed.rb', line 1217

def media_text
  if @media_text.nil?
    @media_text = FeedTools::XmlHelper.select_not_blank([
      FeedTools::XmlHelper.try_xpaths(self.channel_node, [
        "media:text/text()"
      ], :select_result_value => true),
      FeedTools::XmlHelper.try_xpaths(self.root_node, [
        "media:text/text()"
      ], :select_result_value => true)
    ])
    unless @media_text.blank?
      @media_text = FeedTools::HtmlHelper.unescape_entities(@media_text)
      @media_text = FeedTools::HtmlHelper.sanitize_html(@media_text)
      @media_text.strip!
    else
      @media_text = nil
    end
  end
  return @media_text
end

#media_text=(new_media_text) ⇒ Object

Sets the contents of the media:text element



1239
1240
1241
# File 'lib/feed_tools/feed.rb', line 1239

def media_text=(new_media_text)
  @media_text = new_media_text
end

#podcast?Boolean

True if this feed contains audio content enclosures

Returns:

  • (Boolean)


2498
2499
2500
2501
2502
2503
2504
2505
2506
# File 'lib/feed_tools/feed.rb', line 2498

def podcast?
  podcast = false
  self.items.each do |item|
    item.enclosures.each do |enclosure|
      podcast = true if enclosure.audio?
    end
  end
  return podcast
end

#publishedObject

Returns the feed published time



1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
# File 'lib/feed_tools/feed.rb', line 1935

def published
  if @published.nil?
    published_string =
      FeedTools::XmlHelper.try_xpaths(self.channel_node, [
        "atom10:published/text()",
        "atom03:published/text()",
        "atom:published/text()",
        "published/text()",
        "dc:date/text()",
        "pubDate/text()",
        "atom10:issued/text()",
        "atom03:issued/text()",
        "atom:issued/text()",
        "issued/text()"
      ], :select_result_value => true)
    unless published_string.blank?
      @published = Time.parse(published_string).gmtime rescue nil
    else
      @published = nil
    end
  end
  return @published
end

#published=(new_published) ⇒ Object

Sets the feed published time



1960
1961
1962
# File 'lib/feed_tools/feed.rb', line 1960

def published=(new_published)
  @published = new_published
end

#publisherObject

Returns the feed publisher



1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
# File 'lib/feed_tools/feed.rb', line 1759

def publisher
  if @publisher.nil?
    @publisher = FeedTools::Author.new
    @publisher.raw = FeedTools::HtmlHelper.unescape_entities(        
      FeedTools::XmlHelper.try_xpaths(self.channel_node, [
        "webMaster/text()",
        "dc:publisher/text()"
      ], :select_result_value => true))

    unless @publisher.raw.blank?
      raw_scan = @publisher.raw.scan(
        /(.*)\((\b[A-Z0-9._%-\+]+@[A-Z0-9._%-]+\.[A-Z]{2,4}\b)\)/i)
      if raw_scan.nil? || raw_scan.size == 0
        raw_scan = @publisher.raw.scan(
          /(\b[A-Z0-9._%-\+]+@[A-Z0-9._%-]+\.[A-Z]{2,4}\b)\s*\((.*)\)/i)
        unless raw_scan.size == 0
          publisher_raw_pair = raw_scan.first.reverse
        end
      else
        publisher_raw_pair = raw_scan.first
      end
      if raw_scan.nil? || raw_scan.size == 0
        email_scan = @publisher.raw.scan(
          /\b[A-Z0-9._%-\+]+@[A-Z0-9._%-]+\.[A-Z]{2,4}\b/i)
        if email_scan != nil && email_scan.size > 0
          @publisher.email = email_scan.first.strip
        end
      end
      unless publisher_raw_pair.nil? || publisher_raw_pair.size == 0
        @publisher.name = publisher_raw_pair.first.strip
        @publisher.email = publisher_raw_pair.last.strip
      else
        unless @publisher.raw.include?("@")
          # We can be reasonably sure we are looking at something
          # that the creator didn't intend to contain an email address if
          # it got through the preceeding regexes and it doesn't
          # contain the tell-tale '@' symbol.
          @publisher.name = @publisher.raw
        end
      end
    end

    @publisher.name = nil if @publisher.name.blank?
    @publisher.raw = nil if @publisher.raw.blank?
    @publisher.email = nil if @publisher.email.blank?
    @publisher.url = nil if @publisher.url.blank?
    if @publisher.url != nil
      begin
        if !(@publisher.url =~ /^file:/) &&
            !FeedTools::UriHelper.is_uri?(@publisher.url)
          channel_base_uri = nil
          unless self.channel_node.nil?
            channel_base_uri = self.channel_node.base_uri
          end
          @publisher.url = FeedTools::UriHelper.resolve_relative_uri(
            @publisher.url, [channel_base_uri, self.base_uri])
        end
      rescue
      end
    end        
  end
  return @publisher
end

#publisher=(new_publisher) ⇒ Object

Sets the feed publisher



1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
# File 'lib/feed_tools/feed.rb', line 1824

def publisher=(new_publisher)
  if new_publisher.respond_to?(:name) &&
      new_publisher.respond_to?(:email) &&
      new_publisher.respond_to?(:url)
    # It's a complete Author object, just set it.
    @publisher = new_publisher
  else
    # We're not looking at an Author object, this is probably a string,
    # default to setting the publisher's name.
    if @publisher.nil?
      @publisher = FeedTools::Author.new
    end
    @publisher.name = new_publisher
  end
end

#rightsObject Also known as: copyright

Returns the feed’s copyright information



2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
# File 'lib/feed_tools/feed.rb', line 2071

def rights
  if @rights.nil?
    repair_entities = false
    rights_node = FeedTools::XmlHelper.try_xpaths(self.channel_node, [
      "atom10:copyright",
      "atom03:copyright",
      "atom:copyright",
      "copyright",
      "copyrights",
      "dc:rights",
      "rights"
    ])
    @rights = FeedTools::HtmlHelper.process_text_construct(rights_node,
      self.feed_type, self.feed_version, [self.base_uri])
    if self.feed_type == "atom" ||
        self.configurations[:always_strip_wrapper_elements]
      @rights = FeedTools::HtmlHelper.strip_wrapper_element(@rights)
    end
  end
  return @rights
end

#rights=(new_rights) ⇒ Object Also known as: copyright=

Sets the feed’s rights information



2094
2095
2096
# File 'lib/feed_tools/feed.rb', line 2094

def rights=(new_rights)
  @rights = new_rights
end

#root_nodeObject

Returns the root node of the feed.



751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
# File 'lib/feed_tools/feed.rb', line 751

def root_node
  if @root_node.nil?
    # TODO: Fix this so that added content at the end of the file doesn't
    # break this stuff.
    # E.g.: http://smogzer.tripod.com/smog.rdf
    # ===================================================================
    begin
      if self.xml_document.nil?
        return nil
      else
        @root_node = self.xml_document.root
      end
    rescue Exception
      return nil
    end
  end
  return @root_node
end

#saveObject

Persists the current feed state to the cache.



2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
# File 'lib/feed_tools/feed.rb', line 2818

def save
  if self.configurations[:feed_cache].nil?
    # The cache is disabled for this feed, do nothing.
    return
  end
  if self.feed_data.blank? && self.http_headers.blank?
    # There's no data, nothing to save.
    return
  end
  if self.http_headers['content-type'] =~ /text\/html/ ||
      self.http_headers['content-type'] =~ /application\/xhtml\+xml/
    if self.title.nil? && self.link.nil? && self.entries.blank?
      # Don't save html pages to the cache, it messes with
      # autodiscovery.
      return
    end
  end
  unless self.href =~ /^file:\/\//
    if FeedTools.feed_cache.nil?
      raise "Caching is currently disabled.  Cannot save to cache."
    elsif self.href.nil?
      raise "The url field must be set to save to the cache."
    elsif self.cache_object.nil?
      raise "The cache_object is currently nil.  Cannot save to cache."
    else
      self.cache_object.href = self.href
      unless self.feed_data.nil?
        self.cache_object.title = self.title
        self.cache_object.link = self.link
        self.cache_object.feed_data = self.feed_data
        self.cache_object.feed_data_type = self.feed_data_type.to_s
      end
      self.cache_object.http_headers = self.http_headers.to_yaml
      self.cache_object.last_retrieved = self.last_retrieved
      Thread.pass
      self.cache_object.save
    end
  end
end

#serializableObject

Returns a duplicate object suitable for serialization



508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
# File 'lib/feed_tools/feed.rb', line 508

def serializable
  self.full_parse()
  entries_to_dump = self.entries
  # This prevents errors due to temporarily having feed items with
  # multiple parent feeds.
  self.entries = []
  feed_to_dump = self.dup
  feed_to_dump.instance_variable_set("@xml_document", nil)
  feed_to_dump.instance_variable_set("@root_node", nil)
  feed_to_dump.instance_variable_set("@channel_node", nil)
  feed_to_dump.entries = entries_to_dump.collect do |entry|
    entry.serializable
  end
  self.entries = entries_to_dump
  feed_to_dump.entries.each do |entry|
    entry.instance_variable_set("@root_node", nil)
  end
  return feed_to_dump
end

#serialize_to_cacheObject

Does a full parse, then serializes the feed object directly to the cache.



493
494
495
496
497
498
499
500
501
502
503
504
505
# File 'lib/feed_tools/feed.rb', line 493

def serialize_to_cache
  @cache_object = nil
  require 'yaml'
  serialized_feed = YAML.dump(self.serializable)
  if self.cache_object != nil
    begin
      self.cache_object.serialized = serialized_feed
      self.cache_object.save
    rescue Exception
    end
  end
  return nil
end

#subtitleObject Also known as: tagline, description, abstract

Returns the feed subtitle



1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
# File 'lib/feed_tools/feed.rb', line 1113

def subtitle
  if @subtitle.nil?
    subtitle_node = FeedTools::XmlHelper.try_xpaths(self.channel_node, [
      "atom10:subtitle",
      "subtitle",
      "atom03:tagline",
      "tagline",
      "description",
      "summary",
      "abstract",
      "ABSTRACT",
      "content:encoded",
      "encoded",
      "content",
      "xhtml:body",
      "body",
      "xhtml:div",
      "div",
      "p:payload",
      "payload",
      "channelDescription",
      "blurb",
      "info"
    ])
    @subtitle = FeedTools::HtmlHelper.process_text_construct(
      subtitle_node, self.feed_type, self.feed_version, [self.base_uri])
    if self.feed_type == "atom" ||
        self.configurations[:always_strip_wrapper_elements]
      @subtitle = FeedTools::HtmlHelper.strip_wrapper_element(@subtitle)
    end
    if @subtitle.blank?
      @subtitle = self.itunes_summary
    end
    if @subtitle.blank?
      @subtitle = self.itunes_subtitle
    end
  end
  return @subtitle
end

#subtitle=(new_subtitle) ⇒ Object Also known as: tagline=, description=, abstract=

Sets the feed subtitle



1154
1155
1156
# File 'lib/feed_tools/feed.rb', line 1154

def subtitle=(new_subtitle)
  @subtitle = new_subtitle
end

#text_inputObject

Returns the feed’s text input field



2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
# File 'lib/feed_tools/feed.rb', line 2256

def text_input
  if @text_input.nil?
    @text_input = FeedTools::TextInput.new
    text_input_node =
      FeedTools::XmlHelper.try_xpaths(self.channel_node, ["textInput"])
    unless text_input_node.nil?
      @text_input.title =
        FeedTools::XmlHelper.try_xpaths(text_input_node,
          ["title/text()"],
          :select_result_value => true)
      @text_input.description =
        FeedTools::XmlHelper.try_xpaths(text_input_node,
          ["description/text()"],
          :select_result_value => true)
      @text_input.link =
        FeedTools::XmlHelper.try_xpaths(text_input_node,
          ["link/text()"],
          :select_result_value => true)
      @text_input.name =
        FeedTools::XmlHelper.try_xpaths(text_input_node,
          ["name/text()"],
          :select_result_value => true)
    end
  end
  return @text_input
end

#timeObject

Returns the feed time



1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
# File 'lib/feed_tools/feed.rb', line 1859

def time
  if @time.nil?
    time_string = FeedTools::XmlHelper.try_xpaths(self.channel_node, [
      "atom10:updated/text()",
      "atom03:updated/text()",
      "atom:updated/text()",
      "updated/text()",
      "atom10:modified/text()",
      "atom03:modified/text()",
      "atom:modified/text()",
      "modified/text()",
      "time/text()",
      "lastBuildDate/text()",
      "atom10:issued/text()",
      "atom03:issued/text()",
      "atom:issued/text()",
      "issued/text()",
      "atom10:published/text()",
      "atom03:published/text()",
      "atom:published/text()",
      "published/text()",
      "dc:date/text()",
      "pubDate/text()",
      "date/text()"
    ], :select_result_value => true)
    begin
      unless time_string.blank?
        @time = Time.parse(time_string).gmtime
      else
        if self.configurations[:timestamp_estimation_enabled]
          @time = Time.now.gmtime
        end
      end
    rescue
      if self.configurations[:timestamp_estimation_enabled]
        @time = Time.now.gmtime
      end
    end
  end
  return @time
end

#time=(new_time) ⇒ Object

Sets the feed time



1902
1903
1904
# File 'lib/feed_tools/feed.rb', line 1902

def time=(new_time)
  @time = new_time
end

#time_to_liveObject Also known as: ttl

Returns the number of seconds before the feed should expire



2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
# File 'lib/feed_tools/feed.rb', line 2119

def time_to_live
  if @time_to_live.nil?
    unless channel_node.nil?
      # get the feed time to live from the xml document
      update_frequency = FeedTools::XmlHelper.try_xpaths(
        self.channel_node,
        ["syn:updateFrequency/text()"], :select_result_value => true)
      if !update_frequency.blank?
        update_period = FeedTools::XmlHelper.try_xpaths(
          self.channel_node,
          ["syn:updatePeriod/text()"], :select_result_value => true)
        if update_period == "daily"
          @time_to_live = update_frequency.to_i.day
        elsif update_period == "weekly"
          @time_to_live = update_frequency.to_i.week
        elsif update_period == "monthly"
          @time_to_live = update_frequency.to_i.month
        elsif update_period == "yearly"
          @time_to_live = update_frequency.to_i.year
        else
          # hourly
          @time_to_live = update_frequency.to_i.hour
        end
      end
      if @time_to_live.nil?
        # usually expressed in minutes
        update_frequency = FeedTools::XmlHelper.try_xpaths(
          self.channel_node, ["ttl/text()"],
          :select_result_value => true)
        if !update_frequency.blank?
          update_span = FeedTools::XmlHelper.try_xpaths(
            self.channel_node, ["ttl/@span"],
            :select_result_value => true)
          if update_span == "seconds"
            @time_to_live = update_frequency.to_i
          elsif update_span == "minutes"
            @time_to_live = update_frequency.to_i.minute
          elsif update_span == "hours"
            @time_to_live = update_frequency.to_i.hour
          elsif update_span == "days"
            @time_to_live = update_frequency.to_i.day
          elsif update_span == "weeks"
            @time_to_live = update_frequency.to_i.week
          elsif update_span == "months"
            @time_to_live = update_frequency.to_i.month
          elsif update_span == "years"
            @time_to_live = update_frequency.to_i.year
          else
            @time_to_live = update_frequency.to_i.minute
          end
        end
      end
      if @time_to_live.nil?
        @time_to_live = 0
        update_frequency_days =
          FeedTools::XmlHelper.try_xpaths(self.channel_node,
          ["schedule/intervaltime/@day"], :select_result_value => true)
        update_frequency_hours =
          FeedTools::XmlHelper.try_xpaths(self.channel_node,
          ["schedule/intervaltime/@hour"], :select_result_value => true)
        update_frequency_minutes =
          FeedTools::XmlHelper.try_xpaths(self.channel_node,
          ["schedule/intervaltime/@min"], :select_result_value => true)
        update_frequency_seconds =
          FeedTools::XmlHelper.try_xpaths(self.channel_node,
          ["schedule/intervaltime/@sec"], :select_result_value => true)
        if !update_frequency_days.blank?
          @time_to_live = @time_to_live + update_frequency_days.to_i.day
        end
        if !update_frequency_hours.blank?
          @time_to_live = @time_to_live + update_frequency_hours.to_i.hour
        end
        if !update_frequency_minutes.blank?
          @time_to_live = @time_to_live +
            update_frequency_minutes.to_i.minute
        end
        if !update_frequency_seconds.blank?
          @time_to_live = @time_to_live + update_frequency_seconds.to_i
        end
        if @time_to_live == 0
          @time_to_live = self.configurations[:default_ttl].to_i
        end
      end
    end
  end
  if @time_to_live.nil? || @time_to_live == 0
    # Default to one hour
    @time_to_live = self.configurations[:default_ttl].to_i
  elsif self.configurations[:max_ttl] != nil &&
      self.configurations[:max_ttl] != 0 &&
      @time_to_live >= self.configurations[:max_ttl].to_i
    @time_to_live = self.configurations[:max_ttl].to_i
  end
  @time_to_live = @time_to_live.round
  return @time_to_live
end

#time_to_live=(new_time_to_live) ⇒ Object Also known as: ttl=

Sets the feed time to live



2217
2218
2219
2220
# File 'lib/feed_tools/feed.rb', line 2217

def time_to_live=(new_time_to_live)
  @time_to_live = new_time_to_live.round
  @time_to_live = 30.minutes if @time_to_live < 30.minutes
end

#titleObject

Returns the feed title



1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
# File 'lib/feed_tools/feed.rb', line 1082

def title
  if @title.nil?
    repair_entities = false
    title_node = FeedTools::XmlHelper.try_xpaths(self.channel_node, [
      "atom10:title",
      "atom03:title",
      "atom:title",
      "title",
      "dc:title",
      "channelTitle",
      "TITLE"
    ])
    @title = FeedTools::HtmlHelper.process_text_construct(title_node,
      self.feed_type, self.feed_version, [self.base_uri])
    if self.feed_type == "atom" ||
        self.configurations[:always_strip_wrapper_elements]
      @title = FeedTools::HtmlHelper.strip_wrapper_element(@title)
    end
    @title = nil if @title.blank?
    self.cache_object.title = @title unless self.cache_object.nil?
  end
  return @title
end

#title=(new_title) ⇒ Object

Sets the feed title



1107
1108
1109
1110
# File 'lib/feed_tools/feed.rb', line 1107

def title=(new_title)
  @title = new_title
  self.cache_object.title = new_title unless self.cache_object.nil?
end

#update!Object

Loads the feed from the remote url if the feed has expired from the cache or cannot be retrieved from the cache for some reason.



179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
# File 'lib/feed_tools/feed.rb', line 179

def update!
  # Don't do anything if this option is set
  return if self.configurations[:disable_update_from_remote]

  if !FeedTools.feed_cache.nil? &&
      !FeedTools.feed_cache.set_up_correctly?
    FeedTools.feed_cache.initialize_cache()
  end
  if !FeedTools.feed_cache.nil? &&
      !FeedTools.feed_cache.set_up_correctly?
    raise "Your feed cache system is incorrectly set up.  " +
      "Please see the documentation for more information."
  end
  if self.http_headers.blank? && !(self.cache_object.nil?) &&
      !(self.cache_object.http_headers.nil?)
    @http_headers = YAML.load(self.cache_object.http_headers)
    @http_headers = {} unless @http_headers.kind_of? Hash
  elsif self.http_headers.blank?
    @http_headers = {}
  end
  if self.expired? == false
    @live = false
  else
    load_remote_feed!
    
    # Handle autodiscovery
    if self.http_headers['content-type'] =~ /text\/html/ ||
        self.http_headers['content-type'] =~ /application\/xhtml\+xml/

      autodiscovered_url = nil
      ['atom', 'rss', 'rdf'].each do |type|
        autodiscovered_url =
          FeedTools::HtmlHelper.extract_link_by_mime_type(self.feed_data,
            "application/#{type}+xml")
        break unless autodiscovered_url.nil?
      end
      
      if autodiscovered_url != nil
        begin
          autodiscovered_url = FeedTools::UriHelper.resolve_relative_uri(
            autodiscovered_url, [self.href])
        rescue Exception
        end
        if self.href == autodiscovered_url
          raise FeedAccessError,
            "Autodiscovery loop detected: #{autodiscovered_url}"
        end
        self.feed_data = nil
        
        self.href = autodiscovered_url
        if FeedTools.feed_cache.nil?
          self.cache_object = nil
        else
          self.cache_object =
            FeedTools.feed_cache.find_by_href(autodiscovered_url)
        end
        self.update!
      else
        html_body = FeedTools::XmlHelper.try_xpaths(self.xml_document, [
          "html/body"
        ])
        if html_body != nil
          raise FeedAccessError,
            "#{self.href} does not appear to be a feed."
        end
      end
    else
      ugly_redirect = FeedTools::XmlHelper.try_xpaths(self.xml_document, [
        "redirect/newLocation/text()"
      ], :select_result_value => true)
      if !ugly_redirect.blank?
        if self.href == ugly_redirect
          raise FeedAccessError,
            "Ugly redirect loop detected: #{ugly_redirect}"
        end
        self.feed_data = nil
        self.href = ugly_redirect
        if FeedTools.feed_cache.nil?
          self.cache_object = nil
        else
          self.cache_object =
            FeedTools.feed_cache.find_by_href(ugly_redirect)
        end
        self.update!
      end
    end
    
    # Reset everything that needs to be reset.
    @xml_document = nil
    @encoding_from_feed_data = nil
    @root_node = nil
    @channel_node = nil
    @id = nil
    @title = nil
    @subtitle = nil
    @copyright = nil
    @link = nil
    @time_to_live = nil
    @entries = nil
    
    if self.configurations[:lazy_parsing_enabled] == false
      self.full_parse()
    end
  end
end

#updatedObject

Returns the feed updated time



1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
# File 'lib/feed_tools/feed.rb', line 1907

def updated
  if @updated.nil?
    updated_string = FeedTools::XmlHelper.try_xpaths(self.channel_node, [
      "atom10:updated/text()",
      "atom03:updated/text()",
      "atom:updated/text()",
      "updated/text()",
      "atom10:modified/text()",
      "atom03:modified/text()",
      "atom:modified/text()",
      "modified/text()",
      "lastBuildDate/text()"
    ], :select_result_value => true)
    unless updated_string.blank?
      @updated = Time.parse(updated_string).gmtime rescue nil
    else
      @updated = nil
    end
  end
  return @updated
end

#updated=(new_updated) ⇒ Object

Sets the feed updated time



1930
1931
1932
# File 'lib/feed_tools/feed.rb', line 1930

def updated=(new_updated)
  @updated = new_updated
end

#vidlog?Boolean

True if this feed contains video content enclosures

Returns:

  • (Boolean)


2509
2510
2511
2512
2513
2514
2515
2516
2517
# File 'lib/feed_tools/feed.rb', line 2509

def vidlog?
  vidlog = false
  self.items.each do |item|
    item.enclosures.each do |enclosure|
      vidlog = true if enclosure.video?
    end
  end
  return vidlog
end

#xml_documentObject

Returns a REXML Document of the feed_data



709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
# File 'lib/feed_tools/feed.rb', line 709

def xml_document
  if @xml_document.nil?
    return nil if self.feed_data.blank?
    if self.feed_data_type != :xml
      @xml_document = nil
    else
      begin
        @xml_document = REXML::Document.new(self.feed_data_utf_8)
      rescue Exception
        # Something failed, attempt to repair the xml with html5lib.
        begin
          @xml_document = HTML5::XMLParser.parse(self.feed_data_utf_8)
        rescue Exception
          # Failed again, give up.
          return nil
        end
      end
    end
  end
  return @xml_document
end