Class: GreenDay::AtcoderClient

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

Constant Summary collapse

ATCODER_ENDPOINT =
'https://atcoder.jp'
'.cookie-store'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeAtcoderClient

Returns a new instance of AtcoderClient.



21
22
23
24
25
26
27
28
# File 'lib/green_day/atcoder_client.rb', line 21

def initialize
  @cookie_jar = create_or_load_cookie_jar
  @conn = Faraday.new(url: ATCODER_ENDPOINT) do |builder|
    builder.use :cookie_jar, jar: cookie_jar
    builder.request :url_encoded
    builder.adapter :net_http
  end
end

Instance Attribute Details

#connObject (readonly)

Returns the value of attribute conn.



11
12
13
# File 'lib/green_day/atcoder_client.rb', line 11

def conn
  @conn
end

Returns the value of attribute cookie_jar.



11
12
13
# File 'lib/green_day/atcoder_client.rb', line 11

def cookie_jar
  @cookie_jar
end

Class Method Details

.get_parsed_body(path) ⇒ Object



17
18
19
# File 'lib/green_day/atcoder_client.rb', line 17

def self.get_parsed_body(path)
  new.get_parsed_body(path)
end

.login(username, password) ⇒ Object



13
14
15
# File 'lib/green_day/atcoder_client.rb', line 13

def self.(username, password)
  new.(username, password)
end

Instance Method Details

#get_parsed_body(path) ⇒ Object



30
31
32
33
34
35
36
37
38
39
# File 'lib/green_day/atcoder_client.rb', line 30

def get_parsed_body(path)
  res = conn.get(path)
  if res.status == 429
    puts "429 error with #{path} retrying..."
    sleep 1
    return get_parsed_body(path)
  end

  Nokogiri::HTML.parse(res.body)
end

#login(username, password) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/green_day/atcoder_client.rb', line 41

def (username, password)
  csrf_token = obtain_atcoder_csrf_token

  conn.post('/login',
            username:,
            password:,
            csrf_token:)

  unless 
    ## ex error:Username or Password is incorrect
    raise Error, CGI.unescape(flash_cookie.value).split('.').shift
  end

  cookie_jar.save(COOKIE_FILE_NAME)
end