Class: Autodiscovery

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

Instance Method Summary collapse

Constructor Details

#initialize(page_html) ⇒ Autodiscovery

Returns a new instance of Autodiscovery.



5
6
7
8
# File 'lib/autodiscovery.rb', line 5

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



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/autodiscovery.rb', line 11

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
  [:rss, :atom].each do |flavor|
    if x=@doc.at("head link[@type=application/#{flavor}+xml]")
      puts "Found feed link #{x}"
      return x[:href]
    end
  end
  if x=@doc.at("head link[@type=text/xml]")
    puts "Found feed link #{x}"
    return x[:href]
  end
  return nil
end