Class: PojOrg::User

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

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#usernameObject (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

Returns:

  • (Boolean)


14
15
16
17
18
19
# File 'lib/poj_org/user.rb', line 14

def authenticated?
  
  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(options = {})
  html = Nokogiri::HTML(open(status_url(options)))
  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 options[:bottom] && ids.last <= options[:bottom]
      ids = ids.keep_if { |id| id > options[:bottom] }
    else
      ids += code_ids(options.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(options = {})
  code_ids(options).map do |id|
    code(id)
  end
end

#login {|response['set-cookie']| ... } ⇒ Object

Yields:

  • (response['set-cookie'])

Raises:



21
22
23
24
25
26
27
28
29
30
# File 'lib/poj_org/user.rb', line 21

def 
  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

#urlObject



62
63
64
# File 'lib/poj_org/user.rb', line 62

def url
  "http://poj.org/userstatus?user_id=#{@username}"
end