Class: FeedParser::Feed

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(feed_url, http_options = {}) ⇒ Feed

Returns a new instance of Feed.



5
6
7
8
9
10
11
12
13
# File 'lib/feed_parser/feed.rb', line 5

def initialize(feed_url, http_options = {})
  @http_options = http_options
  raw_feed = open_or_follow_redirect(feed_url)
  @feed = Nokogiri::XML(raw_feed)
  @feed.remove_namespaces!
  @type = ((@feed.xpath('/rss')[0] && :rss) || (@feed.xpath('/feed')[0] && :atom))
  raise FeedParser::UnknownFeedType.new("Feed is not an RSS feed or an ATOM feed") unless @type
  self
end

Instance Attribute Details

#typeObject (readonly)

Returns the value of attribute type.



3
4
5
# File 'lib/feed_parser/feed.rb', line 3

def type
  @type
end

Instance Method Details

#as_jsonObject



40
41
42
43
44
45
46
# File 'lib/feed_parser/feed.rb', line 40

def as_json
  {
    :title => title,
    :url => url,
    :items => items.map(&:as_json)
  }
end

#itemsObject



32
33
34
35
36
37
38
# File 'lib/feed_parser/feed.rb', line 32

def items
  klass = (@type == :rss && RssItem || AtomItem)

  @items ||= @feed.xpath(Dsl[@type][:item]).map do |item|
    klass.new(item)
  end
end

#titleObject



15
16
17
# File 'lib/feed_parser/feed.rb', line 15

def title
  @title = @feed.xpath(Dsl[@type][:title]).text
end

#urlObject



19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/feed_parser/feed.rb', line 19

def url
  _url = case @type
    when :rss
      @feed.xpath(Dsl[@type][:url])
    when :atom
      @feed.xpath(Dsl[@type][:url]).first && @feed.xpath(Dsl[@type][:url]).attribute("href") ||
      @feed.xpath(Dsl[@type][:alternate_url]).first && @feed.xpath(Dsl[@type][:alternate_url]).attribute("href")
    else
      nil
  end
  @url = _url && _url.text || ""
end