Class: Jtest::Problem
- Inherits:
-
Object
- Object
- Jtest::Problem
- Defined in:
- lib/jtest/problem.rb
Constant Summary collapse
- URL =
"https://www.jutge.org/problems/"
- REGEXPS =
{ :title => /<title>Jutge :: Problem (.+?)<\/title>/, :samples => /<pre.+?>(.+?)<\/pre>/m, :dir_id => /[0-9]+/, :invalid_url => "Wrong URL." }
- DEFAULT_OPTIONS =
{:lang => 'en', :prefix => 'P', :dirname => nil}
Instance Attribute Summary collapse
-
#dirname ⇒ Object
readonly
Returns the value of attribute dirname.
-
#id ⇒ Object
readonly
Returns the value of attribute id.
-
#samples ⇒ Object
readonly
Returns the value of attribute samples.
-
#title ⇒ Object
readonly
Returns the value of attribute title.
Class Method Summary collapse
Instance Method Summary collapse
- #connect ⇒ Object
- #exists? ⇒ Boolean
-
#initialize(id, options = {}) ⇒ Problem
constructor
A new instance of Problem.
- #retrieve_info ⇒ Object
Constructor Details
#initialize(id, options = {}) ⇒ Problem
Returns a new instance of Problem.
20 21 22 23 24 25 26 27 |
# File 'lib/jtest/problem.rb', line 20 def initialize(id, = {}) = DEFAULT_OPTIONS.merge() @id = [:prefix] + id.to_s + '_' + [:lang] @title = nil @samples = [] @dirname = [:dirname] end |
Instance Attribute Details
#dirname ⇒ Object (readonly)
Returns the value of attribute dirname.
18 19 20 |
# File 'lib/jtest/problem.rb', line 18 def dirname @dirname end |
#id ⇒ Object (readonly)
Returns the value of attribute id.
15 16 17 |
# File 'lib/jtest/problem.rb', line 15 def id @id end |
#samples ⇒ Object (readonly)
Returns the value of attribute samples.
17 18 19 |
# File 'lib/jtest/problem.rb', line 17 def samples @samples end |
#title ⇒ Object (readonly)
Returns the value of attribute title.
16 17 18 |
# File 'lib/jtest/problem.rb', line 16 def title @title end |
Class Method Details
.find(name) ⇒ Object
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/jtest/problem.rb', line 68 def find(name) list_entry = / <td.+?> # Before problem id (P[0-9]+) # Capture problem id <\/.+?td> # After problem id \n # Line break \s+? # Possible whitespaces <td.+?> # Before problem title ( # Capture problem title [^<>]*? # All characters except HTML tags #{Regexp.quote(name)} # Match the given name [^<>]*? # Same as before ) # End capture <\/.+?td>/x # After problem title source = open(URL, :ssl_verify_mode => OpenSSL::SSL::VERIFY_NONE).read problems = source.scan(list_entry).flatten.each_slice(2).to_a return problems end |
.select(ids, options = {}) ⇒ Object
53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/jtest/problem.rb', line 53 def select(ids, = {}) = DEFAULT_OPTIONS.merge() dirs = dirs_match(ids) problems = [] dirs.each do |dir| REGEXPS[:dir_id].match(dir) do |match| [:dirname] = dir problems << Problem.new(match, ) unless match.nil? end end return problems end |
Instance Method Details
#connect ⇒ Object
29 30 31 32 |
# File 'lib/jtest/problem.rb', line 29 def connect url = URL + @id @source = open(url, :ssl_verify_mode => OpenSSL::SSL::VERIFY_NONE).read end |
#exists? ⇒ Boolean
34 35 36 37 38 |
# File 'lib/jtest/problem.rb', line 34 def exists? connect if @source.nil? not @source.include? REGEXPS[:invalid_url] end |
#retrieve_info ⇒ Object
40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/jtest/problem.rb', line 40 def retrieve_info connect return false unless exists? @title = @source.scan(REGEXPS[:title])[0][0] @samples = @source.scan(REGEXPS[:samples]).flatten.each_slice(2).to_a @dirname = @title.gsub(/_(.+?)\:/, '').gsub(/\s/, '_') if @dirname.nil? return true end |