Class: Soccerwiki::Player

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/soccerwiki/player.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id, name, picture, rating, club, nationality, position) ⇒ Player

Returns a new instance of Player.



9
10
11
12
13
14
15
16
17
# File 'lib/soccerwiki/player.rb', line 9

def initialize(id, name, picture, rating, club, nationality, position)
  @id = id
  @full_name = name
  @picture = picture
  @rating = rating.to_i
  @club = club
  @nationality = nationality
  @position = position
end

Instance Attribute Details

#clubObject (readonly)

Returns the value of attribute club.



5
6
7
# File 'lib/soccerwiki/player.rb', line 5

def club
  @club
end

#full_nameObject (readonly)

Returns the value of attribute full_name.



5
6
7
# File 'lib/soccerwiki/player.rb', line 5

def full_name
  @full_name
end

#idObject (readonly)

Returns the value of attribute id.



5
6
7
# File 'lib/soccerwiki/player.rb', line 5

def id
  @id
end

#nationalityObject (readonly)

Returns the value of attribute nationality.



5
6
7
# File 'lib/soccerwiki/player.rb', line 5

def nationality
  @nationality
end

#pictureObject (readonly)

Returns the value of attribute picture.



5
6
7
# File 'lib/soccerwiki/player.rb', line 5

def picture
  @picture
end

#positionObject (readonly)

Returns the value of attribute position.



5
6
7
# File 'lib/soccerwiki/player.rb', line 5

def position
  @position
end

#ratingObject (readonly)

Returns the value of attribute rating.



5
6
7
# File 'lib/soccerwiki/player.rb', line 5

def rating
  @rating
end

Class Method Details

.find_by_id(player_id) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/soccerwiki/player.rb', line 19

def self.find_by_id(player_id)
  req = self.get("/player.php", query: {pid: player_id})
  if req.code.to_i == 404
    raise(Soccerwiki::Exceptions::NotFound, "Could not find player with id='#{player_id}'")
  elsif req.code.to_i == 200
    html_doc = Nokogiri::HTML(req.parsed_response)
    
    headers = html_doc.xpath("//table[@id='realLifeTable']//tr//th").collect(&:text)
    values = html_doc.xpath("//table[@id='realLifeTable']//tr//td").collect(&:text)

    player_data = Hash[headers.zip(values)]

    player_name = player_data["Full Name"]
    player_picture = html_doc.xpath('//div[@id="ppdiv"]/img').first["src"]
    player_rating = html_doc.xpath('//div[@id="smratingdiv"]/font/b').first.text
    player_club = player_data["Club"]
    player_nationality = player_data["Nation"].gsub(" ", '').slice!(0)
    player_position = player_data["Position"]
    
    # player_positions_short = html_doc.xpath('//table[@id="realLifeTable"]//tr[8]//td//acronym').first.text
    self.new(player_id, player_name, player_picture, player_rating, player_club, player_nationality, player_position)
  end
end

.search(player_name) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/soccerwiki/player.rb', line 43

def self.search(player_name)
  puts "Searching player #{player_name}"
  req = self.get("/wiki.php", query: {action: "search", "searchType" => "players", q: player_name})
  if req.code.to_i != 200
    puts "RESPONSE #{req.code}"
    []
  else
    html_doc = Nokogiri::HTML(req.parsed_response)
    puts "Using version #{Soccerwiki::VERSION}"
    puts "Searched #{player_name}"
    players_links = html_doc.xpath('//div[@id="searchResultsDiv"]//table/tr/td[2]/a').collect {|a| a["href"] }
    puts "Found #{players_links.count}"
    players = players_links.collect do |player_link|
      player_id = player_link.match(/\=([0-9]*)/)[1]
      self.find_by_id(player_id)
    end
  end
end