class GreenDay::AtcoderClient
Constants
- ATCODER_ENDPOINT
- COOKIE_FILE_NAME
Attributes
client[R]
Public Class Methods
new()
click to toggle source
# File lib/green_day/atcoder_client.rb, line 14 def initialize @cookie_jar = create_or_load_cookie_jar @client = Faraday.new(url: ATCODER_ENDPOINT) do |builder| builder.use :cookie_jar, jar: cookie_jar builder.request :url_encoded builder.adapter :net_http end end
Public Instance Methods
contest_exist?(contest_name)
click to toggle source
# File lib/green_day/atcoder_client.rb, line 23 def contest_exist?(contest_name) res = client.get("contests/#{contest_name}") res.status == 200 end
fetch_inputs_and_outputs(contest, task)
click to toggle source
# File lib/green_day/atcoder_client.rb, line 36 def fetch_inputs_and_outputs(contest, task) contest_name = contest.name path = "contests/#{contest_name}/tasks/#{contest_name}_#{task.code.downcase}" body = get_parsed_body(path) samples = body.css('.lang-ja > .part > section > pre').map { |e| e.children.text } inputs, outputs = samples.partition.with_index { |_sample, i| i.even? } [inputs, outputs] end
fetch_task_codes(contest)
click to toggle source
# File lib/green_day/atcoder_client.rb, line 28 def fetch_task_codes(contest) body = get_parsed_body("contests/#{contest.name}/tasks") # 3問だったら<tbody>の中に<tr>が3 * 2個 </tbody> が1mh個 tasks_size = ((body.at('tbody').children.size - 1) / 2.0).ceil ('A'..'Z').to_a.shift(tasks_size) end
login(username, password)
click to toggle source
# File lib/green_day/atcoder_client.rb, line 47 def login(username, password) csrf_token = obtain_atcoder_csrf_token client.post('/login', username: username, password: password, csrf_token: csrf_token) unless login_succeed? ## ex error:Username or Password is incorrect raise Error, CGI.unescape(select_flash_cookie.value).split('.').shift end cookie_jar.save(COOKIE_FILE_NAME) end
Private Instance Methods
get_parsed_body(path)
click to toggle source
# File lib/green_day/atcoder_client.rb, line 94 def get_parsed_body(path) res = client.get(path) Nokogiri::HTML.parse(res.body) end
login_succeed?()
click to toggle source
# File lib/green_day/atcoder_client.rb, line 85 def login_succeed? flash_cookie = select_flash_cookie flash_cookie.value.include?('Welcome') end
obtain_atcoder_csrf_token()
click to toggle source
# File lib/green_day/atcoder_client.rb, line 77 def obtain_atcoder_csrf_token get_login_response = client.get('/login') login_html = Nokogiri::HTML.parse(get_login_response.body) login_html.at('input[name="csrf_token"]')['value'] rescue StandardError raise Error, 'cant get_csrf_token' end