pauldix (owner)

Revisions

  • 276e3a Fri Jan 16 06:51:06 -0800 2009
  • 38a164 Fri Jan 16 06:33:46 -0800 2009
  • d68589 Fri Jan 16 06:27:38 -0800 2009
gist: 47938 Download_button fork
public
Public Clone URL: git://gist.github.com/47938.git
Embed All Files: show embed
Ruby #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
module Feedzirra
  class AtomEntry
    include SAXMachine
    element :title
    # the :as argument makes this available through atom_entry.author instead of .name
    element :name, :as => :author
    element "feedburner:origLink", :as => :url
    element :summary
    element :content
    element :published
  end
  
  # Class for parsing Atom feeds
  class Atom
    include SAXMachine
    element :title
    # the :with argument means that you only match a link tag that has an attribute of :type => "text/html"
    # the :value argument means that instead of setting the value to the text between the tag,
    # it sets it to the attribute value of :href
    element :link, :value => :href, :as => :url, :with => {:type => "text/html"}
    element :link, :value => :href, :as => :feed_url, :with => {:type => "application/atom+xml"}
    elements :entry, :as => :entries, :class => AtomEntry
  end
end
 
# you can then parse like this
feed = Atom.parse(xml_text)
# then you're ready to rock
feed.title # => whatever the title of the blog is
feed.url # => the main url of the blog
feed.feed_url # => goes to the feedburner feed
 
feed.entries.first.title # => title of the first entry
feed.entries.first.author # => the author of the first entry
feed.entries.first.url # => the permalink on the blog for this entry
# etc ...