Module: AOJ::CLI::Helper

Included in:
AOJ::CLI
Defined in:
lib/aoj/cli/helper.rb

Instance Method Summary collapse

Instance Method Details

#confObject



81
82
83
# File 'lib/aoj/cli/helper.rb', line 81

def conf
  Conf.instance
end

#detect_language(file, opt) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/aoj/cli/helper.rb', line 5

def detect_language(file, opt)
  language =
    if opt
      AOJ::Language.find(opt.to_sym)
    else
      AOJ::Language.find_by_ext(File.extname(file))
    end

  unless language
    raise AOJ::Error::LanguageDetectionError, "Failed to detect language, check `aoj langs`"
  end
  language
end

#detect_problem(file, opt) ⇒ Object



19
20
21
22
# File 'lib/aoj/cli/helper.rb', line 19

def detect_problem(file, opt)
  problem_id = opt || File.basename(file, ".*")
  API.problem_search problem_id
end

#input_credentialsObject



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/aoj/cli/helper.rb', line 46

def input_credentials
  credential = Credential.new

  loop do
    puts "Input AOJ username:"
    credential.username = STDIN.gets.strip
    puts "Input AOJ password:"
    credential.password = STDIN.noecho(&:gets).strip
    puts
    break if credential.valid?
    puts "Invalid username/password."
  end

  conf['username'] = credential.username
  conf['password'] = credential.password
  conf.save
  puts "Your credential is saved at #{conf.rcfile_path}"
end


37
38
39
40
41
42
43
44
# File 'lib/aoj/cli/helper.rb', line 37

def print_result(result)
  puts "Judge result:"
  puts "  Status:    #{result.status}"     if result.status
  puts "  Code Size: #{result.code_size}"  if result.code_size
  puts "  CPU Time:  #{result.cputime}"    if result.cputime
  puts "  Memory:    #{result.memory}"     if result.memory
  puts "  URL:       #{result.review_url}" if result.review_url
end


30
31
32
33
34
35
# File 'lib/aoj/cli/helper.rb', line 30

def print_solution_info(solution)
  puts "Language: #{solution.language.submit_name}"
  puts "Problem:"
  puts "  id:   #{solution.problem.id}"
  puts "  name: #{solution.problem.name}"
end

#read_file(file) ⇒ Object



24
25
26
27
28
# File 'lib/aoj/cli/helper.rb', line 24

def read_file(file)
  File.read(file)
rescue Errno::ENOENT => e
  raise AOJ::Error::FileOpenError, e.message
end

#twitter_authObject



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/aoj/cli/helper.rb', line 65

def twitter_auth
  request_token = Twitter.instance.request_token
  puts "Connect to twitter account. Please access to url."
  puts request_token.authorize_url
  puts "Input PIN:"
  pin = STDIN.gets.strip
  puts
  access_token = request_token.get_access_token(oauth_verifier: pin)
  conf['twitter'] = {
    'access_token'        => access_token.token,
    'access_token_secret' => access_token.secret
  }
  conf.save
  puts "Your credential is saved at #{conf.rcfile_path}"
end