Class: ReadabilityImporter::Importer
- Inherits:
-
Object
- Object
- ReadabilityImporter::Importer
- Defined in:
- lib/readability_importer/importer.rb
Constant Summary collapse
- MAX_URLS_IN_MAIL =
20.freeze
Instance Method Summary collapse
- #import(urls) ⇒ Object
-
#initialize(email_address, options = {}) ⇒ Importer
constructor
A new instance of Importer.
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, = {}) @email_address = email_address @from = [:from] || "foo@bar" @max_concurrency = [:max_concurrency] || 4 @verbose = !![:verbose] @retry = !![:retry] @on_importing = [:on_importing] @on_imported = [:on_imported] @on_failed = [: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 |