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



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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/sutazekarate/category.rb', line 104

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

  actions_element = Nokogiri::HTML5.fragment(data['detail'])
  action_urls = actions_element.search('a').map do |a|
    a.attr('href')
  end

  has_draw_flat = action_urls.any? do |url|
    url.start_with?('pdf_rozlosovanieexportrobin.php')
  end
  has_draw_ladder = action_urls.any? do |url|
    url.start_with?('sutaze_kategoriarozl.php')
  end
  has_results_ladder = action_urls.any? do |url|
    url.start_with?('sutaze_kategoriarec.php')
  end

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

.find(id) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/sutazekarate/category.rb', line 88

def self.find(id)
  response = HTTP.get("https://www.sutazekarate.sk/sutaze_kategoriazoz.php?k=#{id}")
  html = Nokogiri::HTML5.fragment(response.body.to_s)

  competition_link = html.search('a').find do |x|
    x.attr('href').starts_with?('sutaze_sutazinf.php')
  end
  competition_href = competition_link.attr('href')

  competition_id = Addressable::URI.parse(competition_href).query_values['sutaz']

  Competition.find(competition_id).categories.find do |category|
    category.id.to_s == id.to_s
  end
end

Instance Method Details

#competitorsObject



36
37
38
39
40
41
42
43
44
45
# File 'lib/sutazekarate/category.rb', line 36

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

#draw_ladderObject



59
60
61
62
63
64
# File 'lib/sutazekarate/category.rb', line 59

def draw_ladder
  @draw_ladder ||= begin
    logger.debug("Fetching draw ladder for category #{id}")
    parse_ladder(draw_ladder_url)
  end
end

#draw_ladder_export_urlObject



55
56
57
# File 'lib/sutazekarate/category.rb', line 55

def draw_ladder_export_url
  "https://www.sutazekarate.sk/pdf_rozlosovanieexport.php?id=#{id}"
end

#draw_ladder_urlObject



51
52
53
# File 'lib/sutazekarate/category.rb', line 51

def draw_ladder_url
  "https://www.sutazekarate.sk/sutaze_kategoriarozl.php?k=#{id}"
end

#ladderObject



47
48
49
# File 'lib/sutazekarate/category.rb', line 47

def ladder
  draw_ladder
end

#preloadObject



77
78
79
80
81
82
# File 'lib/sutazekarate/category.rb', line 77

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

#preload!Object



84
85
86
# File 'lib/sutazekarate/category.rb', line 84

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

#results_ladderObject



70
71
72
73
74
75
# File 'lib/sutazekarate/category.rb', line 70

def results_ladder
  @results_ladder ||= begin
    logger.debug("Fetching results ladder for category #{id}")
    parse_ladder(results_ladder_url)
  end
end

#results_ladder_urlObject



66
67
68
# File 'lib/sutazekarate/category.rb', line 66

def results_ladder_url
  "https://www.sutazekarate.sk/sutaze_kategoriarec.php?k=#{id}"
end

#serializable_hash(options = nil) ⇒ Object



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

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

#time_beginObject



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

def time_begin
  time_range&.begin
end

#time_endObject



32
33
34
# File 'lib/sutazekarate/category.rb', line 32

def time_end
  time_range&.end
end