Class: Instapaper::ConfigFile

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

Instance Method Summary collapse

Constructor Details

#initialize(config_file = "~/.instapaper_download") ⇒ ConfigFile

Returns a new instance of ConfigFile.



9
10
11
12
13
# File 'lib/instapaper_download.rb', line 9

def initialize(config_file = "~/.instapaper_download")
  @config_file = File.expand_path(config_file)
  @config_hash = {}
  @save_to_file = nil
end

Instance Method Details

#create_or_updateObject



20
21
22
23
24
25
# File 'lib/instapaper_download.rb', line 20

def create_or_update
  File.open(@config_file, "w") do |file|
    file.write(YAML::dump(@config_hash))
  end
  puts %Q{Saved configuration to "#{@config_file}"}
end

#exists?Boolean

Returns:

  • (Boolean)


27
28
29
# File 'lib/instapaper_download.rb', line 27

def exists?
  File.exists?(@config_file) && File.file?(@config_file)      
end

#pathObject



31
32
33
# File 'lib/instapaper_download.rb', line 31

def path
  @config_file
end

#read_from_fileObject



15
16
17
18
# File 'lib/instapaper_download.rb', line 15

def read_from_file
  raise "Please make sure that #{@config_file} exists" unless exists?
  @config_hash = YAML::load_file(@config_file)
end

#save_to_file_if_necessary(hash) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/instapaper_download.rb', line 35

def save_to_file_if_necessary(hash)
  @config_hash = @config_hash.merge(hash) #Overwrites old values from @config_hash with new values from hash in case of identical keys
  puts
  while @save_to_file.nil? do
    print "Should I hold on to this information for you? Caution: The password is stored in plain text! [y/n] "
    response = gets.chomp.downcase
    if response == "y"
      @save_to_file = true
    elsif response == "n"
      @save_to_file = false
    end
  end
  if @save_to_file
    create_or_update
  end
end