Class: FeedProcessor::Parser

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

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Parser

Returns a new instance of Parser.



7
8
9
10
# File 'lib/feed_processor/parser.rb', line 7

def initialize(options={})
  @options = options
  setup_mongo(options[:mongo])
end

Instance Method Details

#executeObject



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/feed_processor/parser.rb', line 11

def execute
  queue = Beanstalk::Pool.new(['localhost:11300'])
  puts "Now accepting feeds to parse..."
  begin
    loop do
      job = queue.reserve
      if job.body =~ /^START (.+)$/
        puts "starting #{$1} feeds"
      elsif job.body =~ /^END (.+)$/
        puts "finished #{$1} feeds"
      else
        url = job.body
        puts "parsing #{url}"
        responses = Response.all(:conditions => {'url' => url})
        responses.each do |response|
          begin
            feed = Feedzirra::Feed.parse(response.data)
            f = Feed.create({:url => url, :status => 'to-process'})
            entries = feed.entries
            puts "found #{entries.length} entries in #{url}"
            entries.each do |entry|
              begin
                f.contents.create({
                  :title => entry.title,
                  :url => entry.url,
                  :author => entry.author,
                  :summary => entry.summary,
                  :content => entry.content,
                  :published => entry.published,
                  :categories => entry.categories,
                })
              rescue => e
                puts "error creating entry #{entry.url}: #{e.message}"
              end
            end
          rescue => e
            puts "error parsing feed #{url}: #{e.message}"
          end
        end
      end
      job.delete
    end
  rescue Interrupt
    puts "Exiting parser"
  end
end