Class: ReadabilityImporter::Importer

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

Constant Summary collapse

MAX_URLS_IN_MAIL =
20.freeze

Instance Method Summary collapse

Constructor Details

#initialize(email_address, options = {}) ⇒ Importer

Returns a new instance of Importer.



8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/readability_importer/importer.rb', line 8

def initialize(email_address, options = {})
  @email_address = email_address

  @from = options[:from] || "foo@bar"
  @max_concurrency = options[:max_concurrency] || 4
  @verbose = !!options[:verbose]
  @retry = !!options[:retry]

  @on_importing = options[:on_importing]
  @on_imported = options[:on_imported]
  @on_failed = options[:on_failed]
end

Instance Method Details

#import(urls) ⇒ Object



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
# File 'lib/readability_importer/importer.rb', line 21

def import(urls)
  urls_set = []
  urls.each_slice(MAX_URLS_IN_MAIL){|urls| urls_set << urls}

  EventMachine.run do
    iterator = EventMachine::Iterator.new(urls_set, @max_concurrency)
    iterator.each(proc do |urls, it|
      @on_importing.call(urls) if @on_importing
      EventMachine::Protocols::SmtpClient.send({
        :verbose => @verbose,
        :domain => domain,
        :host   => host,
        :from   => @from,
        :to     => @email_address,
        :header => {"From" => @from, "To" => @email_address},
        :body   => urls.join("\r\n")
      }).tap do |job|
        job.callback do
          @on_imported.call(urls) if @on_imported
          it.next
        end
        job.errback do |*args|
          @on_failed.call(urls) if @on_failed
          if @retry
            iterator.instance_variable_get(:@list).unshift(urls)
          end
          it.next
        end
      end
    end, proc do
      EventMachine.stop
    end)
  end
end