poj_org 0.2.1 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/lib/poj_org.rb CHANGED
@@ -1,17 +1,19 @@
1
1
  require 'net/http'
2
+ require 'open-uri'
3
+ require 'cgi'
2
4
 
3
5
  # This class provides authetication methods
4
6
  class PojOrg
5
- # Stored user name of poj.org
7
+ # Stored user name
6
8
  @@user = nil
7
9
 
8
- # Stored password of poj.org
10
+ # Stored password
9
11
  @@password = nil
10
12
 
11
13
  # Status of last authentication
12
14
  @@authenticated = false
13
15
 
14
- # Autheticate and store user name and password of poj.org
16
+ # Autheticate and store user name and password
15
17
  #
16
18
  # @example
17
19
  # >> PojOrg::authenticate('poj_library', 'secret')
@@ -19,10 +21,10 @@ class PojOrg
19
21
  # >> PojOrg::authenticate('poj_library', 'wrong_password')
20
22
  # => false
21
23
  #
22
- # @param [String] user user name of poj.org
23
- # @param [String] password password of poj.org
24
+ # @param [String] user user name of poj.org
25
+ # @param [String] password password of poj.org
24
26
  #
25
- # @return [Boolean] whether authenticated or not
27
+ # @return [Boolean] whether authenticated or not
26
28
  def self.authenticate(user, password)
27
29
  if login(user, password)
28
30
  @@user = user
@@ -33,6 +35,13 @@ class PojOrg
33
35
  end
34
36
  end
35
37
 
38
+ # Unauthenticate and erase user name and password
39
+ def self.unauthenticate
40
+ @@user = nil
41
+ @@password = nil
42
+ @@authenticated = false
43
+ end
44
+
36
45
  # Status of last authentication
37
46
  #
38
47
  # @return [Boolean] Whether the last authentication success or not
@@ -40,6 +49,63 @@ class PojOrg
40
49
  @@authenticated
41
50
  end
42
51
 
52
+ # Url of problem
53
+ #
54
+ # @param [Integer] ID of problem
55
+ #
56
+ # @return [String] Url of problem
57
+ def self.problem_url(id)
58
+ "http://poj.org/problem?id=#{id}"
59
+ end
60
+
61
+ # Details of problem
62
+ #
63
+ # @param [Integer] ID of problem
64
+ #
65
+ # @return [Hash] Details of problem or Nil if any error occured
66
+ def self.problem(id)
67
+ html = open(problem_url(id)).read
68
+ return nil if html.include? '<title>Error</title>'
69
+ {
70
+ id: id,
71
+ url: problem_url(id),
72
+ title: html[/(?<=<div class="ptt" lang="en-US">).+?(?=<\/div>)/],
73
+ time_limit_in_ms: html[/(?<=<b>Time Limit:<\/b>)\s*\d+/].to_i,
74
+ memory_limit_in_kb: html[/(?<=<b>Memory Limit:<\/b>)\s*\d+/].to_i
75
+ }
76
+ end
77
+
78
+ # Url of code
79
+ #
80
+ # @param [Integer] ID of code
81
+ #
82
+ # @return [String] Url of code
83
+ def self.code_url(id)
84
+ "http://poj.org/showsource?solution_id=#{id}"
85
+ end
86
+
87
+ # Details of code
88
+ # Need to be autheticated
89
+ #
90
+ # @param [Integer] ID of code
91
+ #
92
+ # @return [Hash] Details of code or Nil if any error occured
93
+ def self.code(id)
94
+ return nil unless authenticated?
95
+ html = open(code_url(id), 'Cookie' => login).read
96
+ return nil if html.include? '<title>Error</title>'
97
+ {
98
+ id: id,
99
+ url: code_url(id),
100
+ problem_id: (/<td><b>Problem:.+?<a.+?>(.+?)<\/a><\/td>/).match(html)[1].to_i,
101
+ user: (/<td><b>User:.+?<a.+?>(.+?)<\/a><\/td>/).match(html)[1],
102
+ time_in_ms: (/<td><b>Time:.+?(\d+)MS<\/td>/).match(html)[1].to_i,
103
+ memory_in_kb: (/<td><b>Memory:.+?(\d+)K<\/td>/).match(html)[1].to_i,
104
+ language: (/<td><b>Language:<\/b>\s*(.+?)<\/td>/).match(html)[1],
105
+ content: CGI.unescapeHTML((/<pre.+?>(.+)<\/pre>/m).match(html)[1])
106
+ }
107
+ end
108
+
43
109
  private
44
110
 
45
111
  def self.login(user = nil, password = nil)
@@ -51,6 +117,3 @@ class PojOrg
51
117
  login.body['failed'] ? false : login['set-cookie']
52
118
  end
53
119
  end
54
-
55
- require 'poj_org/problem'
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.2.1
4
+ version: 0.3.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-08-23 00:00:00.000000000 Z
12
+ date: 2013-01-25 00:00:00.000000000 Z
13
13
  dependencies: []
14
14
  description: Fetch details of problems and codes from poj.org
15
15
  email: pinepara@gmail.com
@@ -17,9 +17,7 @@ executables: []
17
17
  extensions: []
18
18
  extra_rdoc_files: []
19
19
  files:
20
- - lib/poj_org.rb
21
- - lib/poj_org/problem.rb
22
- - lib/poj_org/code.rb
20
+ - ./lib/poj_org.rb
23
21
  homepage: https://github.com/pinepara/poj_org
24
22
  licenses: []
25
23
  post_install_message:
@@ -45,4 +43,3 @@ signing_key:
45
43
  specification_version: 3
46
44
  summary: API for poj.org
47
45
  test_files: []
48
- has_rdoc:
data/lib/poj_org/code.rb DELETED
@@ -1,56 +0,0 @@
1
- require 'open-uri'
2
- require 'cgi'
3
-
4
- # This class handles codes on poj.org
5
- class PojOrg::Code
6
- # Get the url of code via specified ID on poj.org
7
- #
8
- # @param [Integer] id ID of code
9
- #
10
- # @return [String] Url of code
11
- def self.url_for(id)
12
- "http://poj.org/showsource?solution_id=#{id}"
13
- end
14
-
15
- # @return [Integer] ID of code
16
- attr_accessor :id
17
-
18
- # @return [Integer] Author's user name
19
- attr_accessor :user
20
-
21
- # @return [Integer] Time costed in ms
22
- attr_accessor :time_in_ms
23
-
24
- # @return [Integer] Memory costed in KB
25
- attr_accessor :memory_in_kb
26
-
27
- # @return [String] Programming Language in which code is writen
28
- attr_accessor :language
29
-
30
- # @return [String] Source code
31
- attr_accessor :content
32
-
33
- # Get a code from poj.org via ID.
34
- # Need to be autheticated.
35
- #
36
- # @param [Integer] id ID of code
37
- def initialize(id)
38
- self.id = id
39
- if PojOrg::authenticated?
40
- cookie = PojOrg::login
41
- html = open(url, 'Cookie' => cookie).read
42
- self.user = (/<td><b>User:.+?<a.+?>(.+?)<\/a><\/td>/).match(html)[1]
43
- self.time_in_ms = (/<td><b>Time:.+?(\d+)MS<\/td>/).match(html)[1].to_i
44
- self.memory_in_kb = (/<td><b>Memory:.+?(\d+)K<\/td>/).match(html)[1].to_i
45
- self.language = (/<td><b>Language:<\/b>\s*(.+?)<\/td>/).match(html)[1]
46
- self.content = CGI.unescapeHTML((/<pre.+?>(.+)<\/pre>/m).match(html)[1])
47
- end
48
- end
49
-
50
- # Get the url of code on poj.org
51
- #
52
- # @return [String] Url of code
53
- def url
54
- PojOrg::Code.url_for id
55
- end
56
- end
@@ -1,41 +0,0 @@
1
- # This class handles problems on poj.org
2
- class PojOrg::Problem
3
- # Get the url of problem via specified ID on poj.org
4
- #
5
- # @param [Integer] id ID of problem
6
- #
7
- # @return [String] Url of problem
8
- def self.url_for(id)
9
- "http://poj.org/problem?id=#{id}"
10
- end
11
-
12
- # @return [Integer] ID of problem
13
- attr_accessor :id
14
-
15
- # @return [String] Title
16
- attr_accessor :title
17
-
18
- # @return [Integer] Time limit in ms
19
- attr_accessor :time_limit_in_ms
20
-
21
- # @return [Integer] Memory limit in KB
22
- attr_accessor :memory_limit_in_kb
23
-
24
- # Get a problem for poj.org via ID
25
- #
26
- # @param [Integer] id ID of problem
27
- def initialize(id)
28
- self.id = id
29
- html = open(url).read
30
- self.title = html[/(?<=<div class="ptt" lang="en-US">).+?(?=<\/div>)/]
31
- self.time_limit_in_ms = html[/(?<=<b>Time Limit:<\/b>)\s*\d+/].to_i
32
- self.memory_limit_in_kb = html[/(?<=<b>Memory Limit:<\/b>)\s*\d+/].to_i
33
- end
34
-
35
- # Get the url of problem on poj.org
36
- #
37
- # @return [String] Url of problem
38
- def url
39
- PojOrg::Problem.url_for id
40
- end
41
- end