Class: GreenDay::AtcoderClient
- Inherits:
-
Object
- Object
- GreenDay::AtcoderClient
- Defined in:
- lib/green_day/atcoder_client.rb
Constant Summary collapse
- ATCODER_ENDPOINT =
'https://atcoder.jp'
- COOKIE_FILE_NAME =
'.cookie-store'
Instance Attribute Summary collapse
-
#conn ⇒ Object
readonly
Returns the value of attribute conn.
-
#cookie_jar ⇒ Object
readonly
Returns the value of attribute cookie_jar.
Class Method Summary collapse
Instance Method Summary collapse
- #get_parsed_body(path) ⇒ Object
-
#initialize ⇒ AtcoderClient
constructor
A new instance of AtcoderClient.
- #login(username, password) ⇒ Object
Constructor Details
#initialize ⇒ AtcoderClient
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 = @conn = Faraday.new(url: ATCODER_ENDPOINT) do |builder| builder.use :cookie_jar, jar: builder.request :url_encoded builder.adapter :net_http end end |
Instance Attribute Details
#conn ⇒ Object (readonly)
Returns the value of attribute conn.
11 12 13 |
# File 'lib/green_day/atcoder_client.rb', line 11 def conn @conn end |
#cookie_jar ⇒ Object (readonly)
Returns the value of attribute cookie_jar.
11 12 13 |
# File 'lib/green_day/atcoder_client.rb', line 11 def @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.login(username, password) new.login(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 login(username, password) csrf_token = obtain_atcoder_csrf_token conn.post('/login', username:, password:, csrf_token:) unless login_succeed? ## ex error:Username or Password is incorrect raise Error, CGI.unescape(.value).split('.').shift end .save(COOKIE_FILE_NAME) end |