Class: NpbApi::Player

Inherits:
Object
  • Object
show all
Defined in:
lib/npb-api/player.rb

Constant Summary collapse

TEAMS =
%w[g t c d db s h bs f m l e]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id: nil, note: nil) ⇒ Player

Returns a new instance of Player.



38
39
40
41
# File 'lib/npb-api/player.rb', line 38

def initialize(id: nil, note: nil)
  @id = id
  @note = note
end

Instance Attribute Details

#noteObject

Returns the value of attribute note.



36
37
38
# File 'lib/npb-api/player.rb', line 36

def note
  @note
end

Class Method Details

.allObject



20
21
22
23
24
# File 'lib/npb-api/player.rb', line 20

def self.all
  TEAMS.each_with_object([]) do |team, arr|
    arr << list(team: team)
  end.flatten
end

.list(team: nil) ⇒ Object

Raises:



11
12
13
14
15
16
17
18
# File 'lib/npb-api/player.rb', line 11

def self.list(team: nil)
  raise UnknownTeamError unless TEAMS.include?(team)
  source(team).css('tr.rosterPlayer').each_with_object([]) do |row, arr|
    next if row.css('td.rosterRegister a').empty?
    row.css('td.rosterRegister a').attr('href').text =~ /\/players\/(\d+)\.html/
    arr << new(id: $1, note: row.css('td.rosterdetail').text)
  end
end

Instance Method Details

#birthdayObject



63
64
65
66
# File 'lib/npb-api/player.rb', line 63

def birthday
  detail[1].text.split('  ')[0] =~ /\A(\d{4})年(\d{1,2})月(\d{1,2})日生\z/
  Date.new($1.to_i, $2.to_i, $3.to_i)
end

#careerObject



82
83
84
# File 'lib/npb-api/player.rb', line 82

def career
  detail[2].text
end

#draftObject



86
87
88
# File 'lib/npb-api/player.rb', line 86

def draft
  detail[3].text
end

#handObject



78
79
80
# File 'lib/npb-api/player.rb', line 78

def hand
  detail[1].text.split('  ')[3]
end

#heightObject



68
69
70
71
# File 'lib/npb-api/player.rb', line 68

def height
  detail[1].text.split('  ')[1] =~ /\A身長(\d{3})cm\z/
  $1.to_i
end

#kanaObject



59
60
61
# File 'lib/npb-api/player.rb', line 59

def kana
  detail[0].text
end

#nameObject



47
48
49
# File 'lib/npb-api/player.rb', line 47

def name
  header.css('.registerPlayer').text
end

#numberObject



51
52
53
# File 'lib/npb-api/player.rb', line 51

def number
  header.css('.registerNo').text
end

#positionObject



55
56
57
# File 'lib/npb-api/player.rb', line 55

def position
  header.css('.registerPosition').text
end

#teamObject



43
44
45
# File 'lib/npb-api/player.rb', line 43

def team
  header.css('.registerTeam').text
end

#weightObject



73
74
75
76
# File 'lib/npb-api/player.rb', line 73

def weight
  detail[1].text.split('  ')[2] =~ /\A体重(\d{2,3})kg\z/
  $1.to_i
end