Class: Pcoder::Atcoder

Inherits:
Object
  • Object
show all
Defined in:
lib/pcoder.rb

Instance Method Summary collapse

Constructor Details

#initializeAtcoder



59
60
61
# File 'lib/pcoder.rb', line 59

def initialize
  @agent = Mechanize.new
end

Instance Method Details

#get_task_id(pos) ⇒ Object



97
98
99
100
101
# 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) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/pcoder.rb', line 63

def (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) ⇒ Object



92
93
94
95
# File 'lib/pcoder.rb', line 92

def set_proxy(proxy)
  host, port = proxy.split(":")
  @agent.set_proxy(host, port)
end

#submit(source) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
89
90
# 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