Class: Transcriptic::Auth
- Inherits:
-
Object
- Object
- Transcriptic::Auth
show all
- Extended by:
- Thor::Shell, Helpers
- Defined in:
- lib/transcriptic/auth.rb
Class Attribute Summary collapse
Class Method Summary
collapse
Methods included from Helpers
json_decode, json_encode, running_on_a_mac?, running_on_windows?
Class Attribute Details
.credentials ⇒ Object
Returns the value of attribute credentials.
6
7
8
|
# File 'lib/transcriptic/auth.rb', line 6
def credentials
@credentials
end
|
Class Method Details
.api_key ⇒ Object
52
53
54
55
|
# File 'lib/transcriptic/auth.rb', line 52
def api_key get_credentials
@credentials[1]
end
|
.ask_for_and_save_credentials ⇒ Object
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
|
# File 'lib/transcriptic/auth.rb', line 122
def ask_for_and_save_credentials
begin
@credentials = ask_for_credentials
write_credentials
check
rescue ::RestClient::Unauthorized, ::RestClient::ResourceNotFound => e
delete_credentials
clear
say "Authentication failed."
retry if retry_login?
exit 1
rescue Exception => e
delete_credentials
raise e
end
end
|
.ask_for_credentials ⇒ Object
85
86
87
88
89
90
91
92
93
|
# File 'lib/transcriptic/auth.rb', line 85
def ask_for_credentials
puts "Enter your Transcriptic credentials."
user = ask "Email: "
password = running_on_windows? ? ask_for_password_on_windows : ask_for_password
api_key = Transcriptic::Client.auth(user, password, host)['api_key']
[user, api_key]
end
|
.ask_for_password ⇒ Object
114
115
116
117
118
119
120
|
# File 'lib/transcriptic/auth.rb', line 114
def ask_for_password
echo_off
password = ask "Password: "
puts
echo_on
return password
end
|
.ask_for_password_on_windows ⇒ Object
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
|
# File 'lib/transcriptic/auth.rb', line 95
def ask_for_password_on_windows
require "Win32API"
char = nil
password = ''
print "Password: "
while char = Win32API.new("crtdll", "_getch", [ ], "L").Call do
break if char == 10 || char == 13 if char == 127 || char == 8 password.slice!(-1, 1)
else
(password << char.chr) rescue RangeError
end
end
puts
return password
end
|
.check ⇒ Object
just a stub; will raise if not authenticated
31
32
33
|
# File 'lib/transcriptic/auth.rb', line 31
def check
client.list_runs
end
|
.clear ⇒ Object
25
26
27
28
|
# File 'lib/transcriptic/auth.rb', line 25
def clear
@credentials = nil
@client = nil
end
|
.client ⇒ Object
8
9
10
11
12
13
14
|
# File 'lib/transcriptic/auth.rb', line 8
def client
@client ||= begin
client = Transcriptic::Client.new(user, api_key, host)
client.on_warning { |msg| self.display("\n#{msg}\n\n") }
client
end
end
|
.credentials_file ⇒ Object
.default_host ⇒ Object
35
36
37
|
# File 'lib/transcriptic/auth.rb', line 35
def default_host
"https://secure.transcriptic.com"
end
|
.delete_credentials ⇒ Object
158
159
160
161
|
# File 'lib/transcriptic/auth.rb', line 158
def delete_credentials
FileUtils.rm_f(credentials_file)
clear
end
|
.echo_off ⇒ Object
77
78
79
|
# File 'lib/transcriptic/auth.rb', line 77
def echo_off
system "stty -echo"
end
|
.echo_on ⇒ Object
81
82
83
|
# File 'lib/transcriptic/auth.rb', line 81
def echo_on
system "stty echo"
end
|
.get_credentials ⇒ Object
65
66
67
68
69
70
71
|
# File 'lib/transcriptic/auth.rb', line 65
def get_credentials return if @credentials
unless @credentials = read_credentials
ask_for_and_save_credentials
end
@credentials
end
|
.host ⇒ Object
39
40
41
|
# File 'lib/transcriptic/auth.rb', line 39
def host
ENV['TRANSCRIPTIC_HOST'] || default_host
end
|
.login ⇒ Object
16
17
18
19
|
# File 'lib/transcriptic/auth.rb', line 16
def login
delete_credentials
get_credentials
end
|
.logout ⇒ Object
21
22
23
|
# File 'lib/transcriptic/auth.rb', line 21
def logout
delete_credentials
end
|
.read_credentials ⇒ Object
73
74
75
|
# File 'lib/transcriptic/auth.rb', line 73
def read_credentials
File.exists?(credentials_file) and File.read(credentials_file).split("\n")
end
|
.reauthorize ⇒ Object
43
44
45
|
# File 'lib/transcriptic/auth.rb', line 43
def reauthorize
@credentials = ask_for_and_save_credentials
end
|
.retry_login? ⇒ Boolean
139
140
141
142
143
|
# File 'lib/transcriptic/auth.rb', line 139
def retry_login?
@login_attempts ||= 0
@login_attempts += 1
@login_attempts < 3
end
|
.set_credentials_permissions ⇒ Object
153
154
155
156
|
# File 'lib/transcriptic/auth.rb', line 153
def set_credentials_permissions
FileUtils.chmod 0700, File.dirname(credentials_file)
FileUtils.chmod 0600, credentials_file
end
|
.user ⇒ Object
47
48
49
50
|
# File 'lib/transcriptic/auth.rb', line 47
def user get_credentials
@credentials[0]
end
|
.write_credentials ⇒ Object
145
146
147
148
149
150
151
|
# File 'lib/transcriptic/auth.rb', line 145
def write_credentials
FileUtils.mkdir_p(File.dirname(credentials_file))
f = File.open(credentials_file, 'w')
f.puts self.credentials
f.close
set_credentials_permissions
end
|