Module: Utilities

Included in:
Profile::CLI
Defined in:
lib/git/profile.rb

Instance Method Summary collapse

Instance Method Details

#fetch_user_dataObject



18
19
20
21
22
23
24
25
26
27
28
# File 'lib/git/profile.rb', line 18

def fetch_user_data
  username = ask("enter your username:")
  email = ask("enter your email:")

  unless email =~ /\A[\w+\-.]+@[a-z\d\-]+(\.[a-z\d\-]+)*\.[a-z]+\z/i 
    say("Invalid email address, Try Again! \n")
    self.fetch_user_data
  end

  return [username, email]
end

#save(username, email) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/git/profile.rb', line 30

def save(username, email)
  profiles = File.join(Dir.home, "/.git-profile/profiles.yml")
  if File.zero?(profiles)
    users = {users: [{username: "#{username}", email: "#{email}"}]}
    File.open(profiles, "w") { |file| file.write(users.to_yaml) }
  else
    if user_exists?(email)
      say("User already exists!")
      exit
    end
    users = YAML.load(File.read(profiles))
    user = {username: "#{username}", email: "#{email}"}
    users[:users].push(user)
    File.open(profiles, "w") { |file| file.write(users.to_yaml) }
  end
  say("User #{username} <#{email}> has been added!")
end

#user_exists?(email) ⇒ Boolean

Returns:

  • (Boolean)


7
8
9
10
11
12
13
14
15
16
# File 'lib/git/profile.rb', line 7

def user_exists?(email)
  profiles = File.join(Dir.home, "/.git-profile/profiles.yml")
  users = YAML.load(File.read(profiles))

  emails = users[:users].filter do |user| 
    user[:email] == email
  end
  
  emails.empty? ? false : true
end