Module: AtCoderFriends::Scraping::CustomTest

Included in:
Agent
Defined in:
lib/at_coder_friends/scraping/custom_test.rb

Overview

run tests on custom_test page

Instance Method Summary collapse

Instance Method Details

#check_custom_testObject



37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/at_coder_friends/scraping/custom_test.rb', line 37

def check_custom_test
  100.times do
    page = agent.get(contest_url('custom_test/json?reload=true'))
    data = JSON.parse(page.body)
    return nil unless data.is_a?(Hash) && data['Result']
    return data if data.dig('Result', 'Status') == 3
    return data unless data['Interval']

    sleep 1.0 * data['Interval'] / 1000
  end

  nil
end

#code_test(infile) ⇒ Object



9
10
11
12
13
14
15
16
17
# File 'lib/at_coder_friends/scraping/custom_test.rb', line 9

def code_test(infile)
  path, _dir, _prg, _base, ext, _q = ctx.path_info.components
  langs = lang_id(ext)
  src = File.read(path, encoding: Encoding::UTF_8)
  data = File.read(infile)

  post_custom_test(langs, src, data)
  check_custom_test
end

#post_custom_test(langs, src, data) ⇒ Object

Raises:



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/at_coder_friends/scraping/custom_test.rb', line 19

def post_custom_test(langs, src, data)
  page = fetch_with_auth(contest_url('custom_test'))
  lang = find_lang(page, langs)
  script = page.search('script').text
  csrf_token = script.scan(/var csrfToken = "(.*)"/)[0][0]

  page = agent.post(
    contest_url('custom_test/submit/json'),
    'data.LanguageId' => lang,
    'sourceCode' => src,
    'input' => data,
    'csrf_token' => csrf_token
  )

  msg = page.body
  raise AppError, msg unless msg.empty?
end