module Turbot::Helpers
Constants
- DEFAULT_HOST
Public Instance Methods
Returns a Turbot
API client.
@param [String] api_key an API key @return [Turbot::API] a Turbot
API client
# File lib/turbot/helpers/api_helper.rb, line 9 def api(api_key = nil) # For whatever reason, unless we provide a truthy API key, Turbot::API's # initializer immediately attempts to `#get_api_key_for_credentials`. api_key ||= email_address_and_api_key[1] || '' turbot_api.new(turbot_api_parameters.merge(:api_key => api_key)) end
# File lib/turbot/helpers/shell_helper.rb, line 5 def ask STDIN.gets.to_s.strip end
# File lib/turbot/helpers/shell_helper.rb, line 29 def ask_for_password system 'stty -echo' password = ask system 'stty echo' password end
# File lib/turbot/helpers/shell_helper.rb, line 9 def ask_for_password_on_windows require 'Win32API' password = '' while char = Win32API.new('crtdll', '_getch', [], 'L').Call do if char == 10 || char == 13 # received carriage return or newline break end 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 password end
Deletes the Turbot
host's entry from the `.netrc` file.
# File lib/turbot/helpers/netrc_helper.rb, line 52 def delete_netrc_entry netrc = open_netrc netrc.delete("api.#{host}") netrc.save end
Reads the user's email address and API key from either the `TURBOT_API_KEY` environment variable or from a `.netrc` file.
@return [Array<String>] the user's email address and API key
# File lib/turbot/helpers/netrc_helper.rb, line 9 def email_address_and_api_key if ENV['TURBOT_API_KEY'] ['', ENV['TURBOT_API_KEY']] elsif netrc_exists? open_netrc["api.#{host}"] || [] else [] end end
# File lib/turbot/helpers.rb, line 14 def error(message) prefix = ' ! ' $stderr.puts(prefix + message.split("\n").join("\n#{prefix}")) exit(1) end
# File lib/turbot/helpers.rb, line 24 def format_error(error, message = 'Turbot client internal error.') formatted_error = [] formatted_error << " ! #{message}" formatted_error << ' ! Report a bug at: https://github.com/openc/turbot-client/issues/new' formatted_error << '' formatted_error << " Error: #{error.message} (#{error.class})" formatted_error << " Backtrace: #{error.backtrace.first}" error.backtrace[1..-1].each do |line| formatted_error << " #{line}" end if error.backtrace.length > 1 formatted_error << '' end command = ARGV.map do |arg| if arg.include?(' ') arg = %{"#{arg}"} else arg end end.join(' ') formatted_error << " Command: turbot #{command}" unless host == DEFAULT_HOST formatted_error << " Host: #{host}" end formatted_error << " Version: #{Turbot::DEBUG_VERSION}" formatted_error << "\n" formatted_error.join("\n") end
Returns whether a `.netrc` file exists.
@return [Boolean] whether a `.netrc` file exists
# File lib/turbot/helpers/netrc_helper.rb, line 36 def netrc_exists? File.exist?(netrc_path) end
Returns the path to the `.netrc` file containing the Turbot
host's entry with the user's email address and API key.
@return [String] the path to the `.netrc` file
# File lib/turbot/helpers/netrc_helper.rb, line 23 def netrc_path unencrypted = Netrc.default_path encrypted = unencrypted + '.gpg' if File.exists?(encrypted) encrypted else unencrypted end end
Reads a `.netrc` file.
@return [Netrc] the `.netrc` file
# File lib/turbot/helpers/netrc_helper.rb, line 43 def open_netrc begin Netrc.read(netrc_path) rescue Netrc::Error => e error e.message end end
Saves the user's email address and AP key to the Turbot
host's entry in the `.netrc` file.
# File lib/turbot/helpers/netrc_helper.rb, line 60 def save_netrc_entry(email_address, api_key) netrc = open_netrc netrc["api.#{host}"] = [email_address, api_key] netrc.save end
# File lib/turbot/helpers.rb, line 20 def styled_error(error, message = 'Turbot client internal error.') $stderr.puts(format_error(error, message)) end
The `turbot_api` gem is slow to load, so we defer its loading.
@return [Class] the Turbot::API class
# File lib/turbot/helpers/api_helper.rb, line 33 def turbot_api @turbot_api ||= begin require 'turbot_api' Turbot::API end end
Returns the parameters for the Turbot
API, based on the base URL of the Turbot
server.
@return [Hash] the parameters for the Turbot
API
# File lib/turbot/helpers/api_helper.rb, line 20 def turbot_api_parameters uri = URI.parse(host) { :host => uri.host, :port => uri.port, :scheme => uri.scheme, } end