Class: SportsDataApi::Mlb::Players

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/sports_data_api/mlb/players.rb

Instance Method Summary collapse

Constructor Details

#initialize(xml) ⇒ Players

Initialize by passing the raw XML returned from the API



8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/sports_data_api/mlb/players.rb', line 8

def initialize(xml)
  @players = []
  xml = xml.first if xml.is_a? Nokogiri::XML::NodeSet
  if xml.is_a? Nokogiri::XML::Element
    xml.xpath('team').each do |team|
      team.xpath('players').xpath('player').each do |player|
        p = Player.new(player, team['id'])
        @players << p
      end
    end
  end
  @players
end

Instance Method Details

#[](search_index) ⇒ Object



22
23
24
25
26
27
# File 'lib/sports_data_api/mlb/players.rb', line 22

def [](search_index)
  found_index = @teams.index(search_index)
  unless found_index.nil?
    @teams[found_index]
  end
end

#each(&block) ⇒ Object

Make the class Enumerable



31
32
33
34
35
36
37
38
39
# File 'lib/sports_data_api/mlb/players.rb', line 31

def each(&block)
  @players.each do |player|
    if block_given?
      block.call player
    else
      yield player
    end
  end
end