Class: Soccerwiki::Player
- Inherits:
-
Object
- Object
- Soccerwiki::Player
- Includes:
- HTTParty
- Defined in:
- lib/soccerwiki/player.rb
Instance Attribute Summary collapse
-
#club ⇒ Object
readonly
Returns the value of attribute club.
-
#full_name ⇒ Object
readonly
Returns the value of attribute full_name.
-
#id ⇒ Object
readonly
Returns the value of attribute id.
-
#nationality ⇒ Object
readonly
Returns the value of attribute nationality.
-
#picture ⇒ Object
readonly
Returns the value of attribute picture.
-
#position ⇒ Object
readonly
Returns the value of attribute position.
-
#rating ⇒ Object
readonly
Returns the value of attribute rating.
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(id, name, picture, rating, club, nationality, position) ⇒ Player
constructor
A new instance of Player.
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, , club, nationality, position) @id = id @full_name = name @picture = picture @rating = .to_i @club = club @nationality = nationality @position = position end |
Instance Attribute Details
#club ⇒ Object (readonly)
Returns the value of attribute club.
5 6 7 |
# File 'lib/soccerwiki/player.rb', line 5 def club @club end |
#full_name ⇒ Object (readonly)
Returns the value of attribute full_name.
5 6 7 |
# File 'lib/soccerwiki/player.rb', line 5 def full_name @full_name end |
#id ⇒ Object (readonly)
Returns the value of attribute id.
5 6 7 |
# File 'lib/soccerwiki/player.rb', line 5 def id @id end |
#nationality ⇒ Object (readonly)
Returns the value of attribute nationality.
5 6 7 |
# File 'lib/soccerwiki/player.rb', line 5 def nationality @nationality end |
#picture ⇒ Object (readonly)
Returns the value of attribute picture.
5 6 7 |
# File 'lib/soccerwiki/player.rb', line 5 def picture @picture end |
#position ⇒ Object (readonly)
Returns the value of attribute position.
5 6 7 |
# File 'lib/soccerwiki/player.rb', line 5 def position @position end |
#rating ⇒ Object (readonly)
Returns the value of attribute rating.
5 6 7 |
# File 'lib/soccerwiki/player.rb', line 5 def @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"] = 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_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 |