poj_org 0.1.4 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,47 @@
1
+ require 'open-uri'
2
+ require 'cgi'
3
+
4
+ # This class handles codes on poj.org
5
+ class PojOrg::Code
6
+ # @return [Integer] ID of code
7
+ attr_accessor :id
8
+
9
+ # @return [Integer] Author's user name
10
+ attr_accessor :user
11
+
12
+ # @return [Integer] Time costed in ms
13
+ attr_accessor :time_in_ms
14
+
15
+ # @return [Integer] Memory costed in KB
16
+ attr_accessor :memory_in_kb
17
+
18
+ # @return [String] Programming Language in which code is writen
19
+ attr_accessor :language
20
+
21
+ # @return [String] Source code
22
+ attr_accessor :content
23
+
24
+ # Get a code from poj.org via ID.
25
+ # Need to be autheticated.
26
+ #
27
+ # @param [Integer] id ID of code
28
+ def initialize(id)
29
+ self.id = id
30
+ if PojOrg::authenticated?
31
+ cookie = PojOrg::login
32
+ html = open(url, 'Cookie' => cookie).read
33
+ self.user = (/<td><b>User:.+?<a.+?>(.+?)<\/a><\/td>/).match(html)[1]
34
+ self.time_in_ms = (/<td><b>Time:.+?(\d+)MS<\/td>/).match(html)[1].to_i
35
+ self.memory_in_kb = (/<td><b>Memory:.+?(\d+)K<\/td>/).match(html)[1].to_i
36
+ self.language = (/<td><b>Language:<\/b>\s*(.+?)<\/td>/).match(html)[1]
37
+ self.content = CGI.unescapeHTML((/<pre.+?>(.+)<\/pre>/m).match(html)[1])
38
+ end
39
+ end
40
+
41
+ # Get the url of code on poj.org
42
+ #
43
+ # @return [String] Url of code
44
+ def url
45
+ "http://poj.org/showsource?solution_id=#{id}"
46
+ end
47
+ end
@@ -1,19 +1,32 @@
1
+ # This class handles problems on poj.org
1
2
  class PojOrg::Problem
2
- def self.url_for(id)
3
- "http://poj.org/problem?id=#{id}"
4
- end
3
+ # @return [Integer] ID of problem
4
+ attr_accessor :id
5
5
 
6
- attr_accessor :id, :title, :time_limit_in_ms, :memory_limit_in_kb
6
+ # @return [String] Title
7
+ attr_accessor :title
8
+
9
+ # @return [Integer] Time limit in ms
10
+ attr_accessor :time_limit_in_ms
7
11
 
8
- def initialize(new_id)
9
- self.id = new_id
12
+ # @return [Integer] Memory limit in KB
13
+ attr_accessor :memory_limit_in_kb
14
+
15
+ # Get a problem for poj.org via ID
16
+ #
17
+ # @param [Integer] id ID of problem
18
+ def initialize(id)
19
+ self.id = id
10
20
  html = open(url).read
11
21
  self.title = html[/(?<=<div class="ptt" lang="en-US">).+?(?=<\/div>)/]
12
22
  self.time_limit_in_ms = html[/(?<=<b>Time Limit:<\/b>)\s*\d+/].to_i
13
23
  self.memory_limit_in_kb = html[/(?<=<b>Memory Limit:<\/b>)\s*\d+/].to_i
14
24
  end
15
25
 
26
+ # Get the url of problem on poj.org
27
+ #
28
+ # @return [String] Url of problem
16
29
  def url
17
- PojOrg::Problem.url_for id
30
+ "http://poj.org/problem?id=#{id}"
18
31
  end
19
32
  end
data/lib/poj_org.rb CHANGED
@@ -1,10 +1,28 @@
1
1
  require 'net/http'
2
2
 
3
+ # This class provides authetication methods
3
4
  class PojOrg
5
+ # Stored user name of poj.org
4
6
  @@user = nil
7
+
8
+ # Stored password of poj.org
5
9
  @@password = nil
10
+
11
+ # Status of last authentication
6
12
  @@authenticated = false
7
13
 
14
+ # Autheticate and store user name and password of poj.org
15
+ #
16
+ # @example
17
+ # >> PojOrg::authenticate('poj_library', 'secret')
18
+ # => true
19
+ # >> PojOrg::authenticate('poj_library', 'wrong_password')
20
+ # => false
21
+ #
22
+ # @param [String] user user name of poj.org
23
+ # @param [String] password password of poj.org
24
+ #
25
+ # @return [Boolean] whether authenticated or not
8
26
  def self.authenticate(user, password)
9
27
  if login(user, password)
10
28
  @@user = user
@@ -15,6 +33,9 @@ class PojOrg
15
33
  end
16
34
  end
17
35
 
36
+ # Status of last authentication
37
+ #
38
+ # @return [Boolean] Whether the last authentication success or not
18
39
  def self.authenticated?
19
40
  @@authenticated
20
41
  end
@@ -32,4 +53,4 @@ class PojOrg
32
53
  end
33
54
 
34
55
  require 'poj_org/problem'
35
- require 'poj_org/source'
56
+ require 'poj_org/code'
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: poj_org
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.4
4
+ version: 0.2.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,9 +9,9 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-08-24 00:00:00.000000000 Z
12
+ date: 2012-08-23 00:00:00.000000000 Z
13
13
  dependencies: []
14
- description: Fetch details of problem of source code from poj.org
14
+ description: Fetch details of problems and codes from poj.org
15
15
  email: pinepara@gmail.com
16
16
  executables: []
17
17
  extensions: []
@@ -19,7 +19,7 @@ extra_rdoc_files: []
19
19
  files:
20
20
  - lib/poj_org.rb
21
21
  - lib/poj_org/problem.rb
22
- - lib/poj_org/source.rb
22
+ - lib/poj_org/code.rb
23
23
  homepage: https://github.com/pinepara/poj_org
24
24
  licenses: []
25
25
  post_install_message:
@@ -45,3 +45,4 @@ signing_key:
45
45
  specification_version: 3
46
46
  summary: API for poj.org
47
47
  test_files: []
48
+ has_rdoc:
@@ -1,27 +0,0 @@
1
- require 'open-uri'
2
- require 'cgi'
3
-
4
- class PojOrg::Source
5
- def self.url_for(id)
6
- "http://poj.org/showsource?solution_id=#{id}"
7
- end
8
-
9
- attr_accessor :id, :user, :time_in_ms, :memory_in_kb, :language, :source
10
-
11
- def initialize(new_id)
12
- self.id = new_id
13
- if PojOrg::authenticated?
14
- cookie = PojOrg::login
15
- html = open(url, 'Cookie' => cookie).read
16
- self.user = (/<td><b>User:.+?<a.+?>(.+?)<\/a><\/td>/).match(html)[1]
17
- self.time_in_ms = (/<td><b>Time:.+?(\d+)MS<\/td>/).match(html)[1].to_i
18
- self.memory_in_kb = (/<td><b>Memory:.+?(\d+)K<\/td>/).match(html)[1].to_i
19
- self.language = (/<td><b>Language:<\/b>\s*(.+?)<\/td>/).match(html)[1]
20
- self.source = CGI.unescapeHTML((/<pre.+?>(.+)<\/pre>/m).match(html)[1])
21
- end
22
- end
23
-
24
- def url
25
- PojOrg::Source.url_for id
26
- end
27
- end