Class: SportsDataApi::Nfl::Teams

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(teams_hash) ⇒ Teams

Returns a new instance of Teams.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/sports_data_api/nfl/teams.rb', line 8

def initialize(teams_hash)
  @teams = []
  teams_hash['conferences'].each do |conference_hash|
    # Conference ID, e.g., AFC or NFC
    cid = conference_hash['id']

    conference_hash['divisions'].each do |division_hash|
      # Division ID, e.g., AFC_EAST, NFC_NORTH
      did = division_hash['id']

      # Create a new team
      @teams << division_hash['teams'].map { |team_hash| Team.new(team_hash, cid, did) }
    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



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/sports_data_api/nfl/teams.rb', line 49

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/nfl/teams.rb', line 6

def conferences
  @conferences
end

#divisionsObject (readonly)

Returns the value of attribute divisions.



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

def divisions
  @divisions
end

Instance Method Details

#[](search_index) ⇒ Object



34
35
36
37
38
39
# File 'lib/sports_data_api/nfl/teams.rb', line 34

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



67
68
69
70
71
72
73
74
75
# File 'lib/sports_data_api/nfl/teams.rb', line 67

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

#respond_to?(method) ⇒ Boolean

Returns:

  • (Boolean)


43
44
45
# File 'lib/sports_data_api/nfl/teams.rb', line 43

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