Class: Greyatom::User

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

Constant Summary collapse

DEFAULT_EDITOR =
'atom'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeUser

Returns a new instance of User.



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

def initialize()
  	@netrc = Greyatom::NetrcInteractor.new()
end

Instance Attribute Details

#netrcObject (readonly)

Returns the value of attribute netrc.



8
9
10
# File 'lib/greyatom/user.rb', line 8

def netrc
  @netrc
end

Instance Method Details

#confirmAndResetObject



68
69
70
71
72
73
74
75
76
77
78
# File 'lib/greyatom/user.rb', line 68

def confirmAndReset
  	if confirmReset?
    	netrc.delete!(machine: 'ga-config')
    	netrc.delete!(machine: 'ga-extra')
    	puts "Sorry to see you go!"
   	else
   		puts "Thanks for being there with us!"
  	end

  	exit
end

#confirmReset?Boolean

Returns:

  • (Boolean)


80
81
82
83
84
85
86
87
# File 'lib/greyatom/user.rb', line 80

def confirmReset?
  	puts "This will remove your existing login configuration and reset.\n"
  	print "Are you sure you want to do this? [yN]: "

  	response = STDIN.gets.chomp.downcase

  	!!(response == 'yes' || response == 'y')
end

#save(userDetails, token) ⇒ Object



48
49
50
51
52
53
54
# File 'lib/greyatom/user.rb', line 48

def save(userDetails, token)
	username = userDetails.fetch('username')
	github_uid = userDetails.fetch('github_uid')
	netrc.write(new_login: 'greyatom', new_password: token)
	netrc.write(machine: 'ga-extra', new_login: username, new_password: github_uid)
	welcome(username)
end

#setDefaultWorkspaceObject



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

def setDefaultWorkspace
	workspaceDir = File.expand_path('~/Workspace/code')
	configPath = File.expand_path('~/.ga-config')

	FileUtils.mkdir_p(workspaceDir)
	FileUtils.touch(configPath)

	data = YAML.dump({ workspace: workspaceDir, editor: DEFAULT_EDITOR })

	File.write(configPath, data)
end

#validate(token) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/greyatom/user.rb', line 16

def validate(token)
	puts "Authenticating..."
	begin
		Timeout::timeout(15) do
			response = Greyatom::API.new().get('/bins/c4vbn')
			if response.status == 200
				# Save valid user details in netrc
				user = JSON.parse(response.body)
				, password = netrc.read
				if .nil? || password.nil?
					save(user, token)
				else
					username = user.fetch('username')
					welcome(username)
				end
			else
				case response.status
	            when 401
	              puts "It seems your OAuth token is incorrect. Please retry with correct token."
	              exit 1
	            else
	              puts "Something went wrong. Please try again."
	              exit 1
	            end
			end
		end
	rescue Timeout::Error
		puts "Please check your internet connection."
		exit
	end
end

#welcome(username) ⇒ Object



89
90
91
# File 'lib/greyatom/user.rb', line 89

def welcome(username)
	puts "Welcome, #{username}!"
end