Class: FeedNinja

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeFeedNinja

Returns a new instance of FeedNinja.



11
12
13
14
15
# File 'lib/feed_ninja/feed_ninja.rb', line 11

def initialize
  @limit = 4
  @writer = AtomIshWriter.new
  @ninja_prefix = "N! "
end

Instance Attribute Details

#limitObject

Returns the value of attribute limit.



9
10
11
# File 'lib/feed_ninja/feed_ninja.rb', line 9

def limit
  @limit
end

#picture_xpathObject

Returns the value of attribute picture_xpath.



9
10
11
# File 'lib/feed_ninja/feed_ninja.rb', line 9

def picture_xpath
  @picture_xpath
end

#text_xpathObject

Returns the value of attribute text_xpath.



9
10
11
# File 'lib/feed_ninja/feed_ninja.rb', line 9

def text_xpath
  @text_xpath
end

#title_regexObject

Returns the value of attribute title_regex.



9
10
11
# File 'lib/feed_ninja/feed_ninja.rb', line 9

def title_regex
  @title_regex
end

#uriObject

Returns the value of attribute uri.



9
10
11
# File 'lib/feed_ninja/feed_ninja.rb', line 9

def uri
  @uri
end

Instance Method Details

#fetch(url) ⇒ Object

get the feed and iterate over the entries



33
34
35
36
37
38
39
# File 'lib/feed_ninja/feed_ninja.rb', line 33

def fetch url
  open(url) do |feed|
    doc = RSS::Parser.parse(feed)
    initialize_writer(doc)
    process_items(doc)
  end
end

#initialize_writer(doc) ⇒ Object



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

def initialize_writer doc
  @writer.updated = DateTime.now.to_s

  case doc.feed_type
  when "atom"
    @writer.title = @ninja_prefix + doc.title.content
    @writer.link = doc.link.href
  when "rss"
    @writer.title = @ninja_prefix + doc.channel.title
    @writer.link = doc.channel.link
  else
    raise "Invalid feed format"
  end
end

#picture_at(*xpath) ⇒ Object

DSL convenience setters



84
85
86
# File 'lib/feed_ninja/feed_ninja.rb', line 84

def picture_at *xpath
  @picture_xpath = xpath
end

#process_item(original, feed_type, index) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/feed_ninja/feed_ninja.rb', line 53

def process_item(original, feed_type, index)
  @writer.new_entry(index) do |entry|
    extractor = Extractor.new
    case feed_type
    when "atom"
      entry.title = original.title.content
      entry.link = original.link.href
      entry.updated = original.updated
      entry.id = original.id
      extractor.fetch original.link.href
    when "rss"
      entry.title = original.title
      entry.link = original.link
      entry.updated = original.pubDate ? original.pubDate.xmlschema : DateTime.now.to_s
      entry.id = entry.link
      extractor.fetch original.link
    end

    entry.images = extractor.extract_images @picture_xpath
    entry.summary = extractor.extract_xml @text_xpath

    entry #it's kind of fishy to explicitly have to return the entry here...
  end
end

#process_items(doc) ⇒ Object



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

def process_items doc
  items = doc.items
  if title_regex
    items = items.select { |item| title_regex =~ item.title }
  end
  threads = []
  items.first(@limit).each_with_index do |item, index|
    threads << Thread.new { process_item(item, doc.feed_type, index) }
  end
  ThreadsWait.all_waits(*threads)
end

#text_at(*xpath) ⇒ Object



88
89
90
# File 'lib/feed_ninja/feed_ninja.rb', line 88

def text_at *xpath
  @text_xpath = xpath
end

#title_matches(regex) ⇒ Object



92
93
94
# File 'lib/feed_ninja/feed_ninja.rb', line 92

def title_matches regex
  @title_regex = regex
end

#to_sObject



78
79
80
# File 'lib/feed_ninja/feed_ninja.rb', line 78

def to_s
  @writer.to_s
end