Class: Configuration

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

Instance Method Summary collapse

Instance Method Details

#configuration_setupObject

Create the user framework needed to run the application



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/Configuration.rb', line 10

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



30
31
32
# File 'lib/Configuration.rb', line 30

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

#create_storage_dirObject



26
27
28
# File 'lib/Configuration.rb', line 26

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

#create_user_conf_fileObject



34
35
36
37
38
# File 'lib/Configuration.rb', line 34

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



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

def create_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)


70
71
72
73
74
75
76
77
# File 'lib/Configuration.rb', line 70

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)


79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/Configuration.rb', line 79

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



5
6
7
# File 'lib/Configuration.rb', line 5

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

#set_default_kindle_address(address) ⇒ Object

Raises:

  • (ArgumentError)


46
47
48
49
50
51
52
53
54
# File 'lib/Configuration.rb', line 46

def set_default_kindle_address(address)
  raise ArgumentError, "Error: No email address entered" if address.nil? or address.empty?

  print "Setting up kindle credentials..."
  File.open(USER_CONF_FILE,"w") do |file|
    file.puts "kindle_addr: #{address}"
  end
  puts "Complete!"
end

#set_email_credentials(token, token_secret, email) ⇒ Object

Raises:

  • (ArgumentError)


56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/Configuration.rb', line 56

def set_email_credentials(token, token_secret, email)
  raise ArgumentError, "Error: Please provide a valid OAUTH token" if token.nil? or token.empty?
  raise ArgumentError, "Error, Please provide a valid OAUTH token secret"  if token_secret.nil? or token.empty?
  raise ArgumentError, "Error: Please provide a valid gmail address" if email.nil? or email.empty?
  print "Setting up email credentials..."
  File.open(EMAIL_CONF_FILE, "w") do |file|
    file.puts "smtp_oauth_token: #{token}"
    file.puts "smtp_oauth_token_secret: #{token_secret}"
    file.puts "email #{email}"
  end
  puts "Complete!"
end