Class: KindleMailer

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(email_credentials) ⇒ KindleMailer

Returns a new instance of KindleMailer.

Raises:

  • (ArgumentError)


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_credentialsObject

Returns the value of attribute email_credentials.



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

def email_credentials
  @email_credentials
end

#kindle_addressObject

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.expand_path(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

    message = GmailMailer::Message.new(@kindle_address)
    message.add_attachment(filepath)
    
    mailer = GmailMailer::Mailer.new(@email_credentials)
    mailer.send(message)
  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.expand_path(STAGING_DIR + "/" + new_filename)
  FileUtils.cp(filepath, new_location)
  new_location
end

#validate_file_path(filepath) ⇒ Object

Raises:

  • (ArgumentError)


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

Raises:

  • (ArgumentError)


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