class Pcoder::Atcoder

Public Class Methods

new() click to toggle source
# File lib/pcoder.rb, line 59
def initialize
  @agent = Mechanize.new
end

Public Instance Methods

get_task_id(pos) click to toggle source
# File lib/pcoder.rb, line 97
def get_task_id(pos)
  @agent.get("http://#{@agent.page.uri.host}/submit")
  selecter = "//select[@id=\"submit-task-selector\"]/option[#{pos}]"
  @agent.page.at(selecter)['value']
end
login(user, pass, host) click to toggle source
# File lib/pcoder.rb, line 63
def login(user, pass, host)
  @agent.get("http://#{host}/login")
  before_uri = @agent.page.uri
  @agent.page.form_with do |f|
    f.field_with(:name => "name").value = user
    f.field_with(:name => "password").value = pass
  end.click_button
  if @agent.page.uri == before_uri
    mes = "The username or password you entered is incorrect."
    raise LoginError.exception(mes)
  end
  true
end
set_proxy(proxy) click to toggle source
# File lib/pcoder.rb, line 92
def set_proxy(proxy)
  host, port = proxy.split(":")
  @agent.set_proxy(host, port)
end
submit(source) click to toggle source
# File lib/pcoder.rb, line 77
def submit(source)
  @agent.get("http://#{@agent.page.uri.host}/submit")
  task_id = get_task_id(source.task)
  @agent.page.form_with do |f|
    f.field_with(:name => "task_id").value = task_id
    f.field_with(:name => "language_id_#{task_id}").value = source.language_id
    f.field_with(:name => "source_code").value = source.body
  end.click_button
  if @agent.page.uri.path == "/submit"
    mes = "Please check file name or body and try again"
    raise InputFormError.exception(mes)
  end
  true
end