scapeshift 1.0.1rg0

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.
@@ -0,0 +1,85 @@
1
+ module Scapeshift
2
+
3
+ ##
4
+ # Contains any custom exceptions this gem might raise.
5
+ #
6
+ # @author Josh Lindsey
7
+ #
8
+ # @since 0.1.0
9
+ #
10
+ module Errors
11
+
12
+ ##
13
+ # Raised when an unrecognized crawler is specified in
14
+ # {Scapeshift::Crawler.crawl}
15
+ #
16
+ # @author Josh Lindsey
17
+ #
18
+ # @since 0.1.0
19
+ #
20
+ class InvalidCrawlerType < StandardError; end
21
+
22
+ ##
23
+ # Raised when an unknown card attribute is encountered in
24
+ # {Scapeshift::Crawlers::Cards#_parse_row}
25
+ #
26
+ # @author Josh Lindsey
27
+ #
28
+ # @since 0.1.0
29
+ #
30
+ class UnknownCardAttribute < StandardError; end
31
+
32
+ ##
33
+ # Raised when an unknown metadata type is supplied to
34
+ # the Meta crawler.
35
+ #
36
+ # @author Josh Lindsey
37
+ #
38
+ # @since 0.1.4
39
+ #
40
+ class UnknownMetaType < StandardError; end
41
+
42
+ ##
43
+ # Raised when the card name supplied to the Single crawler
44
+ # resolves to a Gatherer search results page instead of a
45
+ # Card detail page (implying that the Card name is ambiguous,
46
+ # or doesn't exist).
47
+ #
48
+ # @author Josh Lindsey
49
+ #
50
+ # @since 0.2.0
51
+ #
52
+ class CardNameAmbiguousOrNotFound < StandardError; end
53
+
54
+ ##
55
+ # Raised when insufficient options are passed to one of the
56
+ # Crawlers for it to continue execution.
57
+ #
58
+ # @author Josh Lindsey
59
+ #
60
+ # @since 0.2.0
61
+ #
62
+ class InsufficientOptions < StandardError; end
63
+
64
+
65
+ ##
66
+ # Raised when an unknown word is encountered in
67
+ # {Scapeshift::Card.cost_symbol_from_str}.
68
+ #
69
+ # @author Josh Lindsey
70
+ #
71
+ # @since 0.2.0
72
+ #
73
+ class UnknownCostSymbol < StandardError; end
74
+
75
+ ##
76
+ # Raised when a subclass of {Crawlers::Base} doesn't
77
+ # override a required method (such as {Crawlers::Base#crawl}).
78
+ #
79
+ # @author Josh Lindsey
80
+ #
81
+ # @since 0.3.0
82
+ #
83
+ class InvalidSubclass < StandardError; end
84
+ end
85
+ end
@@ -0,0 +1,78 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{scapeshift}
8
+ s.version = "1.0.1rg0"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Josh Lindsey"]
12
+ s.date = %q{2010-04-15}
13
+ s.description = %q{Magic: The Gathering Oracle web scraper.}
14
+ s.email = %q{joshua.s.lindsey@gmail.com}
15
+ s.extra_rdoc_files = [
16
+ "LICENSE",
17
+ "README.md"
18
+ ]
19
+ s.files = [
20
+ ".document",
21
+ ".gitignore",
22
+ ".yardopts",
23
+ "Gemfile",
24
+ "Gemfile.lock",
25
+ "LICENSE",
26
+ "README.md",
27
+ "Rakefile",
28
+ "VERSION",
29
+ "lib/scapeshift.rb",
30
+ "lib/scapeshift/card.rb",
31
+ "lib/scapeshift/crawler.rb",
32
+ "lib/scapeshift/crawlers.rb",
33
+ "lib/scapeshift/crawlers/base.rb",
34
+ "lib/scapeshift/crawlers/cards.rb",
35
+ "lib/scapeshift/crawlers/meta.rb",
36
+ "lib/scapeshift/crawlers/single.rb",
37
+ "lib/scapeshift/errors.rb",
38
+ "scapeshift.gemspec",
39
+ "test/helper.rb",
40
+ "test/test_base_crawler.rb",
41
+ "test/test_card.rb",
42
+ "test/test_card_crawler.rb",
43
+ "test/test_crawler_main.rb",
44
+ "test/test_meta_crawler.rb",
45
+ "test/test_single_crawler.rb"
46
+ ]
47
+ s.homepage = %q{http://github.com/Karunamon/scapeshift}
48
+ s.rdoc_options = ["--charset=UTF-8"]
49
+ s.require_paths = ["lib"]
50
+ s.rubygems_version = %q{1.3.6}
51
+ s.summary = %q{Magic: The Gathering Oracle web scraper}
52
+ s.test_files = [
53
+ "test/test_card.rb",
54
+ "test/helper.rb",
55
+ "test/test_crawler_main.rb",
56
+ "test/test_single_crawler.rb",
57
+ "test/test_meta_crawler.rb",
58
+ "test/test_card_crawler.rb",
59
+ "test/test_base_crawler.rb"
60
+ ]
61
+
62
+ if s.respond_to? :specification_version then
63
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
64
+ s.specification_version = 3
65
+
66
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
67
+ s.add_development_dependency(%q<bundler>, [">= 0"])
68
+ s.add_runtime_dependency(%q<nokogiri>, [">= 0"])
69
+ else
70
+ s.add_dependency(%q<bundler>, [">= 0"])
71
+ s.add_dependency(%q<nokogiri>, [">= 0"])
72
+ end
73
+ else
74
+ s.add_dependency(%q<bundler>, [">= 0"])
75
+ s.add_dependency(%q<nokogiri>, [">= 0"])
76
+ end
77
+ end
78
+
data/test/helper.rb ADDED
@@ -0,0 +1,16 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+ require 'shoulda'
4
+ require 'fakeweb'
5
+ require 'fakeweb_helper'
6
+
7
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
8
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
9
+ require 'scapeshift'
10
+ require 'set'
11
+
12
+ FakeWeb.allow_net_connect = false
13
+ FakeWebHelper.fake_all_urls
14
+
15
+ class Test::Unit::TestCase
16
+ end
@@ -0,0 +1,48 @@
1
+ require 'helper'
2
+
3
+ class TestBaseCrawler < Test::Unit::TestCase
4
+ context "The Crawler Base class" do
5
+ should "raise the correct exception when crawl is called" do
6
+ assert_raise Scapeshift::Errors::InvalidSubclass do
7
+ crawler = Scapeshift::Crawlers::Base.new :fake => :hash
8
+ crawler.crawl
9
+ end
10
+ end
11
+
12
+ should "raise the correct exception when instantiated without options" do
13
+ assert_raise Scapeshift::Errors::InsufficientOptions do
14
+ Scapeshift::Crawlers::Base.new
15
+ end
16
+ end
17
+
18
+ should "repond to has_callback_hook" do
19
+ assert_respond_to Scapeshift::Crawlers::Base, :has_callback_hook
20
+ end
21
+
22
+ context "with callbacks" do
23
+ setup do
24
+ class CallbackTester < Scapeshift::Crawlers::Base
25
+ has_callback_hook :before_foo
26
+
27
+ def foo
28
+ str = "Failure"
29
+ self.hook :before_foo, str
30
+ str
31
+ end
32
+ end
33
+
34
+ @tester = CallbackTester.new :fake => :hash
35
+ end
36
+
37
+ should "generate the instance method" do
38
+ assert_respond_to @tester, :before_foo
39
+ end
40
+
41
+ should "call the blocks for the hook" do
42
+ @tester.before_foo { |str| str.replace "Success" }
43
+ assert_equal "Success", @tester.foo
44
+ end
45
+ end
46
+ end
47
+ end
48
+
data/test/test_card.rb ADDED
@@ -0,0 +1,80 @@
1
+ require 'helper'
2
+
3
+ class CardTest < Test::Unit::TestCase
4
+ context "The Card class" do
5
+ should "properly convert cost words to symbols" do
6
+ assert_equal "B", Scapeshift::Card.cost_symbol_from_str("Black")
7
+ assert_equal "W", Scapeshift::Card.cost_symbol_from_str("White")
8
+ assert_equal "U", Scapeshift::Card.cost_symbol_from_str("Blue")
9
+ assert_equal "R", Scapeshift::Card.cost_symbol_from_str("Red")
10
+ assert_equal "G", Scapeshift::Card.cost_symbol_from_str("Green")
11
+ assert_equal "X", Scapeshift::Card.cost_symbol_from_str("Variable Colorless")
12
+ assert_equal "3", Scapeshift::Card.cost_symbol_from_str("3")
13
+ assert_equal "[chaos]", Scapeshift::Card.cost_symbol_from_str("[chaos]")
14
+ end
15
+
16
+ should "raise the proper error when passed an invalid cost word" do
17
+ assert_raise Scapeshift::Errors::UnknownCostSymbol do
18
+ Scapeshift::Card.cost_symbol_from_str "Yellow"
19
+ end
20
+ end
21
+ end
22
+
23
+ context "A Card object" do
24
+ context "with complex types" do
25
+ setup do
26
+ @types_str = "Legendary Artifact Creature - Human Wizard"
27
+ @card = Scapeshift::Card.new
28
+ @card.types = @types_str
29
+ end
30
+
31
+ should "parse the types string correctly" do
32
+ assert_equal %w{Legendary}, @card.supertypes
33
+ assert_equal %w{Artifact Creature}, @card.base_types
34
+ assert_equal %w{Human Wizard}, @card.subtypes
35
+ end
36
+
37
+ should "display the string representation of its types" do
38
+ assert_equal @types_str, @card.types
39
+ end
40
+ end
41
+
42
+ context "with several sets and rarities" do
43
+ setup do
44
+ printings = [["Magic 2010", "Rare"], ["Tenth Edition", "Uncommon"]]
45
+ @card = Scapeshift::Card.new :sets => printings
46
+ end
47
+
48
+ should "display its most recent set" do
49
+ assert_equal "Magic 2010", @card.set
50
+ end
51
+
52
+ should "display its most recent rarity" do
53
+ assert_equal "Rare", @card.rarity
54
+ end
55
+ end
56
+
57
+ context "that was instantiated with a Hash" do
58
+ setup do
59
+ @params = { :name => "Mind Spring", :cost => "XBB",
60
+ :types => "Sorcery", :text => "Draw X cards.",
61
+ :sets => [["Magic 2010", "Rare"], ["Morningtide", "Rare"]],
62
+ :image_id => 191323 }
63
+
64
+ @card = Scapeshift::Card.new @params
65
+ end
66
+
67
+ should "have its attributes set properly" do
68
+ assert_instance_of Scapeshift::Card, @card
69
+
70
+ assert_equal @params[:name], @card.name
71
+ assert_equal @params[:cost], @card.cost
72
+ assert_equal @params[:types], @card.types
73
+ assert_equal @params[:text], @card.text
74
+ assert_equal @params[:sets], @card.sets
75
+ assert_equal 'http://gatherer.wizards.com/Handlers/Image.ashx?multiverseid=191323&type=card', @card.image_uri
76
+ end
77
+ end
78
+ end
79
+ end
80
+
@@ -0,0 +1,92 @@
1
+ require 'helper'
2
+
3
+ class TestCardCrawler < Test::Unit::TestCase
4
+ context "The Card crawler" do
5
+ should "respond to crawl" do
6
+ crawler = Scapeshift::Crawlers::Cards.new :set => "fake"
7
+ assert_respond_to crawler, :crawl
8
+ end
9
+
10
+ context "when passed insufficient options" do
11
+ should "raise the appropriate exception" do
12
+ assert_raise Scapeshift::Errors::InsufficientOptions do
13
+ Scapeshift::Crawlers::Cards.new
14
+ end
15
+ end
16
+ end
17
+
18
+ context "when passed valid options" do
19
+ setup do
20
+ # Pull from a specific set instead of Type 2 so we don't have to
21
+ # keep updating this test when new blocks cycle in.
22
+ @cards = Scapeshift::Crawler.crawl :cards, :set => "Darksteel"
23
+ end
24
+
25
+ should "return a SortedSet of Card objects" do
26
+ assert_instance_of SortedSet, @cards
27
+ assert_instance_of Scapeshift::Card, @cards.to_a.first
28
+ end
29
+
30
+ should "pull from the correct set" do
31
+ check = Set.new %w(Coretapper Dismantle Eater\ of\ Days Soulscour Trinisphere Thunderstaff)
32
+ names = Set.new
33
+ @cards.each { |card| names << card.name }
34
+
35
+ assert check.proper_subset?(names)
36
+ end
37
+
38
+ should "have created the Card objects with the correct data" do
39
+ # Using the last entry here because the first one is
40
+ # Æther Snap and fuck typing that out every time.
41
+ #
42
+ # Also, Set and SortedSet don't implement #first or #last.
43
+ # Which is why we're doing this little hack.
44
+ card = @cards.entries[@cards.size - 3]
45
+
46
+ check_card = Scapeshift::Card.new :name => "Wurm's Tooth",
47
+ :types => "Artifact", :sets => [["Magic 2012", "Uncommon"], ["Magic 2011", "Uncommon"],
48
+ ["Magic 2010", "Uncommon"], ["Tenth Edition", "Uncommon"], ["Ninth Edition", "Uncommon"],
49
+ ["Darksteel", "Uncommon"]],
50
+ :cost => "2", :text => "Whenever a player casts a green spell, you may gain 1 life.",
51
+ :image_uri => "http://gatherer.wizards.com/Handlers/Image.ashx?multiverseid=72684&type=card",
52
+ :multiverse_id => "72684"
53
+
54
+ assert_equal check_card, card
55
+ end
56
+ end
57
+
58
+ context "when looking in sets with planeswalkers" do
59
+ setup do
60
+ # Pull from a specific set instead of Type 2 so we don't have to
61
+ # keep updating this test when new blocks cycle in.
62
+ @cards = Scapeshift::Crawler.crawl :cards, :set => "Shards of Alara"
63
+ end
64
+
65
+ should "return a SortedSet of Card objects" do
66
+ assert_instance_of SortedSet, @cards
67
+ assert_instance_of Scapeshift::Card, @cards.to_a.first
68
+ end
69
+
70
+ should "pull from the correct set" do
71
+ check = Set.new %w(Ajani\ Vengeant Archdemon\ of\ Unx Mindlock\ Orb Prince\ of\ Thralls)
72
+ names = Set.new
73
+ @cards.each { |card| names << card.name }
74
+
75
+ assert check.proper_subset?(names)
76
+ end
77
+
78
+ should "have created the Card objects with the correct data" do
79
+ card = @cards.entries[0]
80
+
81
+ check_card = Scapeshift::Card.new :name => "Ad Nauseam",
82
+ :types => "Instant", :sets => [["Shards of Alara", "Rare"]],
83
+ :cost => "3BB", :text => "Reveal the top card of your library and put that card into your hand. You lose life equal to its converted mana cost. You may repeat this process any number of times.",
84
+ :image_uri => "http://gatherer.wizards.com/Handlers/Image.ashx?multiverseid=174915&type=card",
85
+ :multiverse_id => "174915"
86
+
87
+ assert_equal check_card, card
88
+ end
89
+ end
90
+ end
91
+ end
92
+
@@ -0,0 +1,16 @@
1
+ require 'helper'
2
+
3
+ class TestCrawlerMain < Test::Unit::TestCase
4
+ context "The Crawler router class" do
5
+ should "respond to crawl" do
6
+ assert_respond_to Scapeshift::Crawler, :crawl
7
+ end
8
+
9
+ should "raise an exception for an invalid crawler type" do
10
+ assert_raise Scapeshift::Errors::InvalidCrawlerType do
11
+ Scapeshift::Crawler.crawl :invalid
12
+ end
13
+ end
14
+ end
15
+ end
16
+
@@ -0,0 +1,78 @@
1
+ require 'helper'
2
+
3
+ class TestMetaCrawler < Test::Unit::TestCase
4
+ context "The Meta crawler" do
5
+ should "respond to crawl" do
6
+ assert_respond_to Scapeshift::Crawlers::Meta.new(:type => "fake"), :crawl
7
+ end
8
+
9
+ context "when passed no options" do
10
+ should "raise the appropriate exception" do
11
+ assert_raise Scapeshift::Errors::InsufficientOptions do
12
+ Scapeshift::Crawlers::Meta.new
13
+ end
14
+ end
15
+ end
16
+
17
+ context "when passed a bad meta type" do
18
+ should "raise the appropiate exception" do
19
+ assert_raise Scapeshift::Errors::UnknownMetaType do
20
+ crawler = Scapeshift::Crawlers::Meta.new :type => :not_a_real_type
21
+ crawler.crawl
22
+ end
23
+ end
24
+ end
25
+
26
+ context "when crawling expansion Sets" do
27
+ setup do
28
+ @sets = Scapeshift::Crawler.crawl :meta, :type => :sets
29
+ end
30
+
31
+ should "return a SortedSet" do
32
+ assert_instance_of SortedSet, @sets
33
+ end
34
+
35
+ should "return valid expansion sets" do
36
+ check = Set.new %w{Darksteel Coldsnap Zendikar Shadowmoor Lorwyn Nemesis Onslaught}
37
+ assert check.proper_subset?(@sets)
38
+ end
39
+
40
+ should "not contain empty sets" do
41
+ @sets.each do |set|
42
+ assert !set.empty?
43
+ end
44
+ end
45
+ end
46
+
47
+ context "when crawling Formats" do
48
+ setup do
49
+ @formats = Scapeshift::Crawler.crawl :meta, :type => :formats
50
+ end
51
+
52
+ should "return a SortedSet" do
53
+ assert_instance_of SortedSet, @formats
54
+ end
55
+
56
+ should "return valid formats" do
57
+ check = Set.new %w{Standard Classic Legacy Extended Freeform Vintage}
58
+ assert check.proper_subset?(@formats)
59
+ end
60
+ end
61
+
62
+ context "when crawling Card Types" do
63
+ setup do
64
+ @types = Scapeshift::Crawler.crawl :meta, :type => :types
65
+ end
66
+
67
+ should "return a SortedSet" do
68
+ assert_instance_of SortedSet, @types
69
+ end
70
+
71
+ should "return valid card types" do
72
+ check = Set.new %w{Artifact World Tribal Plane Land}
73
+ assert check.proper_subset?(@types)
74
+ end
75
+ end
76
+ end
77
+ end
78
+