Class: Autodiscovery

Inherits:
Object
  • Object
show all
Defined in:
lib/kindle-feeds.rb

Instance Method Summary collapse

Constructor Details

#initialize(page_html) ⇒ Autodiscovery

Returns a new instance of Autodiscovery.



69
70
71
72
# File 'lib/kindle-feeds.rb', line 69

def initialize(page_html)
  # Downcase the html because capitalized stuff might mess up the Hpricot matching
  @doc = Hpricot(page_html)
end

Instance Method Details

#discoverObject

Returns the url of the feed, or nil if none found



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/kindle-feeds.rb', line 75

def discover
  # Look for rss link, e.g.
  # <link rel="alternate" type="application/rss+xml" title="RSS"
  #       href="http://feeds.feedburner.com/TheRssBlog">
  # Tricky: Hpricot CSS attribute selectors are written like XPath selectors
  [:atom, :rss].each do |flavor|
    if x=@doc.at("head link[@type=application/#{flavor}+xml]")
      return x[:href]
    end
  end
  if x=@doc.at("head link[@type=text/xml]")
    return x[:href]
  end
  return nil
end