Class: Sutazekarate::Competition

Inherits:
Object
  • Object
show all
Extended by:
Logging
Includes:
ActiveModel::Attributes, ActiveModel::Model, ActiveModel::Serializers::JSON, Concurrent::Async, Logging
Defined in:
lib/sutazekarate/competition.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Logging

logger

Class Method Details

.all(year: Date.today.year) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/sutazekarate/competition.rb', line 58

def self.all(year: Date.today.year)
  logger.debug("Fetching competitions for year #{year}")
  response = HTTP.get("https://www.sutazekarate.sk/ajax/av_sutaze.php?lang=sk&r=#{year}&sort=datum&order=desc&limit=1000&offset=0")
  rows = JSON.parse(response.body.to_s)['rows']
  rows.map do |row|
    starts_at = Date.parse(row['datum'])
    content = Nokogiri::HTML5.fragment(row['obsah'])

    name = content.search('h4').first.text.strip
    club_element = content.search('h5').first
    club = club_element.text.strip
    id = Addressable::URI.parse(content.search('a').first.attr('href')).query_values['sutaz']
    location = club_element.next_sibling.text.strip
    note_element = content.search('br').first.next_sibling
    note = if note_element.text?
      note_element.text
    else
      nil
    end

    registration_info_element = content.search('span').find do |elem|
      elem.text.include?('Registrácia: ')
    end
    registration_info = registration_info_element.text
    registration_info_match = registration_info.match(/Registrácia: (.+) - (.+)/)
    registration_starts_at = Date.parse(registration_info_match[1])
    registration_ends_at = Date.parse(registration_info_match[2])

    Competition.new(
      id:,
      starts_at:,
      name:,
      club:,
      location:,
      note:,
      registration_starts_at:,
      registration_ends_at:,
    )
  end
end

Instance Method Details

#categoriesObject



24
25
26
27
28
29
30
31
32
33
# File 'lib/sutazekarate/competition.rb', line 24

def categories
  @categories ||= begin
    logger.debug("Fetching categories for competition #{id}")
    response = HTTP.get("https://www.sutazekarate.sk/ajax/av_sutazkat.php?lang=sk&sutaz=#{id}&order=asc&limit=1000&offset=0")
    rows = JSON.parse(response.body.to_s)
    rows.map do |row|
      Category.build(row)
    end
  end
end

#preloadObject



35
36
37
38
39
# File 'lib/sutazekarate/competition.rb', line 35

def preload
  categories.map do |category|
    category.async.preload
  end.flatten
end

#preload!Object



41
42
43
# File 'lib/sutazekarate/competition.rb', line 41

def preload!
  preload.map(&:value)
end

#timetablesObject



45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/sutazekarate/competition.rb', line 45

def timetables
  @timetables ||= begin
    logger.debug("Fetching timetable for competition #{id}")
    response = HTTP.get("https://sutazekarate.sk/pdf_timetable3.php?sutaz=#{id}")

    html = Nokogiri::HTML5.fragment(response.body.to_s)

    html.search('.divttm').map do |location_element|
      Timetable.build(location_element, categories:)
    end
  end
end