Class: PojOrg::Code

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id) ⇒ Code

Returns a new instance of Code.



11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/poj_org/code.rb', line 11

def initialize(id)
  @id = id
  html = Nokogiri::HTML(open("http://poj.org/status?top=#{@id+1}"))
  raise PojOrg::Errors::CodeNotFound if html.xpath('/html/body/table[2]/tr[2]/td[1]').text.to_i != @id
  @username       = html.xpath('/html/body/table[2]/tr[2]/td[2]').text
  @problem_id     = html.xpath('/html/body/table[2]/tr[2]/td[3]').text.to_i
  @result         = html.xpath('/html/body/table[2]/tr[2]/td[4]').text
  @memory_in_kb   = html.xpath('/html/body/table[2]/tr[2]/td[5]').text.to_i
  @time_in_ms     = html.xpath('/html/body/table[2]/tr[2]/td[6]').text.to_i
  @language       = html.xpath('/html/body/table[2]/tr[2]/td[7]').text
  @length_in_byte = html.xpath('/html/body/table[2]/tr[2]/td[8]').text.to_i
  @submitted_at   = html.xpath('/html/body/table[2]/tr[2]/td[9]').text
end

Instance Attribute Details

#contentObject (readonly)

Returns the value of attribute content.



9
10
11
# File 'lib/poj_org/code.rb', line 9

def content
  @content
end

#idObject (readonly)

Returns the value of attribute id.



9
10
11
# File 'lib/poj_org/code.rb', line 9

def id
  @id
end

#languageObject (readonly)

Returns the value of attribute language.



9
10
11
# File 'lib/poj_org/code.rb', line 9

def language
  @language
end

#length_in_byteObject (readonly)

Returns the value of attribute length_in_byte.



9
10
11
# File 'lib/poj_org/code.rb', line 9

def length_in_byte
  @length_in_byte
end

#memory_in_kbObject (readonly)

Returns the value of attribute memory_in_kb.



9
10
11
# File 'lib/poj_org/code.rb', line 9

def memory_in_kb
  @memory_in_kb
end

#problem_idObject (readonly)

Returns the value of attribute problem_id.



9
10
11
# File 'lib/poj_org/code.rb', line 9

def problem_id
  @problem_id
end

#resultObject (readonly)

Returns the value of attribute result.



9
10
11
# File 'lib/poj_org/code.rb', line 9

def result
  @result
end

#submitted_atObject (readonly)

Returns the value of attribute submitted_at.



9
10
11
# File 'lib/poj_org/code.rb', line 9

def 
  @submitted_at
end

#time_in_msObject (readonly)

Returns the value of attribute time_in_ms.



9
10
11
# File 'lib/poj_org/code.rb', line 9

def time_in_ms
  @time_in_ms
end

#usernameObject (readonly)

Returns the value of attribute username.



9
10
11
# File 'lib/poj_org/code.rb', line 9

def username
  @username
end

Instance Method Details

#fetch_content(password) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
# File 'lib/poj_org/code.rb', line 29

def fetch_content(password)
  PojOrg::User.new(username, password). do |cookie|
    html = Nokogiri::HTML(open(url, 'Cookie' => cookie))
    if html.xpath('/html/head/title').text == 'Error'
      raise PojOrg::Errors::CodeNotFound if html.xpath('/html/body/ul/li').text.include?('No such solution')
      raise PojOrg::Errors::AccessDenied if html.xpath('/html/body/ul/li').text.include?('Source request declined')
      raise PojOrg::Errors::Error
    end
    @content = html.xpath('/html/body/pre').text
  end
end

#urlObject



25
26
27
# File 'lib/poj_org/code.rb', line 25

def url
  "http://poj.org/showsource?solution_id=#{id}"
end