poj_org 0.1.1

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.
@@ -0,0 +1,18 @@
1
+ class PojOrg::Problem
2
+ def initialize(id)
3
+ @id = id
4
+ end
5
+
6
+ def url
7
+ "http://poj.org/problem?id=#{@id}"
8
+ end
9
+
10
+ def details
11
+ html = Net::HTTP.get(URI(url))
12
+ details = {
13
+ :title => html[/(?<=<div class="ptt" lang="en-US">).+?(?=<\/div>)/],
14
+ :time_limit_in_ms => html[/(?<=<b>Time Limit:<\/b>)\s*\d+/].to_i,
15
+ :memory_limit_in_kb => html[/(?<=<b>Memory Limit:<\/b>)\s*\d+/].to_i
16
+ }
17
+ end
18
+ end
@@ -0,0 +1,20 @@
1
+ require 'cgi'
2
+
3
+ class PojOrg::Source
4
+ def initialize(id)
5
+ @id = id
6
+ end
7
+
8
+ def details
9
+ return nil unless PojOrg::authenticated?
10
+ cookie = PojOrg::login
11
+ showsource = Net::HTTP.new('poj.org').get("/showsource?solution_id=#{@id}", {'Cookie' => cookie})
12
+ details = {
13
+ :user => (/<td><b>User:.+?<a.+?>(.+?)<\/a><\/td>/).match(showsource.body)[1],
14
+ :time_in_ms => (/<td><b>Time:.+?(\d+)MS<\/td>/).match(showsource.body)[1].to_i,
15
+ :memory_in_kb => (/<td><b>Memory:.+?(\d+)K<\/td>/).match(showsource.body)[1].to_i,
16
+ :language => (/<td><b>Language:<\/b>\s*(.+?)<\/td>/).match(showsource.body)[1],
17
+ :source => CGI.unescapeHTML((/<pre.+?>(.+)<\/pre>/m).match(showsource.body)[1])
18
+ }
19
+ end
20
+ end
data/lib/poj_org.rb ADDED
@@ -0,0 +1,35 @@
1
+ require 'net/http'
2
+
3
+ class PojOrg
4
+ @@user = nil
5
+ @@password = nil
6
+ @@authenticated = false
7
+
8
+ def self.authenticate(user, password)
9
+ if login(user, password)
10
+ @@user = user
11
+ @@password = password
12
+ @@authenticated = true
13
+ else
14
+ @@authenticated = false
15
+ end
16
+ end
17
+
18
+ def self.authenticated?
19
+ @@authenticated
20
+ end
21
+
22
+ private
23
+
24
+ def self.login(user = nil, password = nil)
25
+ login = Net::HTTP.post_form(
26
+ URI('http://poj.org/login'),
27
+ 'user_id1' => user || @@user,
28
+ 'password1' => password || @@password
29
+ )
30
+ login.body['failed'] ? false : login['set-cookie']
31
+ end
32
+ end
33
+
34
+ require 'poj_org/problem'
35
+ require 'poj_org/source'
metadata ADDED
@@ -0,0 +1,47 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: poj_org
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Jun Zhou
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-08-16 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: Fetch details of problem of source code from poj.org
15
+ email: pinepara@gmail.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - lib/poj_org.rb
21
+ - lib/poj_org/problem.rb
22
+ - lib/poj_org/source.rb
23
+ homepage: http://rubygems.org/gems/poj_org
24
+ licenses: []
25
+ post_install_message:
26
+ rdoc_options: []
27
+ require_paths:
28
+ - lib
29
+ required_ruby_version: !ruby/object:Gem::Requirement
30
+ none: false
31
+ requirements:
32
+ - - ! '>='
33
+ - !ruby/object:Gem::Version
34
+ version: '0'
35
+ required_rubygems_version: !ruby/object:Gem::Requirement
36
+ none: false
37
+ requirements:
38
+ - - ! '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ requirements: []
42
+ rubyforge_project:
43
+ rubygems_version: 1.8.24
44
+ signing_key:
45
+ specification_version: 3
46
+ summary: API for poj.org
47
+ test_files: []