Skip to content

Instantly share code, notes, and snippets.

@lsantos
Created June 17, 2011 14:52
Show Gist options
  • Save lsantos/1031572 to your computer and use it in GitHub Desktop.
Save lsantos/1031572 to your computer and use it in GitHub Desktop.
Fix for pga failures when expiring the cache
module IPad::Api
class UrlMapper
class << self
def urls_for(args, options = {:include_base_url => false})
league, model, id, *extra = args
object = get_model(league, model, id)
model_urls(league, model).map { |url|
instance_eval("\"#{url}\"") unless (url.match(/\#\{id\}/) && id.blank?) || (url.match(/\#\{league\}/) && league.blank?)
}.compact + self.mapped_urls_for(args)
end
def get_model(league, model, id)
return nil if id.blank?
if league.blank? || model.index('::')
model.constantize.find(id)
else
"#{Sport.sport_by_web_name(league)}::#{model.demodulize}".constantize.find(id)
end
rescue
nil
end
def base_url
@@base_url ||= ServerStack.config['base_url']
end
def model_urls(league, model)
models = []
models << model.demodulize unless league.to_s.downcase == 'pga'
models << model
models << "#{Sport.sport_by_web_name(league)}::#{model.demodulize}"
urls = models.uniq.map {|m|
IPAD_URL_MAPPING[m] unless IPAD_URL_MAPPING[m].blank?
}.flatten.uniq.compact
end
def mapped_urls_for(args)
league, model, id, *extra = args
urls = []
urls << self.send("#{model.underscore}_urls", league, id, *extra) if self.respond_to?("#{model.underscore}_urls")
urls << self.send("#{league}_#{model.underscore}_urls", id, *extra) if self.respond_to?("#{league}_#{model.underscore}_urls")
urls.flatten.uniq
end
def event_urls(league, id, *args)
urls = IPad::Util.helpers.today_games_nav_links.map {|hash|
hash[:url][/(\/api\/.*)/,1] if hash[:title].downcase == league.downcase
}.compact
end
def nfl_game_tracker_urls(id, *args)
urls = ["/api/nfl/game_tracker/show?id=#{id}"]
event = Football::Event.find(id)
plays = event.play_by_play_records.group_by(&:drive_id)
drives = plays.keys.sort[-2..-1] || []
urls += drives.map{|drive_id|
"/api/nfl/game_tracker/show?id=#{id}&since_id=#{plays[drive_id].last.id}"
}
urls
end
def ncaaf_event_urls(id, *args)
event = Football::Event.find(id)
event.teams.map(&:conf).uniq.map { |conf|
"/api/ncaaf/scores/conference?conf=#{conf}" unless conf.blank?
}.compact
end
def ncaab_event_urls(id, *args)
event = Basketball::Event.find(id)
event.teams.map(&:conf).uniq.map { |conf|
"/api/ncaab/scores/conference?conf=#{conf}" unless conf.blank?
}.compact
end
def nassp_race_urls(id, *args)
event = Auto::Event.find(id)
[
"/api/nassp/scores/index",
"/api/nassp/scores/standings",
"/api/nassp/scores/race?id=#{id}",
("/api/nassp/scores/leader_stints?id=#{id}" if event.leader_stints.any?),
("/api/nassp/scores/race_results?id=#{id}" if event.driver_records.any?),
("/api/nassp/scores/qualifying?id=#{id}" if event.participations.any?)
].compact
end
def f1_race_urls(id, *args)
event = Auto::Event.find(id)
[
"/api/f1/scores/index",
"/api/f1/scores/standings",
"/api/f1/scores/race?id=#{id}",
("/api/f1/scores/leader_stints?id=#{id}" if event.leader_stints.any?),
("/api/f1/scores/race_results?id=#{id}" if event.driver_records.any?),
("/api/f1/scores/qualifying?id=#{id}" if event.participations.any?)
].compact
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment