Class: KindleMail::Application

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeApplication



81
82
83
84
85
86
# File 'lib/KindleMail.rb', line 81

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

Instance Attribute Details

#cmd_parserObject (readonly)

Returns the value of attribute cmd_parser.



78
79
80
# File 'lib/KindleMail.rb', line 78

def cmd_parser
  @cmd_parser
end

#optsObject (readonly)

Returns the value of attribute opts.



79
80
81
# File 'lib/KindleMail.rb', line 79

def opts
  @opts
end

Instance Method Details



185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
# File 'lib/KindleMail.rb', line 185

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



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
144
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
175
176
177
178
179
180
181
182
183
184
# File 'lib/KindleMail.rb', line 115

def process
  begin
    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



88
89
90
91
# File 'lib/KindleMail.rb', line 88

def run
  setup
  process
end

#setupObject



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/KindleMail.rb', line 93

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 :show_info, "Show information about the way kindlemail is setup", :short => "-i", :default => nil
  end
end