Module: Turbot::Helpers

Extended by:
Helpers
Included in:
CLI, Command, Command::Base, Helpers
Defined in:
lib/turbot/helpers.rb,
lib/turbot/helpers/api_helper.rb,
lib/turbot/helpers/netrc_helper.rb,
lib/turbot/helpers/shell_helper.rb

Constant Summary collapse

DEFAULT_HOST =
'http://turbot.opencorporates.com'

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.api(api_key = nil) ⇒ Turbot::API

Returns a Turbot API client.

Parameters:

  • api_key (String) (defaults to: nil)

    an API key

Returns:

  • (Turbot::API)

    a Turbot API client



9
10
11
12
13
14
# 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

.askObject



5
6
7
# File 'lib/turbot/helpers/shell_helper.rb', line 5

def ask
  STDIN.gets.to_s.strip
end

.ask_for_passwordObject



29
30
31
32
33
34
# File 'lib/turbot/helpers/shell_helper.rb', line 29

def ask_for_password
  system 'stty -echo'
  password = ask
  system 'stty echo'
  password
end

.ask_for_password_on_windowsObject



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# 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

.delete_netrc_entryObject

Deletes the Turbot host's entry from the .netrc file.



52
53
54
55
56
# File 'lib/turbot/helpers/netrc_helper.rb', line 52

def delete_netrc_entry
  netrc = open_netrc
  netrc.delete("api.#{host}")
  netrc.save
end

.email_address_and_api_keyArray<String>

Reads the user's email address and API key from either the TURBOT_API_KEY environment variable or from a .netrc file.

Returns:

  • (Array<String>)

    the user's email address and API key



9
10
11
12
13
14
15
16
17
# 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

.error(message) ⇒ Object



14
15
16
17
18
# File 'lib/turbot/helpers.rb', line 14

def error(message)
  prefix = ' !    '
  $stderr.puts(prefix + message.split("\n").join("\n#{prefix}"))
  exit(1)
end

.format_error(error, message = 'Turbot client internal error.') ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# 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

.hostString

Returns the base URL of the Turbot server.

Returns:

  • (String)

    the base URL of the Turbot server



10
11
12
# File 'lib/turbot/helpers.rb', line 10

def host
  ENV['TURBOT_HOST'] || DEFAULT_HOST
end

.netrc_exists?Boolean

Returns whether a .netrc file exists.

Returns:

  • (Boolean)

    whether a .netrc file exists



36
37
38
# File 'lib/turbot/helpers/netrc_helper.rb', line 36

def netrc_exists?
  File.exist?(netrc_path)
end

.netrc_pathString

Returns the path to the .netrc file containing the Turbot host's entry with the user's email address and API key.

Returns:

  • (String)

    the path to the .netrc file



23
24
25
26
27
28
29
30
31
# 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

.open_netrcNetrc

Reads a .netrc file.

Returns:

  • (Netrc)

    the .netrc file



43
44
45
46
47
48
49
# 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

.save_netrc_entry(email_address, api_key) ⇒ Object

Saves the user's email address and AP key to the Turbot host's entry in the .netrc file.



60
61
62
63
64
# 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

.styled_error(error, message = 'Turbot client internal error.') ⇒ Object



20
21
22
# File 'lib/turbot/helpers.rb', line 20

def styled_error(error, message = 'Turbot client internal error.')
  $stderr.puts(format_error(error, message))
end

.turbot_apiClass

The turbot_api gem is slow to load, so we defer its loading.

Returns:

  • (Class)

    the Turbot::API class



33
34
35
36
37
38
39
# File 'lib/turbot/helpers/api_helper.rb', line 33

def turbot_api
  @turbot_api ||= begin
    require 'turbot_api'

    Turbot::API
  end
end

.turbot_api_parametersHash

Returns the parameters for the Turbot API, based on the base URL of the Turbot server.

Returns:

  • (Hash)

    the parameters for the Turbot API



20
21
22
23
24
25
26
27
28
# 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

Instance Method Details

#api(api_key = nil) ⇒ Turbot::API

Returns a Turbot API client.

Parameters:

  • api_key (String) (defaults to: nil)

    an API key

Returns:

  • (Turbot::API)

    a Turbot API client



9
10
11
12
13
14
# 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

#askObject



5
6
7
# File 'lib/turbot/helpers/shell_helper.rb', line 5

def ask
  STDIN.gets.to_s.strip
end

#ask_for_passwordObject



29
30
31
32
33
34
# File 'lib/turbot/helpers/shell_helper.rb', line 29

def ask_for_password
  system 'stty -echo'
  password = ask
  system 'stty echo'
  password
end

#ask_for_password_on_windowsObject



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# 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

#delete_netrc_entryObject

Deletes the Turbot host's entry from the .netrc file.



52
53
54
55
56
# File 'lib/turbot/helpers/netrc_helper.rb', line 52

def delete_netrc_entry
  netrc = open_netrc
  netrc.delete("api.#{host}")
  netrc.save
end

#email_address_and_api_keyArray<String>

Reads the user's email address and API key from either the TURBOT_API_KEY environment variable or from a .netrc file.

Returns:

  • (Array<String>)

    the user's email address and API key



9
10
11
12
13
14
15
16
17
# 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

#error(message) ⇒ Object



14
15
16
17
18
# File 'lib/turbot/helpers.rb', line 14

def error(message)
  prefix = ' !    '
  $stderr.puts(prefix + message.split("\n").join("\n#{prefix}"))
  exit(1)
end

#format_error(error, message = 'Turbot client internal error.') ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# 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

#hostString

Returns the base URL of the Turbot server.

Returns:

  • (String)

    the base URL of the Turbot server



10
11
12
# File 'lib/turbot/helpers.rb', line 10

def host
  ENV['TURBOT_HOST'] || DEFAULT_HOST
end

#netrc_exists?Boolean

Returns whether a .netrc file exists.

Returns:

  • (Boolean)

    whether a .netrc file exists



36
37
38
# File 'lib/turbot/helpers/netrc_helper.rb', line 36

def netrc_exists?
  File.exist?(netrc_path)
end

#netrc_pathString

Returns the path to the .netrc file containing the Turbot host's entry with the user's email address and API key.

Returns:

  • (String)

    the path to the .netrc file



23
24
25
26
27
28
29
30
31
# 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

#open_netrcNetrc

Reads a .netrc file.

Returns:

  • (Netrc)

    the .netrc file



43
44
45
46
47
48
49
# 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

#save_netrc_entry(email_address, api_key) ⇒ Object

Saves the user's email address and AP key to the Turbot host's entry in the .netrc file.



60
61
62
63
64
# 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

#styled_error(error, message = 'Turbot client internal error.') ⇒ Object



20
21
22
# File 'lib/turbot/helpers.rb', line 20

def styled_error(error, message = 'Turbot client internal error.')
  $stderr.puts(format_error(error, message))
end

#turbot_apiClass

The turbot_api gem is slow to load, so we defer its loading.

Returns:

  • (Class)

    the Turbot::API class



33
34
35
36
37
38
39
# File 'lib/turbot/helpers/api_helper.rb', line 33

def turbot_api
  @turbot_api ||= begin
    require 'turbot_api'

    Turbot::API
  end
end

#turbot_api_parametersHash

Returns the parameters for the Turbot API, based on the base URL of the Turbot server.

Returns:

  • (Hash)

    the parameters for the Turbot API



20
21
22
23
24
25
26
27
28
# 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