Module: GameFaqs::Search
- Extended by:
- Caching
- Defined in:
- lib/gamefaqs/search.rb
Defined Under Namespace
Classes: SearchException
Class Method Summary collapse
-
.game(game_name, platform, options = {:refresh => false, :whiny => false}) ⇒ Object
Find a game by it’s name on a certain platform (by name or Platform instance).
-
.platform(platform_name, options = {:whiny => false}) ⇒ Object
Find a platform by name Per default it returns nil when no platform could be found, or if there are multiple matches Throws a detailed SearchError with help when no exact match is found (only when :whiny => true).
Class Method Details
.game(game_name, platform, options = {:refresh => false, :whiny => false}) ⇒ Object
Find a game by it’s name on a certain platform (by name or Platform instance). Per default it returns nil when no game could be found, or if there are multiple matches Throws a detailed SearchError with help when no exact match is found (only when :whiny => true)
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 38 |
# File 'lib/gamefaqs/search.rb', line 10 def game(game_name, platform, ={:refresh => false, :whiny => false}) cached_value("search-#{game_name.to_s.downcase} [#{platform.to_s.downcase}]", nil, [:refresh]) do platform = self.platform(platform) unless platform.is_a?(Platform) games = [] doc = Hpricot(open("#{SEARCH_URL}#{add_params(game_name, platform)}")) doc.search("//div.head/h1") do |h1| if h1.inner_html =~ /Best Matches/ h1.search("../../div.body/table") do |table| table.search("tr/td/a") do |a| if a.inner_html =~ /#{game_name.split(' ').join('.*')}/i games << Game.new(a.inner_html.strip, platform, GameFaqs.extract_id(a['href'])) end end end end end case games.size when 1 games.first when 0 raise SearchException.new("Could not find a game containing the string \"#{game_name}\" on platform \"#{platform}\"!") if [:whiny ] nil else raise SearchException.new("Found more than one game containing the string \"#{game_name}\": #{games.join(', ')}") if [:whiny ] nil end end end |
.platform(platform_name, options = {:whiny => false}) ⇒ Object
Find a platform by name Per default it returns nil when no platform could be found, or if there are multiple matches Throws a detailed SearchError with help when no exact match is found (only when :whiny => true)
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/gamefaqs/search.rb', line 43 def platform(platform_name, ={:whiny => false}) # get by full name (case insensitive) names = List.platforms.select { |p| p.name.downcase == platform_name.downcase } # find other similar if not found exactly one before names = List.platforms.select{|p| p.name =~ /#{platform_name.split(' ').join('.*')}/i} if names.size != 1 # if still nothing it is probably if you searched for "n64". if names.size != 1 platform_regexp = "" platform_name.each_byte { |byte| platform_regexp << byte.chr << ".*" } names = List.platforms.select{ |p| p.name =~ /#{platform_regexp}/i } end case names.size when 1 names.first when 0 raise SearchException.new("Could not find a platform containing the string \"#{platform_name}\"!") if [:whiny] nil else raise SearchException.new("Found more than one platform containing the string \"#{platform_name}\": #{names.join(', ')}") if [:whiny] nil end end |