Class: SportsDataApi::Ncaamb::Teams

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/sports_data_api/ncaamb/teams.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(xml) ⇒ Teams

Initialize by passing the raw XML returned from the API



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/sports_data_api/ncaamb/teams.rb', line 10

def initialize(xml)
  @teams = []
  xml = xml.first if xml.is_a? Nokogiri::XML::NodeSet
  if xml.is_a? Nokogiri::XML::Element
    xml.xpath('division').each do |division|
      # Division ID, e.g., D1 or D2
      dname = division['alias']

      division.xpath('conference').each do |conference|
        # conference ID, e.g., ATLANTIC, PACIFIC
        cname = conference['alias']

        # Create a new team
        @teams << conference.xpath('team').map { |team| Team.new(team, cname, dname) }
      end
    end
  end

  @teams.flatten!

  uniq_conferences = @teams.map { |team| team.conference.upcase }.uniq
  @allowed_conferences = uniq_conferences.map { |str| str.to_sym }.concat(uniq_conferences.map { |str| str.downcase.to_sym })
  @conferences = uniq_conferences.map { |conf| conf.to_sym }

  uniq_divisions = @teams.map { |team| team.division.upcase }.uniq
  @divisions =  @teams.map { |team| team.division.to_sym }.uniq
  @allowed_divisions = uniq_divisions.map { |str| str.to_sym }.concat(uniq_divisions.map { |str| str.downcase.to_sym })
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/sports_data_api/ncaamb/teams.rb', line 54

def method_missing(method)
  if @allowed_conferences.include?(method)
    @teams.select do |team|
      up = team.conference.upcase.to_sym
      dw = team.conference.downcase.to_sym
      up === method || dw === method
    end
  elsif @allowed_divisions.include?(method)
    @teams.select do |team|
      up = team.division.upcase.to_sym
      dw = team.division.downcase.to_sym
      up === method || dw === method
    end
  end
end

Instance Attribute Details

#conferencesObject (readonly)

Returns the value of attribute conferences.



6
7
8
# File 'lib/sports_data_api/ncaamb/teams.rb', line 6

def conferences
  @conferences
end

#divisionsObject (readonly)

Returns the value of attribute divisions.



6
7
8
# File 'lib/sports_data_api/ncaamb/teams.rb', line 6

def divisions
  @divisions
end

Instance Method Details

#[](search_index) ⇒ Object



39
40
41
42
43
44
# File 'lib/sports_data_api/ncaamb/teams.rb', line 39

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



72
73
74
75
76
77
78
79
80
# File 'lib/sports_data_api/ncaamb/teams.rb', line 72

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

#respond_to?(method) ⇒ Boolean



48
49
50
# File 'lib/sports_data_api/ncaamb/teams.rb', line 48

def respond_to?(method)
  @allowed_conferences.include?(method) || @allowed_divisions.include?(method)
end