Class: KindleMail::Application

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeApplication

Returns a new instance of Application.



12
13
14
15
16
17
# File 'lib/KindleMail.rb', line 12

def initialize
  puts "#{VERSION_STRING}\n\n"
  @datastore = KindleMailFileDatastore.new
  @config_manager = Configuration.new
  @config_manager.configuration_setup
end

Instance Attribute Details

#cmd_parserObject (readonly)

Returns the value of attribute cmd_parser.



9
10
11
# File 'lib/KindleMail.rb', line 9

def cmd_parser
  @cmd_parser
end

#optsObject (readonly)

Returns the value of attribute opts.



10
11
12
# File 'lib/KindleMail.rb', line 10

def opts
  @opts
end

Instance Method Details



145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# File 'lib/KindleMail.rb', line 145

def print_info
  puts "kindlemail was born out of my own laziness. To put things bluntly" 
  puts "I'm too lazy to pick up my beloved Kindle, get a usb cable, plug"
  puts "it into my computer, drag and drop books and documents to it,"
  puts "unplug the usb cable. Too ardous and involves getting up."
  puts
  puts "So I wrote this, it's simple, a bit rubbish, probably doesn't work" 
  puts "properly but it's useful when I just want to fire off a .mobi book" 
  puts "to my Kindle and forget about it until later."
  puts
  puts "Amazon have made a great service with the Personal Document Service," 
  puts "although it's worth reminding users of the 3G Kindle that they will" 
  puts "be charged for using this service"
  puts 
  puts "If you hate this application, supress your hatred and tell me what" 
  puts "you hate about it so I can change it"
  puts 
  puts "Version:                #{APP_VERSION}"
  puts "Homepage:               #{HOMEPAGE}" 
  puts "Author:                 #{AUTHOR}"
  puts 

  begin
    config = @config_manager.get_user_credentials
  rescue
    puts "You do not appear to have a valid user credentials file!"
  else
    puts "Default kindle address: #{config[:kindle_addr]}\n\n"
  end
end

#processObject



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/KindleMail.rb', line 47

def process
  begin

    if(@opts[:setup_given])
      hyphens = "-"*79
      puts hyphens
      puts "kindlemail setup"
      puts "This will overwrite any settings you have set previously"
      puts hyphens
      print "Kindle address to set as your default address> "
      @config_manager.set_default_kindle_address(gets.chomp.to_s) 

      puts 
      puts hyphens
      puts "Gmail Authentication Settings"
      puts "To get a valid anonymous token, use the instructions located here\nhttp://code.google.com/p/google-mail-xoauth-tools/wiki/XoauthDotPyRunThrough"
      puts hyphens
      print "OAUTH Token> "
      token = gets.chomp.to_s
      print "OAUTH Token Secret> "
      token_secret = gets.chomp.to_s
      print "Your gmail address> "
      email = gets.chomp.to_s

      @config_manager.set_email_credentials(token, token_secret, email)
      puts "\n\nkindlemail setup complete"
      exit
    end

    if(@opts[:show_info_given])
      print_info
      exit
    end

    if(@opts[:show_history_given])
      @datastore.print_history
      exit
    end

    if(@opts[:clear_history_given])
      do_it = false
      if(!@opts[:force_given])      
         print "Are you sure you wish to clear the history of files you have sent using kindlemail? [y/n]> "
         response = gets.to_s.chomp
         if(response.empty? or response.downcase.eql?("y") or response.downcase.eql?("yes"))
           do_it = true
         end
      else
        do_it = true
      end

      if(do_it)
        print "Clearing file history"
        @datastore.clear_history
        puts "...done"
      end
      exit
    end

    if ARGV.empty?
      raise ArgumentError, "Please specify a file to send (or use the -h option to see help)" 
    end

    mailer = KindleMailer.new(@config_manager.get_email_credentials)

    kindle_address = ""
    if(!@opts[:kindle_address_given])
      # no -k flag specified, see if a configuration file has been set
      if(File.exist?(USER_CONF_FILE))
        config = @config_manager.get_user_credentials
        kindle_address = config[:kindle_addr]
      else
        raise ArgumentError, "No address has been specified to send the item to.\nEither add an address in #{USER_CONF_FILE} or use the -kindle_address (-k) option"
      end
    else
      #User has specified the -k flag 
      kindle_address = @opts[:kindle_address]
    end

    force_send = @opts[:force_given] ? true : false 
    file_to_send = ARGV[0]

    if(!force_send)
      if(@datastore.file_exists?(kindle_address, File.basename(file_to_send))) 
        raise ArgumentError, "This file has already been sent to #{kindle_address}. Use the --force (-f) option if you want to resend it"
      end
    end

    send_result = mailer.send(kindle_address, file_to_send)
    @datastore.add_entry(kindle_address,File.basename(file_to_send)) if send_result

  rescue ArgumentError => message
    puts "#{message}"
  rescue => message
    puts "Error occured: - \n#{message}"
  end
end

#runObject



19
20
21
22
# File 'lib/KindleMail.rb', line 19

def run
  setup
  process
end

#setupObject



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/KindleMail.rb', line 24

def setup
  ban = ""
  ban << "kindlemail will send items to your kindle in the simplest possible manner"
  ban << "\n\nValid filetypes: -"
  VALID_FILE_TYPES.each { |key,val| ban << "\n  #{key} - #{val}" }
  ban << "\n\nUsage: -"
  ban << "\n  kindlemail [options] <filename>"
  ban << "\n\nExample usage: -"
  ban << "\n kindlemail my_book.mobi"
  ban << "\n\nWhere [options] are: -"

  @opts = Trollop::options do
    version VERSION_STRING
    banner ban
    opt :kindle_address, "Overrides the default kindle address to send items to", :short => "-k", :type => :string
    opt :force, "Send the file regardless of whether you have sent it before", :short => "-f", :default => nil
    opt :show_history, "Show the history of files that have been sent using kindlemail", :short => "-s", :default => nil
    opt :clear_history, "Clear the history of files that have been sent using kindlemail", :short => "-d", :default => nil
    opt :setup, "Setup kindlemail", :default => nil
    opt :show_info, "Show information about the way kindlemail is setup", :short => "-i", :default => nil
  end
end