Class: SquashMatrix::NokogiriParser

Inherits:
Object
  • Object
show all
Defined in:
lib/squash_matrix/nokogiri-parser.rb

Class Method Summary collapse

Class Method Details

.get_club_info(body) ⇒ Object



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
# File 'lib/squash_matrix/nokogiri-parser.rb', line 63

def get_club_info(body)
  html = Nokogiri::HTML(body)
  name = SquashMatrix::Constants::CLUB_FROM_TITLE_REGEX.match(html.css('title').text)[1]
  players = html.xpath('//div[@id="Rankings"]//div[@class="columnmain"]//table[@class="alternaterows"]//tbody//tr')&.map do |r|
    player_path = r.css('td[2]//a').attribute('href').value
    rank = r.css('td[1]').text
    rtn = {
      name: r.css('td[2]').text,
      rating: r.css('td[3]').text.to_f
    }
    rtn[:rank] = rank.to_i if rank
    rtn[:id] = SquashMatrix::Constants::PLAYER_FROM_PATH_REGEX.match(player_path)[1].to_i if player_path
    rtn
  end
  juniors = html.xpath('//div[@id="Rankings"]//div[@class="columnside"]//table[@class="alternaterows"]//tbody//tr')&.map do |r|
    player_path = r.css('td[2]//a').attribute('href').value
    rank = r.css('td[1]').text
    rtn = {
      name: r.css('td[2]').text,
      rating: r.css('td[3]').text.to_f
    }
    rtn[:rank] = rank.to_i if rank
    rtn[:id] = SquashMatrix::Constants::PLAYER_FROM_PATH_REGEX.match(player_path)[1].to_i if player_path
    rtn
  end
  {
    name: name,
    players: players.compact,
    juniors: juniors.compact
  }
end

.get_log_on_error(body) ⇒ Object



114
115
116
# File 'lib/squash_matrix/nokogiri-parser.rb', line 114

def get_log_on_error(body)
  Nokogiri::HTML(body)&.xpath('//div[@class="validation-summary-errors"]//ul//li')&.map(&:content)
end

.get_player_info(body) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/squash_matrix/nokogiri-parser.rb', line 34

def get_player_info(body)
  bc = Nokogiri::HTML(body)&.xpath('//div[@id="bodycontent"]')
  name = bc.children[3].text
  rows = bc.xpath('//div[@id="Summary"]//table[@id="profile"]//tbody//tr')
  rating = rows[1]&.css('td[2]')&.text
  clubs = rows[2]&.css('td[2]')&.css('ul//li')&.map do |c|
    id = c&.css('a')&.attribute('href')&.text
    rtn = {
      name: c&.text
    }
    rtn[:id] = SquashMatrix::Constants::CLUB_FROM_PATH_REGEX.match(id)[1].to_i if id
    rtn
  end
  teams = rows[3]&.css('td[2]')&.css('ul//li')&.map do |c|
    id = c&.css('a')&.attribute('href')&.text
    rtn = {
      name: c&.text
    }
    rtn[:id] = SquashMatrix::Constants::TEAM_FROM_PATH_REGEX.match(id)[1].to_i if id
    rtn
  end
  {
    name: name,
    rating: rating,
    clubs: clubs,
    teams: teams
  }
end

.get_player_results(body) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/squash_matrix/nokogiri-parser.rb', line 9

def get_player_results(body)
  rtn = Nokogiri::HTML(body)&.xpath('//table[@id="results"]//tbody//tr')&.map do |r|
    date = r.at_css('td[1]')&.content
    opponent_id = r.at_css('td[10]//a')&.attribute('href')&.content
    match_id = r.at_css('td[12]//a')&.attribute('href')&.content
    rtn = {
      event: r.at_css('td[2]')&.content,
      division: r.at_css('td[3]')&.content,
      round: r.at_css('td[4]')&.content,
      position: r.at_css('td[5]')&.content,
      games: r.at_css('td[6]')&.content,
      points: r.at_css('td[7]')&.content,
      rating_adjustment: r.at_css('td[8]')&.content&.to_f,
      rating: r.at_css('td[9]')&.content&.to_f,
      opponent_rating: r.at_css('td[11]')&.content&.to_f,
      opponent_name: r.at_css('td[10]//a')&.content
    }
    rtn[:date] = Date.parse(date) if date
    rtn[:opponent_id] = SquashMatrix::Constants::PLAYER_FROM_PATH_REGEX.match(opponent_id)[1].to_i if opponent_id
    rtn[:match_id] = SquashMatrix::Constants::MATCH_FROM_PATH_REGEX.match(match_id)[1].to_i if match_id
    rtn.values.any?(&:nil?) ? nil : rtn
  end
  rtn.compact
end

.get_search_results(body) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/squash_matrix/nokogiri-parser.rb', line 95

def get_search_results(body)
  bc = Nokogiri::HTML.parse(body).at_xpath('//div[@id="bodycontent"]')&.children
  return unless bc&.length
  rtn = {}
  bc&.each_with_index do |c, i|
    if c.name == 'h2'
      case c.text
      when 'Players'
        rtn[:players] = get_players_from_search_results(bc[i + 2]) if bc[i + 2].children.length
      when 'Teams'
        rtn[:teams] = get_teams_from_search_results(bc[i + 2]) if bc[i + 2].children.length
      when 'Clubs'
        rtn[:clubs] = get_clubs_from_search_results(bc[i + 2]) if bc[i + 2].children.length
      end
    end
  end
  rtn
end