Class: FeedProcessor::Parser

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Parser

Returns a new instance of Parser.



9
10
11
12
13
# File 'lib/feed_processor/parser.rb', line 9

def initialize(options={})
  @options = options
  @feed_handler = options[:feed_handler] || FeedProcessor::MongoFeedHandler.new
  setup_mongo(options[:mongo])
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



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

def options
  @options
end

Instance Method Details

#executeObject



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
# File 'lib/feed_processor/parser.rb', line 14

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)
            @feed_handler.process(url, feed)
          rescue => e
            puts "error parsing feed #{url}: #{e.message}"
          end
        end
      end
      job.delete
    end
  rescue Interrupt
    puts "Exiting parser"
  end
end