diff --git a/lib/lingohub/client.rb b/lib/lingohub/client.rb index 7e38fcc..b031a67 100644 --- a/lib/lingohub/client.rb +++ b/lib/lingohub/client.rb @@ -12,7 +12,7 @@ # Example: # # require 'lingohub' -# lingohub = Lingohub::Client.new('me@example.com', 'mypass') +# lingohub = Lingohub::Client.new(auth_token: 'my_api_key') # lingohub.create('myapp') # class Lingohub::Client @@ -25,24 +25,13 @@ def self.gem_version_string "lingohub-gem/#{version}" end - attr_accessor :host, :user, :password - - def self.auth(options) - client = new(options) - OkJson.decode client.post('/sessions', {}, :accept => 'json').to_s - end + attr_accessor :host def initialize(options) - @user = options[:username] - @password = options[:password] @auth_token = options[:auth_token] @host = options[:host] || 'api.lingohub.com' end - def credentials - @auth_token.nil? ? {:username => @user, :password => @password} : {:username => @auth_token, :password => ""} - end - def project(title) project = self.projects[title] raise(Lingohub::Command::CommandFailed, "=== You aren't associated for a project named '#{title}'") if project.nil? @@ -71,28 +60,17 @@ def delete(uri, extra_headers={ }) # :nodoc: def process(method, uri, extra_headers={ }, payload=nil) headers = lingohub_headers.merge(extra_headers) - args = [method, payload, headers].compact - - if credentials[:password] == nil || credentials[:password].empty? - uri = uri + ((uri.include?('?')) ? "&" : "?") - uri = uri + "auth_token=#{credentials[:username]}" - end - - #puts "---- URI --- #{uri} - #{args} - credentials #{credentials}" - response = resource(uri, credentials).send(*args) - - response + args = [method, payload, headers].compact + resource(uri).send(*args) end - def resource(uri, credentials) + def resource(uri) RestClient.proxy = ENV['HTTP_PROXY'] || ENV['http_proxy'] if uri =~ /^https?/ - RestClient::Resource.new(uri, :user => credentials[:username], :password => credentials[:password]) + RestClient::Resource.new(uri) else host_uri = host =~ /^https?/ ? "#{host}/#{api_uri_part}" : "http://#{host}/#{api_uri_part}" - - #puts host_uri + "/" + uri - RestClient::Resource.new(host_uri, :user => credentials[:username], :password => credentials[:password])[uri] + RestClient::Resource.new(host_uri)[uri] end end @@ -102,6 +80,7 @@ def api_uri_part def lingohub_headers # :nodoc: { + 'Authorization' => "Bearer #{@auth_token}", 'X-lingohub-API-Version' => '1', 'User-Agent' => self.class.gem_version_string, 'X-Ruby-Version' => RUBY_VERSION, diff --git a/lib/lingohub/commands/auth.rb b/lib/lingohub/commands/auth.rb index 9a1cb9b..fa1956e 100644 --- a/lib/lingohub/commands/auth.rb +++ b/lib/lingohub/commands/auth.rb @@ -5,13 +5,7 @@ class Auth < Base attr_accessor :credentials def client - @client ||= init_lingohub - end - - def init_lingohub - client = Lingohub::Client.new(:username => user, :auth_token => auth_token, :host => host) -# client.on_warning { |msg| self.display("\n#{msg}\n\n") } - client + @client ||= Lingohub::Client.new(auth_token: auth_token, host: host) end def host @@ -24,17 +18,12 @@ def check end def reauthorize - @credentials = ask_for_and_save_credentials - end - - def user # :nodoc: - get_credentials - @credentials[0] + ask_for_and_save_credentials end def auth_token # :nodoc: get_credentials - @credentials[1] + @credentials end def credentials_file @@ -50,84 +39,29 @@ def get_credentials # :nodoc: end def read_credentials - File.exist?(credentials_file) and File.read(credentials_file).split("\n") - end - - def echo_off - system "stty -echo" - end - - def echo_on - system "stty echo" + File.exist?(credentials_file) and File.read(credentials_file).split("\n").last end def ask_for_credentials - puts "Enter your Lingohub credentials." - - print "Email: " - user = ask - - - print "Password (please leave blank if you want to use your API token): " - password = running_on_windows? ? ask_for_password_on_windows : ask_for_password - - if password.empty? - print "API key: " - api_key = ask - else - api_key = retrieve_api_key(password, user) - end - - [user, api_key] - end - - def retrieve_api_key(password, user) - Lingohub::Client.auth(:username => user, :password => password, :host => host)['api_key'] - end - - 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 - - def ask_for_password - echo_off - password = ask - puts - echo_on - return password + puts "Enter your Lingohub API key." + print "API key: " + ask end def ask_for_and_save_credentials - begin - @credentials = ask_for_credentials - write_credentials - check - rescue ::RestClient::Unauthorized, ::RestClient::ResourceNotFound => e - puts "EXCEPTION #{e}" - delete_credentials - @client = nil - @credentials = nil - display "Authentication failed." - retry if retry_login? - exit 1 - rescue Exception => e - delete_credentials - raise e - end + @credentials = ask_for_credentials + write_credentials + check + rescue ::RestClient::Unauthorized, ::RestClient::ResourceNotFound + delete_credentials + @client = nil + @credentials = nil + display "Authentication failed." + retry if retry_login? + exit 1 + rescue Exception => e + delete_credentials + raise e end def retry_login? @@ -151,7 +85,7 @@ def set_credentials_permissions end def delete_credentials -# FileUtils.rm_f(credentials_file) + FileUtils.rm_f(credentials_file) end end end