Class: PojOrg::User
- Inherits:
-
Object
- Object
- PojOrg::User
- Defined in:
- lib/poj_org/user.rb
Instance Attribute Summary collapse
-
#username ⇒ Object
readonly
Returns the value of attribute username.
Instance Method Summary collapse
- #authenticated? ⇒ Boolean
- #code(id) ⇒ Object
- #code_ids(options = {}) ⇒ Object
- #codes(options = {}) ⇒ Object
-
#initialize(username, password) ⇒ User
constructor
A new instance of User.
- #login {|response['set-cookie']| ... } ⇒ Object
- #url ⇒ Object
Constructor Details
#initialize(username, password) ⇒ User
Returns a new instance of User.
9 10 11 12 |
# File 'lib/poj_org/user.rb', line 9 def initialize(username, password) @username = username @password = password end |
Instance Attribute Details
#username ⇒ Object (readonly)
Returns the value of attribute username.
7 8 9 |
# File 'lib/poj_org/user.rb', line 7 def username @username end |
Instance Method Details
#authenticated? ⇒ Boolean
14 15 16 17 18 19 |
# File 'lib/poj_org/user.rb', line 14 def authenticated? login true rescue false end |
#code(id) ⇒ Object
32 33 34 35 36 |
# File 'lib/poj_org/user.rb', line 32 def code(id) code = Code.new(id) code.fetch_content(@password) code end |
#code_ids(options = {}) ⇒ Object
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/poj_org/user.rb', line 38 def code_ids( = {}) html = Nokogiri::HTML(open(status_url())) ids = [] 2.upto(Float::INFINITY) do |row| element = html.xpath("/html/body/table[2]/tr[#{row}]/td[1]").text break if element.empty? ids << element.to_i end unless ids.empty? if [:bottom] && ids.last <= [:bottom] ids = ids.keep_if { |id| id > [:bottom] } else ids += code_ids(.merge(top: ids.last)) end end ids end |
#codes(options = {}) ⇒ Object
56 57 58 59 60 |
# File 'lib/poj_org/user.rb', line 56 def codes( = {}) code_ids().map do |id| code(id) end end |
#login {|response['set-cookie']| ... } ⇒ Object
21 22 23 24 25 26 27 28 29 30 |
# File 'lib/poj_org/user.rb', line 21 def login response = Net::HTTP.post_form( URI('http://poj.org/login'), 'user_id1' => username, 'password1' => @password ) raise PojOrg::Errors::Unauthorized if response.body['failed'] yield response['set-cookie'] if block_given? open('http://poj.org/login?action=logout') end |
#url ⇒ Object
62 63 64 |
# File 'lib/poj_org/user.rb', line 62 def url "http://poj.org/userstatus?user_id=#{@username}" end |