Class: Mako::FeedFinder

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

Constant Summary collapse

XPATHS =

Patterns for RSS, Atom, and JSON feeds

["//link[@rel='alternate'][@type='application/rss+xml']/@href",
"//link[@rel='alternate'][@type='application/atom+xml']/@href",
"//link[@rel='alternate'][@type='application/json']/@href"].freeze
MIME_TYPES =
['text/xml',
'application/xml',
'application/rss+xml',
'application/atom+xml',
'application/json'].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ FeedFinder

Returns a new instance of FeedFinder.



17
18
19
# File 'lib/mako/feed_finder.rb', line 17

def initialize(args)
  @uris = args.fetch(:uris)
end

Instance Attribute Details

#urisObject (readonly)

Returns the value of attribute uris.



15
16
17
# File 'lib/mako/feed_finder.rb', line 15

def uris
  @uris
end

Instance Method Details

#findArray

From an array of supplied URIs, will request each one and attempt to find a feed URI on the page. If one is found, it will be added to an array and returned.

Returns:

  • (Array)


26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/mako/feed_finder.rb', line 26

def find
  request_uris.map do |request|
    if request[:body].nil?
      request[:uri]
    else
      html = Nokogiri::HTML(request[:body])
      potential_feed_uris = html.xpath(XPATHS.detect { |path| !html.xpath(path).empty? })
      if potential_feed_uris.empty?
        Mako.errors.add_error "Could not find feed for #{request[:uri]}"
        next
      end
      uri_string = potential_feed_uris.first.value
      feed_uri = URI.parse(uri_string)
      feed_uri.absolutize!(request[:uri])
    end
  end.compact
end