Module: Fed

Defined in:
lib/fed.rb,
lib/fed/http.rb,
lib/fed/feed/atom.rb,
lib/fed/feed/base.rb,
lib/fed/feed/rss1.rb,
lib/fed/feed/rss2.rb,
lib/fed/http/curb.rb,
lib/fed/feed/entry.rb,
lib/fed/http/errors.rb,
lib/fed/feed/enclosure.rb

Defined Under Namespace

Modules: Feed, Http

Constant Summary collapse

VERSION =
'0.0.5'

Class Method Summary collapse

Class Method Details

.fetch(feed_url) ⇒ Object



25
26
27
28
29
30
31
32
# File 'lib/fed.rb', line 25

def fetch(feed_url)
  response = Fed::Http.client.get(feed_url)
  
  raise Fed::Http::Errors::NotFound if response == 404
  raise Fed::Http::Errors::ServerError unless response.is_a?(String)

  response
end

.fetch_and_parse(feed_url) ⇒ Object



21
22
23
# File 'lib/fed.rb', line 21

def fetch_and_parse(feed_url)
  parse fetch(feed_url)
end


65
66
67
68
69
70
71
72
73
# File 'lib/fed.rb', line 65

def find_link_in_html(document)
  elems = document.css("link[type='application/atom+xml']")

  if elems.any?
    elems.first.attributes['href'].value
  else
    nil
  end
end

.is_atom?(document) ⇒ Boolean

Returns:

  • (Boolean)


53
54
55
# File 'lib/fed.rb', line 53

def is_atom?(document)
  document.css('feed entry').any?
end

.is_rss1?(document) ⇒ Boolean

Returns:

  • (Boolean)


57
58
59
# File 'lib/fed.rb', line 57

def is_rss1?(document)
  document.xpath('/rdf:RDF').any? rescue false
end

.is_rss2?(document) ⇒ Boolean

Returns:

  • (Boolean)


61
62
63
# File 'lib/fed.rb', line 61

def is_rss2?(document)
  document.css('rss channel').any?
end

.parse(xml) ⇒ Object



34
35
36
37
# File 'lib/fed.rb', line 34

def parse(xml)
  document = Nokogiri::XML(xml)
  parser_for(document).parse
end

.parser_for(doc) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/fed.rb', line 39

def parser_for(doc)
  if is_atom?(doc)
    Fed::Feed::Atom.new(doc)
  elsif is_rss2?(doc)
    Fed::Feed::Rss2.new(doc)
  elsif is_rss1?(doc)
    Fed::Feed::Rss1.new(doc)
  elsif (new_url = find_link_in_html(doc))
    parser_for(fetch(new_url))
  else
    raise Fed::Http::Errors::BadFeed  
  end
end