module ESPN::Mapper
Public: A module that handles mapping to make the ESPN
API a little easier to digest.
Examples
class Client include ESPN::Mapper end
Constants
- LEAGUE_MAP
Public: Map all common leagues to sports.
Public Instance Methods
extract_sport_and_league(args, opts={})
click to toggle source
Public: Get league and sport from args array. If sport or league is passed in the opts hash, they will override any mappings from the args Array.
args - The Array to extract the league and sport from. opts - The Hash that will override all mappings if possible.
Examples
extract_sport_and_league([:mlb]) # => 'baseball', 'mlb' extract_sport_and_league(['horse-racing']) # => 'horse-racing', '' extract_sport_and_league(['baseball', 'mlb'], sport: 'basketball') # => 'basketball', 'mlb'
Returns two Strings.
# File lib/espn/mapper.rb, line 120 def extract_sport_and_league(args, opts={}) sport, league = opts[:sport], opts[:league] if args.size == 1 && league?(args[0]) map = map_league_to_sport(args[0]) sport ||= map[:sport].to_s league ||= map[:league].to_s elsif args.size == 1 && sport?(args[0]) sport ||= args[0].to_s league ||= '' elsif !opts[:league].to_s.empty? map = map_league_to_sport(opts[:league]) sport ||= map[:sport].to_s else sport ||= args[0].to_s || '' league ||= args[1].to_s || '' end return sport, league end
league?(test)
click to toggle source
Public: Determine if the test value is a valid league.
test - The String or Symbol to test.
Returns a Boolean.
# File lib/espn/mapper.rb, line 98 def league?(test) LEAGUE_MAP.values.flatten.include?(test.to_s) end
map_league_to_sport(league)
click to toggle source
Public: Map a league to a sport.
league - The league (String or Symbol) to map to a sport.
Examples
map_league_to_sport(:nhl) # => { league: 'nhl', sport: 'hockey' } map_league_to_sport('mlb') # => { league: 'mlb', sport: 'baseball' } map_league_to_sport('some-random-league') # => { league: 'some-random-league', sport: '' }
Returns a Hash.
# File lib/espn/mapper.rb, line 71 def map_league_to_sport(league) result = { league: league.to_s, sport: nil } LEAGUE_MAP.each do |sport, leagues| if leagues.include? result[:league] result[:sport] = sport.to_s break end end result end
sport?(test)
click to toggle source
Public: Determine if the value is a valid sport.
test - The String or Symbol to test.
Returns a Boolean.
# File lib/espn/mapper.rb, line 89 def sport?(test) LEAGUE_MAP.keys.include?(test.to_sym) end