class Domainr::CLI2::Base

Constants

API_KEY_STORAGE_LOCATION
DEFAULT_COMMAND
STATUS_FETCH_PREFIX

Public Class Methods

rewrite_arguments(arguments) click to toggle source
# File lib/domainr/cli2/base.rb, line 11
def self.rewrite_arguments(arguments)
  options_removed = arguments.reject { |a| a =~ /^\-/ }
  arguments.unshift(DEFAULT_COMMAND) if options_removed.count == 1 && !Domainr::CLI2::Base.commands.keys.include?(options_removed.first)
  arguments
end

Public Instance Methods

api_key(api_key) click to toggle source
# File lib/domainr/cli2/base.rb, line 42
def api_key(api_key)
  File.open(path_to_api_key, 'w') { |f| f.write(api_key) }
end
status(domain) click to toggle source
# File lib/domainr/cli2/base.rb, line 35
def status(domain)
  table = ::Terminal::Table.new headings: %w(Status Domain)
  table << [colorize_status(get_status(domain)), domain]
  puts table
end
version() click to toggle source
# File lib/domainr/cli2/base.rb, line 18
def version
  puts Domainr::CLI2::VERSION
end

Private Instance Methods

client() click to toggle source
# File lib/domainr/cli2/base.rb, line 135
def client
  @client = Domainr::Client.new mashape_key: read_api_key
end
color_for_summary(summary) click to toggle source
# File lib/domainr/cli2/base.rb, line 111
def color_for_summary(summary)
  case summary.downcase.to_sym
  when :active, :undelegated, :tld, :pending
    :red
  when :inactive
    :green
  when :priced, :marketed
    :yellow
  else
    :to_s
  end
end
colorize_status(status) click to toggle source
# File lib/domainr/cli2/base.rb, line 89
def colorize_status(status)
  return 'UKNOWN'.yellow if status.nil?
  summary = status.summary
  friendly_summary(summary)
    .upcase
    .send(color_for_summary(summary))
    .bold
end
debug(info) click to toggle source
# File lib/domainr/cli2/base.rb, line 80
def debug(info)
  return unless options[:verbose]
  puts "[#{'DEBUG'.yellow}] #{info}"
end
friendly_summary(summary) click to toggle source
# File lib/domainr/cli2/base.rb, line 98
def friendly_summary(summary)
  case summary.downcase.to_sym
  when :active, :undelegated, :tld, :pending
    'unavailable'
  when :inactive
    'available'
  when :priced, :marketed
    'for sale'
  else
    summary
  end
end
get_status(domain, current, total) click to toggle source
# File lib/domainr/cli2/base.rb, line 129
def get_status(domain, current, total)
  debug "Fetching status for: #{domain.inspect}"
  print_and_flush "#{STATUS_FETCH_PREFIX} #{current}/#{total}" if options[:status]
  client.status(domain).first
end
path_to_api_key() click to toggle source
# File lib/domainr/cli2/base.rb, line 54
def path_to_api_key
  File.expand_path(API_KEY_STORAGE_LOCATION)
end
print_and_flush(text) click to toggle source
read_api_key() click to toggle source
# File lib/domainr/cli2/base.rb, line 58
def read_api_key
  return @api_key if @api_key
  warn_api_key unless File.exist?(path_to_api_key)
  @api_key = File.open(path_to_api_key).read.strip
  warn_api_key unless @api_key
  debug "Read API key: #{@api_key.inspect}"
  @api_key
end
results_table(results) click to toggle source
# File lib/domainr/cli2/base.rb, line 67
def results_table(results)
  ::Terminal::Table.new headings: %w(Status Domain Path) do |table|
    results.each.with_index do |result, index|
      status = options[:status] ? get_status(result.domain, index+1, results.count) : nil
      table.add_row [
        colorize_status(status),
        result.domain,
        url_for_result(result)
      ]
    end
  end
end
url_for_result(result) click to toggle source
# File lib/domainr/cli2/base.rb, line 85
def url_for_result(result)
  "http://#{result.domain}#{result.path}"
end
warn_api_key() click to toggle source
# File lib/domainr/cli2/base.rb, line 48
def warn_api_key
  STDERR.puts "\nUh oh! You need to set a Mashape API key before you can use this tool.\n".red
  STDERR.puts "To obtain one, visit: https://github.com/domainr/api/wiki\nThen use #{'domainr api_key <your-api-key>'.yellow} to set your key.\n\n"
  exit
end