Class: Transcriptic::Auth
- Inherits:
-
Object
- Object
- Transcriptic::Auth
- Defined in:
- lib/transcriptic/auth.rb
Class Attribute Summary collapse
-
.credentials ⇒ Object
Returns the value of attribute credentials.
Class Method Summary collapse
-
.api_key ⇒ Object
:nodoc:.
- .ask_for_and_save_credentials ⇒ Object
- .ask_for_credentials ⇒ Object
- .ask_for_password ⇒ Object
- .ask_for_password_on_windows ⇒ Object
-
.check ⇒ Object
just a stub; will raise if not authenticated.
- .clear ⇒ Object
- .client ⇒ Object
- .credentials_file ⇒ Object
- .default_host ⇒ Object
- .delete_credentials ⇒ Object
- .echo_off ⇒ Object
- .echo_on ⇒ Object
-
.get_credentials ⇒ Object
:nodoc:.
- .host ⇒ Object
- .login ⇒ Object
- .logout ⇒ Object
- .read_credentials ⇒ Object
- .reauthorize ⇒ Object
- .retry_login? ⇒ Boolean
- .set_credentials_permissions ⇒ Object
-
.user ⇒ Object
:nodoc:.
- .write_credentials ⇒ Object
Class Attribute Details
.credentials ⇒ Object
Returns the value of attribute credentials.
3 4 5 |
# File 'lib/transcriptic/auth.rb', line 3 def credentials @credentials end |
Class Method Details
.api_key ⇒ Object
:nodoc:
49 50 51 52 |
# File 'lib/transcriptic/auth.rb', line 49 def api_key # :nodoc: get_credentials @credentials[1] end |
.ask_for_and_save_credentials ⇒ Object
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 |
# File 'lib/transcriptic/auth.rb', line 120 def ask_for_and_save_credentials begin @credentials = ask_for_credentials write_credentials check rescue ::RestClient::Unauthorized, ::RestClient::ResourceNotFound => e delete_credentials clear display "Authentication failed." retry if retry_login? exit 1 rescue Exception => e delete_credentials raise e end end |
.ask_for_credentials ⇒ Object
82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/transcriptic/auth.rb', line 82 def ask_for_credentials puts "Enter your Transcriptic credentials." print "Email: " user = ask print "Password: " 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
112 113 114 115 116 117 118 |
# File 'lib/transcriptic/auth.rb', line 112 def ask_for_password echo_off password = ask puts echo_on return password end |
.ask_for_password_on_windows ⇒ Object
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/transcriptic/auth.rb', line 94 def ask_for_password_on_windows require "Win32API" char = nil password = '' while char = Win32API.new("crtdll", "_getch", [ ], "L").Call do break if char == 10 || char == 13 # received carriage return or newline if char == 127 || char == 8 # backspace and delete password.slice!(-1, 1) else # windows might throw a -1 at us so make sure to handle RangeError (password << char.chr) rescue RangeError end end puts return password end |
.check ⇒ Object
just a stub; will raise if not authenticated
28 29 30 |
# File 'lib/transcriptic/auth.rb', line 28 def check client.list end |
.clear ⇒ Object
22 23 24 25 |
# File 'lib/transcriptic/auth.rb', line 22 def clear @credentials = nil @client = nil end |
.client ⇒ Object
5 6 7 8 9 10 11 |
# File 'lib/transcriptic/auth.rb', line 5 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
54 55 56 57 58 59 60 |
# File 'lib/transcriptic/auth.rb', line 54 def credentials_file if host == default_host "#{Transcriptic.home_directory}/.transcriptic/credentials" else "#{Transcriptic.home_directory}/.transcriptic/credentials.#{host}" end end |
.default_host ⇒ Object
32 33 34 |
# File 'lib/transcriptic/auth.rb', line 32 def default_host "transcriptic.com" end |
.delete_credentials ⇒ Object
156 157 158 159 |
# File 'lib/transcriptic/auth.rb', line 156 def delete_credentials FileUtils.rm_f(credentials_file) clear end |
.echo_off ⇒ Object
74 75 76 |
# File 'lib/transcriptic/auth.rb', line 74 def echo_off system "stty -echo" end |
.echo_on ⇒ Object
78 79 80 |
# File 'lib/transcriptic/auth.rb', line 78 def echo_on system "stty echo" end |
.get_credentials ⇒ Object
:nodoc:
62 63 64 65 66 67 68 |
# File 'lib/transcriptic/auth.rb', line 62 def get_credentials # :nodoc: return if @credentials unless @credentials = read_credentials ask_for_and_save_credentials end @credentials end |
.host ⇒ Object
36 37 38 |
# File 'lib/transcriptic/auth.rb', line 36 def host ENV['TRANSCRIPTIC_HOST'] || default_host end |
.login ⇒ Object
13 14 15 16 |
# File 'lib/transcriptic/auth.rb', line 13 def login delete_credentials get_credentials end |
.logout ⇒ Object
18 19 20 |
# File 'lib/transcriptic/auth.rb', line 18 def logout delete_credentials end |
.read_credentials ⇒ Object
70 71 72 |
# File 'lib/transcriptic/auth.rb', line 70 def read_credentials File.exists?(credentials_file) and File.read(credentials_file).split("\n") end |
.reauthorize ⇒ Object
40 41 42 |
# File 'lib/transcriptic/auth.rb', line 40 def @credentials = ask_for_and_save_credentials end |
.retry_login? ⇒ Boolean
137 138 139 140 141 |
# File 'lib/transcriptic/auth.rb', line 137 def retry_login? @login_attempts ||= 0 @login_attempts += 1 @login_attempts < 3 end |
.set_credentials_permissions ⇒ Object
151 152 153 154 |
# File 'lib/transcriptic/auth.rb', line 151 def FileUtils.chmod 0700, File.dirname(credentials_file) FileUtils.chmod 0600, credentials_file end |
.user ⇒ Object
:nodoc:
44 45 46 47 |
# File 'lib/transcriptic/auth.rb', line 44 def user # :nodoc: get_credentials @credentials[0] end |