Soleone-gamefaqs 0.0.2 → 0.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -6,20 +6,13 @@ You can search for games by title and platform, and then view _Reviews_, _FAQs_,
6
6
 
7
7
  h2. Installation
8
8
 
9
- @gem install soleone-gamefaqs --source=http://gems.github.com@
9
+ @gem install Soleone-gamefaqs --source=http://gems.github.com@
10
10
 
11
- h2. Usage
11
+ (Case sensitive!)
12
12
 
13
- <pre>
14
- <code>
15
- require 'gamefaqs'
13
+ h2. Getting started
16
14
 
17
- # and for convenience:
18
- include GameFaqs
19
- </code>
20
- </pre>
21
-
22
- h2. Examples
15
+ @require 'gamefaqs'@
23
16
 
24
17
  h3. Find games and platforms
25
18
 
@@ -30,7 +23,7 @@ h3. Find games and platforms
30
23
 
31
24
  # You can also search starting from the platform
32
25
  snes = GameFaqs::Platform.find("snes")
33
- game = snes.find("super mario land")
26
+ game = snes.find("super mario world")
34
27
  </code>
35
28
  </pre>
36
29
 
@@ -62,4 +55,18 @@ h3. Reviews
62
55
  review.created_at
63
56
  review.author
64
57
  </code>
65
- </pre>
58
+ </pre>
59
+
60
+ h2. Requirements
61
+
62
+ *Hpricot* (will be installed automatically by RubyGems as a dependency)
63
+
64
+ h2. Issues
65
+
66
+ Speed! Because sometimes there have to be multiple webpages parsed at once.
67
+
68
+ For example to retrieve the names of all games for a platform, at least 26 pages need to be parsed (one for each letter of the alphabet). So better caching strategies are something to be worked on in the future.
69
+
70
+ h2. Contribute
71
+
72
+ If you're interested in this kind of stuff and want to help improve this library, feel free to fork this project and send me pull-requests/patches.
@@ -6,7 +6,7 @@ require 'open-uri'
6
6
  require 'date'
7
7
 
8
8
  # load all source files
9
- lib = %w[caching platform game search list review]
9
+ lib = %w[caching platform game search list review random]
10
10
  lib.each { |file| require File.join(File.dirname(__FILE__), 'gamefaqs', file) }
11
11
 
12
12
 
@@ -27,5 +27,8 @@ module GameFaqs
27
27
  sum / reviews(review_type).size.to_f
28
28
  end
29
29
 
30
+ def self.find(game, platform)
31
+ Search.game(game, platform)
32
+ end
30
33
  end
31
34
  end
@@ -0,0 +1,16 @@
1
+ module GameFaqs
2
+ module Random
3
+ extend self
4
+
5
+ # A review for a game
6
+ def review(game, platform)
7
+ game = Search.game(game, platform) if game.is_a?(String)
8
+ game and game.reviews[rand(game.reviews.size)]
9
+ end
10
+
11
+ def one_line_review(game, platform, options={:detailed => false})
12
+ rev = review(game, platform)
13
+ rev and "#{options[:detailed] ? (rev.game.to_s << ': ') : ''}\"#{rev.title}\" - #{rev.score}"
14
+ end
15
+ end
16
+ end
@@ -3,52 +3,66 @@ module GameFaqs
3
3
  extend Caching
4
4
  class SearchException < Exception; end
5
5
 
6
- # throws a SearchError when no exact match is found
7
- def self.game(game_name, platform, refresh=false)
8
- cached_value("search-#{game_name}-#{platform}", nil, refresh) do
9
- platform = Platform.find(platform) unless platform.is_a?(Platform)
10
- games = []
11
- doc = Hpricot(open("#{SEARCH_URL}#{add_params(game_name, platform)}"))
12
- doc.search("//div.head/h1") do |h1|
13
- if h1.inner_html =~ /Best Matches/
14
- h1.search("../../div.body/table") do |table|
15
- table.search("tr/td/a") do |a|
16
- if a.inner_html =~ /#{game_name.split(' ').join('.*')}/i
17
- games << Game.new(a.inner_html.strip, platform, GameFaqs.extract_id(a['href']))
6
+ class << self
7
+ # Find a game by it's name on a certain platform (by name or Platform instance).
8
+ # Per default it returns nil when no game could be found, or if there are multiple matches
9
+ # Throws a detailed SearchError with help when no exact match is found (only when :whiny => true)
10
+ def game(game_name, platform, options={:refresh => false, :whiny => false})
11
+ cached_value("search-#{game_name}-#{platform}", nil, options[:refresh]) do
12
+ platform = self.platform(platform) unless platform.is_a?(Platform)
13
+ games = []
14
+ doc = Hpricot(open("#{SEARCH_URL}#{add_params(game_name, platform)}"))
15
+ doc.search("//div.head/h1") do |h1|
16
+ if h1.inner_html =~ /Best Matches/
17
+ h1.search("../../div.body/table") do |table|
18
+ table.search("tr/td/a") do |a|
19
+ if a.inner_html =~ /#{game_name.split(' ').join('.*')}/i
20
+ games << Game.new(a.inner_html.strip, platform, GameFaqs.extract_id(a['href']))
21
+ end
18
22
  end
19
23
  end
20
24
  end
21
25
  end
22
- end
23
26
 
24
- case games.size
25
- when 1
26
- games.first
27
- when 0
28
- raise SearchException.new("Could not find a game containing the string \"#{game_name}\" on platform \"#{platform}\"!")
29
- else
30
- raise SearchException.new("Found more than one game containing the string \"#{game_name}\": #{games.join(', ')}")
27
+ case games.size
28
+ when 1
29
+ games.first
30
+ when 0
31
+ raise SearchException.new("Could not find a game containing the string \"#{game_name}\" on platform \"#{platform}\"!") if options[:whiny ]
32
+ nil
33
+ else
34
+ raise SearchException.new("Found more than one game containing the string \"#{game_name}\": #{games.join(', ')}") if options[:whiny ]
35
+ nil
36
+ end
31
37
  end
32
38
  end
33
- end
34
39
 
35
- class << self
36
-
37
- def platform(platform_name)
40
+ # Find a platform by name
41
+ # Per default it returns nil when no platform could be found, or if there are multiple matches
42
+ # Throws a detailed SearchError with help when no exact match is found (only when :whiny => true)
43
+ def platform(platform_name, options={:whiny => false})
38
44
  # get by full name (case insensitive)
39
45
  names = List.platforms.select { |p| p.name.downcase == platform_name.downcase }
40
46
  # find other similar if not found exactly one before
47
+ names = List.platforms.select{|p| p.name.downcase =~ /#{platform_name.split(' ').join('.*')}/i} if names.size != 1
48
+ # if still nothing it is probably if you searched for "n64".
41
49
  if names.size != 1
42
- names = List.platforms.select{|p| p.name.downcase =~ /#{platform_name.split(' ').join('.*')}/i}
50
+ platform = ""
51
+ platform_name.each_byte do
52
+ |byte| platform << byte.chr << ".*"
53
+ end
54
+ names = List.platforms.select{ |p| p.name.downcase =~ Regexp.compile(platform) }
43
55
  end
44
-
56
+
45
57
  case names.size
46
58
  when 1
47
59
  names.first
48
60
  when 0
49
- raise SearchException.new("Could not find a platform containing the string \"#{platform_name}\"!")
61
+ raise SearchException.new("Could not find a platform containing the string \"#{platform_name}\"!") if options[:whiny]
62
+ nil
50
63
  else
51
- raise SearchException.new("Found more than one platform containing the string \"#{platform_name}\": #{names.join(', ')}")
64
+ raise SearchException.new("Found more than one platform containing the string \"#{platform_name}\": #{names.join(', ')}") if options[:whiny]
65
+ nil
52
66
  end
53
67
  end
54
68
 
@@ -1,11 +1,16 @@
1
1
  require 'test/unit'
2
- require '../game_faqs'
2
+ require '../lib/gamefaqs'
3
3
 
4
4
  class GameFaqsTest < Test::Unit::TestCase
5
5
  include GameFaqs
6
+
7
+ def setup
8
+ @game ||= Search.game("gothic ii", "pc")
9
+ end
6
10
 
7
- def test_all_platforms
8
- # assert Platform.all.first.is_a?(Platform)
11
+ def test_all_platforms_available
12
+ assert Platform.all.size > 0
13
+ assert Platform.all.first.is_a?(Platform)
9
14
  end
10
15
 
11
16
  def test_search_for_game
@@ -16,14 +21,18 @@ class GameFaqsTest < Test::Unit::TestCase
16
21
  assert_equal "DS", game.platform.name
17
22
  end
18
23
 
19
- def test_game_reviews_count
20
- game = Search.game("Castlevania Order", "ds")
21
- reviews = Review.all_for_game(game)
22
- #reviews.each { |r| puts r }
24
+ def test_game_reviews_available
25
+ reviews = Review.all_for(@game)
26
+ assert reviews.size > 0
27
+ assert reviews.first.is_a?(Review)
28
+ end
29
+
30
+ def test_average_score_available
31
+ assert @game.average_score <= 10
23
32
  end
24
33
 
25
- def test_average_score
26
- game = Search.game("gothic ii", "pc")
27
- puts Review.average_score(game)
34
+ def test_review_score_available
35
+ assert @game.reviews.first.score[/(\d\d?)\/\d\d/]
36
+ assert_equal $1.to_i, @game.reviews.first.score_to_i
28
37
  end
29
38
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: Soleone-gamefaqs
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dennis Theisen
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2008-11-09 00:00:00 -08:00
12
+ date: 2008-11-11 00:00:00 -08:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -38,6 +38,7 @@ files:
38
38
  - lib/gamefaqs/platform.rb
39
39
  - lib/gamefaqs/review.rb
40
40
  - lib/gamefaqs/search.rb
41
+ - lib/gamefaqs/random.rb
41
42
  - README.textile
42
43
  has_rdoc: true
43
44
  homepage: http://github.com/soleone/gamefaqs