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



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/sutazekarate/competition.rb', line 99

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'])
    image = Nokogiri::HTML5.fragment(row['obrazok'])

    image_path = image.search('img').attr('src').value
    image_url = "https://www.sutazekarate.sk/#{image_path}"
    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:,
      image_url:,
      starts_at:,
      name:,
      club:,
      location:,
      note:,
      registration_starts_at:,
      registration_ends_at:,
    )
  end
end

.find(id) ⇒ Object



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 60

def self.find(id)
  logger.debug("Fetching competition with id #{id}")

  response = HTTP.get("https://www.sutazekarate.sk/sutaze_sutazinf.php?sutaz=#{id}")
  html = Nokogiri::HTML5.fragment(response.body.to_s)

  image_path = html.search('img.img-responsive.no-margin').attr('src').value
  image_url = "https://www.sutazekarate.sk/#{image_path}"
  name = html.search('h3.section-title-inner').text.strip.rpartition(' - ').first
  club = html.search('.table1 tr:nth-child(1) td:nth-child(2)').text.strip

  starts_at = Date.parse(html.search('.table1 tr:nth-child(2)  td:nth-child(2)').text.strip)

  registration_info = html.search('.table1 tr:nth-child(4) td:nth-child(2)').text.strip
  registration_starts_at, registration_ends_at = registration_info.split(' - ').map do |date|
    Date.parse(date)
  end

  attachments = html.search('.table1 tr').reduce([]) do |acc, row|
    acc += row.search('td:nth-child(2) a[target="_blank"]').map do |a|
      "https://www.sutazekarate.sk/#{a.attr('href')}"
    end
      .grep_v(/\.php/)

    acc
  end

  Competition.new(
    id:,
    image_url:,
    starts_at:,
    name:,
    club:,
    registration_starts_at:,
    registration_ends_at:,
    attachments:,
  )
end

Instance Method Details

#categoriesObject



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

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



37
38
39
40
41
# File 'lib/sutazekarate/competition.rb', line 37

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

#preload!Object



43
44
45
# File 'lib/sutazekarate/competition.rb', line 43

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

#timetablesObject



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

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