Class: KindleMailer
- Inherits:
-
Object
- Object
- KindleMailer
- Defined in:
- lib/KindleMailer.rb
Instance Attribute Summary collapse
-
#email_credentials ⇒ Object
Returns the value of attribute email_credentials.
-
#kindle_address ⇒ Object
Returns the value of attribute kindle_address.
Instance Method Summary collapse
- #create_filename(file) ⇒ Object
-
#initialize(email_credentials) ⇒ KindleMailer
constructor
A new instance of KindleMailer.
- #send(kindle_address, file) ⇒ Object
- #stage_file(filepath) ⇒ Object
- #validate_file_path(filepath) ⇒ Object
- #validate_kindle_address(addr) ⇒ Object
Constructor Details
#initialize(email_credentials) ⇒ KindleMailer
Returns a new instance of KindleMailer.
8 9 10 11 |
# File 'lib/KindleMailer.rb', line 8 def initialize(email_credentials) raise ArgumentError, "You must supply email credentials to use KindleMailer" if email_credentials.nil? @email_credentials = email_credentials end |
Instance Attribute Details
#email_credentials ⇒ Object
Returns the value of attribute email_credentials.
7 8 9 |
# File 'lib/KindleMailer.rb', line 7 def email_credentials @email_credentials end |
#kindle_address ⇒ Object
Returns the value of attribute kindle_address.
6 7 8 |
# File 'lib/KindleMailer.rb', line 6 def kindle_address @kindle_address end |
Instance Method Details
#create_filename(file) ⇒ Object
64 65 66 |
# File 'lib/KindleMailer.rb', line 64 def create_filename(file) new_filename = Digest::MD5.file(file).to_s+"_"+rand(1000000).to_s+File.extname(file) end |
#send(kindle_address, file) ⇒ Object
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 |
# File 'lib/KindleMailer.rb', line 13 def send(kindle_address, file) begin validate_kindle_address(kindle_address) @kindle_address = kindle_address filepath = File.(file) validate_file_path(filepath) puts "Preparing #{File.basename(filepath)} to be sent to #{@kindle_address}" if(File.extname(filepath).eql?(".mobi")) filepath = stage_file(filepath) end = GmailMailer::Message.new(@kindle_address) .(filepath) mailer = GmailMailer::Mailer.new(@email_credentials) mailer.send() rescue raise ensure if(!filepath.nil? and File.exist?(filepath)) FileUtils.rm(filepath) if(File.extname(filepath).eql?(".mobi")) end end puts "#{File.basename(file)} was successfully sent to #{@kindle_address}" return true end |
#stage_file(filepath) ⇒ Object
57 58 59 60 61 62 |
# File 'lib/KindleMailer.rb', line 57 def stage_file(filepath) new_filename=create_filename(filepath) new_location = File.(STAGING_DIR + "/" + new_filename) FileUtils.cp(filepath, new_location) new_location end |
#validate_file_path(filepath) ⇒ Object
45 46 47 48 49 |
# File 'lib/KindleMailer.rb', line 45 def validate_file_path(filepath) raise ArgumentError, "The file you have specified does not exist #{SEE_HELP}" if filepath.nil? || !File.exist?(filepath) raise ArgumentError, "The file you have specified is not a valid type #{SEE_HELP}" if VALID_FILE_TYPES.include?(File.extname(filepath)) == false return true end |
#validate_kindle_address(addr) ⇒ Object
51 52 53 54 55 |
# File 'lib/KindleMailer.rb', line 51 def validate_kindle_address(addr) raise ArgumentError, "You must supply an address to send this item to" if addr.nil? raise ArgumentError, "#{addr} does not appear to be a valid kindle address" if !addr.end_with?("@kindle.com") return true end |