Class: Flannel::FeedParser

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

Instance Method Summary collapse

Constructor Details

#initialize(cache = nil) ⇒ FeedParser

Returns a new instance of FeedParser.



5
6
7
# File 'lib/flannel/feed_parser.rb', line 5

def initialize cache=nil
  @cache = cache
end

Instance Method Details

#format_item(link, title) ⇒ Object



24
25
26
# File 'lib/flannel/feed_parser.rb', line 24

def format_item(link, title)
  "  <li>\n    <a href='#{link}'>#{title}</a>\n  </li>\n"
end

#format_url(url) ⇒ Object



14
15
16
17
18
# File 'lib/flannel/feed_parser.rb', line 14

def format_url url
  url.strip!
  url = "http://#{url}" if url[0..6] != "http://"
  url
end

#get_document(url) ⇒ Object



20
21
22
# File 'lib/flannel/feed_parser.rb', line 20

def get_document url
  URI.parse(url).read
end

#get_items(text) ⇒ Object



49
50
51
52
53
54
55
# File 'lib/flannel/feed_parser.rb', line 49

def get_items text
  items = text[/<item>.*<\/item>/mi]
  
  return [] unless items
  
  items.split(/<\/?item>/).reject { |item| /\A\s*\z/ =~ item }
end

#get_news(url) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/flannel/feed_parser.rb', line 28

def get_news url
  item_string = nil
  item_string = @cache.retrieve(url) if @cache
  
  unless item_string 
    item_string = ""
    doc = get_document(url)
    items = get_items(doc)

    items.each do |item|
      link = inner_html(item, "link")
      title = inner_html(item, "title")
      item_string << format_item(link, title)
    end
    
    @cache.save url, item_string if @cache
  end
  
  item_string
end

#inner_html(text, tag) ⇒ Object



57
58
59
60
61
62
63
64
# File 'lib/flannel/feed_parser.rb', line 57

def inner_html text, tag
  regex = Regexp.compile "<#{tag}>(.*)<\/#{tag}>?"

  matches = regex.match text
  return "" unless matches
  
  matches.captures[0]
end

#sub_feeds(url) ⇒ Object



9
10
11
12
# File 'lib/flannel/feed_parser.rb', line 9

def sub_feeds(url)
  url = format_url(url)
  get_news(url)
end