Class: KindleMail::Configuration

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

Instance Method Summary collapse

Instance Method Details

#configuration_setupObject

Create the user framework needed to run the application



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/KindleMail.rb', line 16

def configuration_setup
  dirname = File.expand_path(USER_DIR)
  if !File.exists?(dirname)
    Dir.mkdir(dirname) 
    create_storage_dir
    create_staging_dir
    create_user_conf_file
    create_user_email_conf_file
  else     
    create_user_conf_file if !File.exists?(USER_CONF_FILE)
    create_storage_dir if !File.exists?(File.expand_path(STORAGE_DIR))
    create_staging_dir if !File.exists?(File.expand_path(STAGING_DIR))
    create_user_email_conf_file if !File.exists?(EMAIL_CONF_FILE)
  end
end

#create_staging_dirObject



36
37
38
# File 'lib/KindleMail.rb', line 36

def create_staging_dir
  Dir.mkdir(File.expand_path(STAGING_DIR))
end

#create_storage_dirObject



32
33
34
# File 'lib/KindleMail.rb', line 32

def create_storage_dir
  Dir.mkdir(File.expand_path(STORAGE_DIR))
end

#create_user_conf_fileObject



40
41
42
43
44
# File 'lib/KindleMail.rb', line 40

def create_user_conf_file
  root = File.expand_path(File.dirname(__FILE__))
  root = File.expand_path("../conf_templates", root)
  FileUtils.cp(File.join(root, '/.kindlemail'), USER_CONF_FILE)
end

#create_user_email_conf_fileObject



46
47
48
49
50
51
# File 'lib/KindleMail.rb', line 46

def create_user_email_conf_file
  puts "Creating user email conf file"
  root = File.expand_path(File.dirname(__FILE__))
  root = File.expand_path("../conf_templates", root)
  FileUtils.cp(File.join(root, '/.email_conf'), EMAIL_CONF_FILE)
end

#get_email_credentialsObject

Raises:

  • (ArgumentError)


53
54
55
56
57
58
59
60
# File 'lib/KindleMail.rb', line 53

def get_email_credentials
  raise ArgumentError, "Cannot find email credentials file #{EMAIL_CONF_FILE}." if !File.exists?(EMAIL_CONF_FILE)
  begin
    load_yaml(EMAIL_CONF_FILE)
  rescue
    raise StandardError, "Error parsing #{EMAIL_CONF_FILE}"
  end
end

#get_user_credentialsObject

Raises:

  • (ArgumentError)


62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/KindleMail.rb', line 62

def get_user_credentials
  error_msg =  "The configuration file #{USER_CONF_FILE} was found but appears to be invalid/incomplete.\nThe most likely reason for this is the fact that you need to set a default kindle address to send items to.\nYou must edit the file and follow the instructions in the comments before trying again. Alternatively use the -k flag to specify a kindle address to send the item to" 

  raise ArgumentError, "Cannot find user credentials file #{USER_CONF_FILE}." if !File.exists?(USER_CONF_FILE)
  begin
    config = load_yaml(USER_CONF_FILE)
  rescue
   raise StandardError, error_msg
  end

  raise StandardError, error_msg if config.key?(:kindle_addr) == false || config[:kindle_addr].nil?
  return config
end

#load_yaml(file) ⇒ Object



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

def load_yaml(file)
  YAML.load_file(file).inject({}){|memo,(k,v)| memo[k.to_sym] = v.to_s; memo}
end