Class: Feedbase::FeedListener

Inherits:
Object
  • Object
show all
Includes:
REXML::StreamListener
Defined in:
lib/feedbase/feed_parser.rb

Constant Summary collapse

FEED_TITLE_TAGS =
%w[ feed/title rss/channel/title rdf:RDF/channel/title ]
%w[ rss/channel/link rdf:RDF/channel/link ]
ITEM_START_TAGS =
%w[ feed/entry rss/channel/item rdf:RDF/item ]
ITEM_TITLE_TAGS =
%w[ feed/entry/title rss/channel/item/title rdf:RDF/item/title ]
ITEM_AUTHOR_TAGS =
%w[ feed/entry/author/name rss/channel/item/author rdf:RDF/item/dc:creator ]
ITEM_GUID_TAGS =
%w[ feed/entry/id rss/channel/item/guid rdf:RDF/item/guid rdf:RDF/item/feedburner:origLink ]
ITEM_PUB_DATE_TAGS =
%w[ feed/entry/published feed/entry/created feed/entry/modified rss/channel/item/pubDate rdf:RDF/item/dc:date ]
%w[ rss/channel/item/link rdf:RDF/item/link ]
ITEM_SUMMARY_TAGS =
%w[ feed/entry/summary rss/channel/item/description rdf:RDF/item/description ]
ITEM_CONTENT_TAGS =
[ %r{feed/entry/content}, %r{rss/channel/item/content}, %r{rss/channel/item/content:encoded},  %r{rss/item/content}, %r{rdf:RDF/item/content} ]

Instance Method Summary collapse

Constructor Details

#initializeFeedListener

Returns a new instance of FeedListener.



63
64
65
66
# File 'lib/feedbase/feed_parser.rb', line 63

def initialize
  @nested_tags = []
  @x = {:items => []}
end

Instance Method Details

#encode(string) ⇒ Object

encoding method



138
139
140
# File 'lib/feedbase/feed_parser.rb', line 138

def encode(string)
  string
end

#pathObject



132
133
134
# File 'lib/feedbase/feed_parser.rb', line 132

def path
  @nested_tags.join('/')
end

#resultObject



68
# File 'lib/feedbase/feed_parser.rb', line 68

def result; @x; end

#tag_end(name) ⇒ Object



82
83
84
85
86
87
88
89
# File 'lib/feedbase/feed_parser.rb', line 82

def tag_end(name)
  case path
  when *ITEM_START_TAGS
    @x[:items] << @current_item
    @current_item = nil
  end
  @nested_tags.pop
end

#tag_start(name, attrs) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
# File 'lib/feedbase/feed_parser.rb', line 70

def tag_start(name, attrs)
  @nested_tags.push name
  case path
  when 'feed/link'
    @x[:link] = encode attrs['href']
  when *ITEM_START_TAGS
    @current_item = {}
  when 'feed/entry/link'
    @current_item[:link] = encode attrs['href']
  end
end

#text(text) ⇒ Object Also known as: cdata



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/feedbase/feed_parser.rb', line 91

def text(text)
  case path
  when *FEED_TITLE_TAGS
    @x[:title] = encode text.strip
  when *FEED_LINK_TAGS 
    @x[:link] = encode text.strip
  when *ITEM_TITLE_TAGS 
    @current_item[:title] = encode(text.strip)
  when *ITEM_AUTHOR_TAGS 
    @current_item[:author] = encode(text.strip)
  when *ITEM_GUID_TAGS 
    @current_item[:guid] = encode(text)
  when *ITEM_PUB_DATE_TAGS
    @current_item[:pub_date] = DateTime.parse(encode(text))
  when *ITEM_LINK_TAGS 
    @current_item[:link] = encode(text)
  when *ITEM_SUMMARY_TAGS 
    if @current_item[:summary] 
      @current_item[:summary] << encode(text)
    else
      @current_item[:summary] = encode(text)
    end
  when *ITEM_CONTENT_TAGS
    if @current_item[:content] 
      @current_item[:content]  << encode(text)
    else
      @current_item[:content] = encode(text)
    end
  end
end

#xmldecl(decl, encoding, extra) ⇒ Object



123
124
125
126
127
128
129
# File 'lib/feedbase/feed_parser.rb', line 123

def xmldecl(decl, encoding, extra)
  if encoding 
    @x[:orig_encoding] = encoding.downcase
  else
    @x[:orig_encoding] = "UTF-8"
  end
end