class Akashiyaki::AccountParser

Public Class Methods

new(options) click to toggle source
# File lib/akashiyaki/account_parser.rb, line 11
def initialize(options)
  @options = options
end

Public Instance Methods

parse() click to toggle source
# File lib/akashiyaki/account_parser.rb, line 15
def parse
  Account.new(company, id, password)
end

Private Instance Methods

company() click to toggle source
# File lib/akashiyaki/account_parser.rb, line 21
def company
  return @options[:company] if @options[:company]
  return config["company"] if config["company"]

  print "Company ID: "
  STDIN.gets.chomp
end
config() click to toggle source
# File lib/akashiyaki/account_parser.rb, line 29
def config
  @config ||= load_config_file
end
config_dir() click to toggle source
# File lib/akashiyaki/account_parser.rb, line 65
def config_dir
  @config_dir ||=
    File.join(
      ENV["XDG_CONFIG_HOME"] || File.expand_path("~/.config"),
      "ak4"
    )
end
id() click to toggle source
# File lib/akashiyaki/account_parser.rb, line 33
def id
  return @options[:id] if @options[:id]
  return config["id"] if config["id"]

  print "Login ID: "
  STDIN.gets.chomp
end
load_config_file() click to toggle source
# File lib/akashiyaki/account_parser.rb, line 73
def load_config_file
  config_file = search_config

  return {} unless config_file

  case config_file
  when /\.json$/
    JSON.parse(File.read(config_file))
  when /\.ya?ml$/
    YAML.load_file(config_file)
  end
end
password() click to toggle source
# File lib/akashiyaki/account_parser.rb, line 41
def password
  return @options[:password] if @options[:password]
  return config["password"] if config["password"]

  print "Password: "
  password = STDIN.noecho(&:gets).chomp
  puts
  password
end
search_config() click to toggle source
# File lib/akashiyaki/account_parser.rb, line 51
def search_config
  if @options[:config]
    path = File.expand_path(@options[:config])
    return path if File.exist?(path)
  end

  %w(json yaml yml).each do |ext|
    path = File.join(config_dir, "account.#{ext}")
    return path if File.exist?(path)
  end

  nil
end