Class: Sutazekarate::Category

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

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Logging

#logger

Class Method Details

.build(data) ⇒ Object



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
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/sutazekarate/category.rb', line 114

def self.build(data)
  detail_element = Nokogiri::HTML5.fragment(data['detail'])
  id = Addressable::URI.parse(detail_element.search('a').first.attr('href')).query_values['k']
  category_element = Nokogiri::HTML5.fragment(data['kategoria'])
  category_span_elements = category_element.search('span')
  name_raw = category_span_elements[0].text.strip
  gender = :unknown
  if category_span_elements[0].attr('class').include?('classzeny')
    gender = :female
  end
  if category_span_elements[0].attr('class').include?('classmuzi')
    gender = :male
  end
  name_match = name_raw.match(/^([^(\/]+?)\s*(?:\(([^)]+)\))?\s*(?:\/([^\/]+)\/)?$/)
  name = name_match[1]
  duration = name_match[3]
  detail_element = category_span_elements[1]
  location = nil
  location_color = nil
  time_range = nil
  if detail_element
    location_color = detail_element.attr('class').match(/c-(\w+)/)[1]
    detail_match = detail_element.text.strip.match(/(.+) \/(.+) - (.+)\//)

    location = detail_match[1]
    time_range = Time.zone.parse(detail_match[2])..Time.zone.parse(detail_match[3])
  end
  discipline = Nokogiri::HTML5.fragment(data['disciplina']).text.strip

  new(
    id:,
    position: data['pc'],
    name:,
    gender:,
    discipline:,
    duration:,
    location:,
    location_color:,
    time_range:,
  )
end

Instance Method Details

#competitorsObject



32
33
34
35
36
37
38
39
40
41
# File 'lib/sutazekarate/category.rb', line 32

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

#ladderObject



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
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
98
99
100
101
# File 'lib/sutazekarate/category.rb', line 43

def ladder
  @ladder ||= begin
    logger.debug("Fetching ladder for category #{id}")
    response = HTTP.get("https://www.sutazekarate.sk/sutaze_kategoriarozl.php?k=#{id}")
    html = Nokogiri::HTML5(response.body.to_s)

    export_element = html.search('a').find do |elem|
      elem.attr('href').start_with?('pdf_rozlosovanieexport.php')
    end
    export_url = "https://www.sutazekarate.sk/#{export_element.attr('href')}"

    stages = []

    stage_index = 0
    loop do
      stage_element = html.search(".stlpecn.posun#{stage_index}").first
      unless stage_element
        break
      end

      pairs = stage_element.search('.obalao').map.with_index do |pair_element, pair_index|
        competitors = pair_element.search('.okienkopavukR').map do |competitor_element|
          id_element = competitor_element.search('#sutaziaci').first
          unless id_element
            next nil
          end

          id = competitor_element.search('#sutaziaci').first.attr('value')
          name = competitor_element.search('.meno').text.strip
          club = competitor_element.search('.klub').text.strip

          Competitor.new(
            id: id,
            name: name,
            club: Club.new(name: club),
          )
        end

        Pair.new(
          index: pair_index,
          competitor1: competitors[0],
          competitor2: competitors[1],
        )
      end

      stages << Stage.new(
        index: stage_index,
        pairs:,
      )

      stage_index += 1
    end

    Ladder.new(
      export_url:,
      stages:,
    )
  end
end

#preloadObject



103
104
105
106
107
108
# File 'lib/sutazekarate/category.rb', line 103

def preload
  [
    async.competitors,
    async.ladder,
  ]
end

#preload!Object



110
111
112
# File 'lib/sutazekarate/category.rb', line 110

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

#serializable_hash(options = nil) ⇒ Object



20
21
22
# File 'lib/sutazekarate/category.rb', line 20

def serializable_hash(options = nil)
  super.merge(time_begin:, time_end:).except('time_range')
end

#time_beginObject



24
25
26
# File 'lib/sutazekarate/category.rb', line 24

def time_begin
  time_range&.begin
end

#time_endObject



28
29
30
# File 'lib/sutazekarate/category.rb', line 28

def time_end
  time_range&.end
end