Class: Zettacode::Scrap

Inherits:
Object
  • Object
show all
Defined in:
lib/zettacode/scrap/scrap.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(filepath) ⇒ Scrap

Returns a new instance of Scrap.



9
10
11
# File 'lib/zettacode/scrap/scrap.rb', line 9

def initialize(filepath)
  @configpath = filepath
end

Instance Attribute Details

#settingsObject (readonly)

Returns the value of attribute settings.



7
8
9
# File 'lib/zettacode/scrap/scrap.rb', line 7

def settings
  @settings
end

Instance Method Details

#find_langsObject



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/zettacode/scrap/scrap.rb', line 23

def find_langs
  # Scrap page
  @settings.each do |problem|
    # 1. Get page
    response = HTTParty.get(problem[:url])
    unless response.code == 200
      puts "==> httparty: [Error] #{response.code}"
      exit 1
    end

    # 2. Find every LANG (a href ="#LANG")
    langs = []
    document = Nokogiri::HTML(response.body)
    document.css("h2 > span").each do |e|
      id = e.attribute("id")
      langs << id unless id.nil?
    end
    puts "==> Problem: #{problem[:name]} (langs=#{langs.size})"
    puts langs.join("\n")

    langs = []
    elems = document.css("a")
    elems.each do |e|
      href = e.attribute("href")
      filter = /\/wiki\/Category:([\w\d.-_]+)/
      items = filter.match(href)&.captures
      langs << items.first unless items.nil?
    end
    puts "==> Problem: #{problem[:name]} (langs=#{langs.size})"
    puts langs.join("\n")
  end
end

#load_settingsObject



13
14
15
16
17
18
19
20
21
# File 'lib/zettacode/scrap/scrap.rb', line 13

def load_settings
  puts "==> Loading settings (#{@configpath})"
  @settings = YAML.load(File.read(@configpath))
  # Show settings
  @settings.each_with_index do |problem, index|
    number = "%02d" % (index + 1)
    puts "    #{number} #{problem[:name]}: #{problem[:url]}"
  end
end