Class: Jtest::Problem

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

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, options = {})
  options = DEFAULT_OPTIONS.merge(options)

  @id = options[:prefix] + id.to_s + '_' + options[:lang]
  @title = nil
  @samples = []
  @dirname = options[:dirname]
end

Instance Attribute Details

#dirnameObject (readonly)

Returns the value of attribute dirname.



18
19
20
# File 'lib/jtest/problem.rb', line 18

def dirname
  @dirname
end

#idObject (readonly)

Returns the value of attribute id.



15
16
17
# File 'lib/jtest/problem.rb', line 15

def id
  @id
end

#samplesObject (readonly)

Returns the value of attribute samples.



17
18
19
# File 'lib/jtest/problem.rb', line 17

def samples
  @samples
end

#titleObject (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, options = {})
  options = DEFAULT_OPTIONS.merge(options)
  dirs = dirs_match(ids)

  problems = []
  dirs.each do |dir|
    REGEXPS[:dir_id].match(dir) do |match|
      options[:dirname] = dir
      problems << Problem.new(match, options) unless match.nil?
    end
  end

  return problems
end

Instance Method Details

#connectObject



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

Returns:

  • (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_infoObject



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