Class: OpenUriAndWrite::Usernames

Inherits:
Object
  • Object
show all
Defined in:
lib/open-uri-and-write/usernames.rb

Instance Method Summary collapse

Instance Method Details

#homedirObject



7
8
9
10
11
12
13
# File 'lib/open-uri-and-write/usernames.rb', line 7

def homedir
  if(Dir.respond_to?("home"))
    Dir.home
  else
    File.expand_path("~")
  end
end

#read_usernames_and_hostsObject



19
20
21
22
23
24
25
26
27
28
# File 'lib/open-uri-and-write/usernames.rb', line 19

def read_usernames_and_hosts
  usernames = {}
  if(File.exist?(usernamesfile))
    open(usernamesfile).readlines.each do |line|
      username, host = line.split(':')
      usernames[host.strip] = username.strip
    end
  end
  return usernames
end

#save_username_and_host(username, host) ⇒ Object



30
31
32
33
34
# File 'lib/open-uri-and-write/usernames.rb', line 30

def save_username_and_host(username,host)
  usernames = read_usernames_and_hosts
  usernames[host] = username
  store_username_and_host(usernames)
end

#store_username_and_host(usernames) ⇒ Object



36
37
38
39
40
41
42
# File 'lib/open-uri-and-write/usernames.rb', line 36

def store_username_and_host(usernames)
  file = open(usernamesfile, "w")
  usernames.keys.each do |key|
    file.puts "#{usernames[key]}:#{key}"
  end
  file.close
end

#username_for_host(host) ⇒ Object



44
45
46
47
# File 'lib/open-uri-and-write/usernames.rb', line 44

def username_for_host(host)
  usernames = read_usernames_and_hosts
  return usernames[host]
end

#usernamesfileObject



15
16
17
# File 'lib/open-uri-and-write/usernames.rb', line 15

def usernamesfile
  homedir + "/.open-uri-and-write-usernames"
end