Class: FeedReader::Reader

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(uri) ⇒ Reader

Returns a new instance of Reader.



10
11
12
13
14
15
16
# File 'lib/feed_reader.rb', line 10

def initialize(uri)
  @doc = Nokogiri::XML(open(uri))
  @list = []
  @doc.css('item').each do |item|
    @list << [ item.at_css('title').text, ActionController::Base.helpers.sanitize(item.at_css('description').text,:tags=>[]), Nokogiri::HTML(item.css('description').text).at_css('img').attributes['src'].value, item.at_css('link').text ] # title, description, image, link
  end
end

Instance Attribute Details

#listObject

Returns the value of attribute list.



8
9
10
# File 'lib/feed_reader.rb', line 8

def list
  @list
end

Instance Method Details

#filter(inc_words, exc_words = []) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/feed_reader.rb', line 18

def filter( inc_words, exc_words=[] )
  template = '\W(?)\W'
  inc = template.gsub '?', inc_words.join('|')
  exc = template.gsub '?', exc_words.join('|')
  list = []
  @list.each do |item|
    str = " #{item[0]} #{item[1]} "
    if inc_words.empty? || !(str =~ Regexp.new(inc,'i')).nil?
      if exc_words.empty? || (str =~ Regexp.new(inc,'i')).nil?
        list<<item
      end
    end
  end
  list
end