metal-archives 0.1.2 → 0.1.3
Sign up to get free protection for your applications and to get access to all the features.
- data/VERSION +1 -1
- data/lib/metal-archives.rb +17 -6
- data/metal-archives.gemspec +1 -1
- data/spec/metal-archives_spec.rb +18 -13
- metadata +3 -3
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.1.
|
1
|
+
0.1.3
|
data/lib/metal-archives.rb
CHANGED
@@ -5,19 +5,20 @@ module MetalArchives
|
|
5
5
|
|
6
6
|
class Agent
|
7
7
|
# An agent accesses the website and holds the HTML source.
|
8
|
-
def initialize
|
8
|
+
def initialize(year=Time.now.year)
|
9
9
|
begin
|
10
10
|
@agent = Mechanize.new
|
11
11
|
rescue Exception => e
|
12
12
|
puts "\nError accessing metal-archives.com on initialization: #{e}"
|
13
13
|
return nil
|
14
14
|
end
|
15
|
+
@year = year
|
15
16
|
end
|
16
17
|
|
17
18
|
# Goes straight to the search results page for the given year.
|
18
|
-
def search_by_year
|
19
|
+
def search_by_year
|
19
20
|
begin
|
20
|
-
@agent.get("#{SITE_URL}/advanced.php?release_year=#{year}&p=1")
|
21
|
+
@agent.get("#{SITE_URL}/advanced.php?release_year=#{@year}&p=1")
|
21
22
|
rescue Exception => e
|
22
23
|
puts "\nError accessing metal-archives.com's search results page: #{e}"
|
23
24
|
return nil
|
@@ -25,11 +26,11 @@ module MetalArchives
|
|
25
26
|
end
|
26
27
|
|
27
28
|
# Finds all the links to the search results pages as they are paginated.
|
28
|
-
def paginated_result_links
|
29
|
+
def paginated_result_links
|
29
30
|
# need the first page because it's not a link
|
30
|
-
links = ["/advanced.php?release_year=#{year}&p=1"]
|
31
|
+
links = ["/advanced.php?release_year=#{@year}&p=1"]
|
31
32
|
begin
|
32
|
-
search_by_year
|
33
|
+
search_by_year.search('body table:nth-child(2n) tr:first-child a').each do |link|
|
33
34
|
links << link['href']
|
34
35
|
end
|
35
36
|
rescue Exception => e
|
@@ -86,6 +87,16 @@ module MetalArchives
|
|
86
87
|
}
|
87
88
|
end
|
88
89
|
|
90
|
+
def total_albums
|
91
|
+
page = search_by_year
|
92
|
+
album_total = page.search('body table:nth-child(2n) tr:first-child b').first.content.match(/\sof\s(\d+)/)
|
93
|
+
if album_total.nil?
|
94
|
+
0
|
95
|
+
else
|
96
|
+
album_total[1].to_i
|
97
|
+
end
|
98
|
+
end
|
99
|
+
|
89
100
|
private
|
90
101
|
|
91
102
|
# The band and and album fields are together, so we need to split them apart.
|
data/metal-archives.gemspec
CHANGED
data/spec/metal-archives_spec.rb
CHANGED
@@ -2,8 +2,8 @@
|
|
2
2
|
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
3
3
|
|
4
4
|
describe "MetalArchives" do
|
5
|
-
context "
|
6
|
-
context "
|
5
|
+
context "with an agent" do
|
6
|
+
context "that searches by year" do
|
7
7
|
before do
|
8
8
|
search_results_html = File.open(File.dirname(__FILE__) + '/html/search_results.html')
|
9
9
|
@search_results = Nokogiri::HTML(search_results_html)
|
@@ -11,29 +11,34 @@ describe "MetalArchives" do
|
|
11
11
|
Mechanize.stub!(:new).and_return(@mechanize)
|
12
12
|
end
|
13
13
|
|
14
|
-
it "should
|
14
|
+
it "should find the total number of albums" do
|
15
15
|
@mechanize.stub!(:get).and_return(@search_results)
|
16
|
-
agent = MetalArchives::Agent.new
|
16
|
+
agent = MetalArchives::Agent.new(2011)
|
17
|
+
|
18
|
+
agent.total_albums.should == 757
|
19
|
+
end
|
20
|
+
|
21
|
+
it "should get the results page" do
|
22
|
+
@mechanize.stub!(:get).and_return(@search_results)
|
23
|
+
agent = MetalArchives::Agent.new(2011)
|
17
24
|
|
18
25
|
agent.search_by_year.should == @search_results
|
19
26
|
end
|
20
27
|
|
21
28
|
context "with a results page" do
|
22
29
|
it "should find the paginated result links" do
|
23
|
-
agent = MetalArchives::Agent.new
|
30
|
+
agent = MetalArchives::Agent.new(2011)
|
24
31
|
agent.stub!(:search_by_year).and_return(@search_results)
|
25
32
|
|
26
|
-
|
27
|
-
|
28
|
-
(1..16).each do |i|
|
29
|
-
links << "/advanced.php?release_year=2011&p=#{i}"
|
33
|
+
links = (1..16).collect do |i|
|
34
|
+
"/advanced.php?release_year=2011&p=#{i}"
|
30
35
|
end
|
31
36
|
agent.paginated_result_links.should == links
|
32
37
|
end
|
33
38
|
|
34
39
|
it "should find the album links" do
|
35
40
|
@mechanize.stub!(:get).and_return(@search_results)
|
36
|
-
agent = MetalArchives::Agent.new
|
41
|
+
agent = MetalArchives::Agent.new(2011)
|
37
42
|
agent.stub!(:search_by_year).and_return(@search_results)
|
38
43
|
|
39
44
|
links = [
|
@@ -61,7 +66,7 @@ describe "MetalArchives" do
|
|
61
66
|
@mechanize = stub('Mechanize')
|
62
67
|
Mechanize.stub!(:new).and_return(@mechanize)
|
63
68
|
@mechanize.stub!(:get).and_return(@search_results)
|
64
|
-
agent = MetalArchives::Agent.new
|
69
|
+
agent = MetalArchives::Agent.new(2011)
|
65
70
|
album_link_from_url = "release.php?id=000001"
|
66
71
|
|
67
72
|
agent.album_from_url(album_link_from_url).should == {
|
@@ -82,7 +87,7 @@ describe "MetalArchives" do
|
|
82
87
|
@mechanize = stub('Mechanize')
|
83
88
|
Mechanize.stub!(:new).and_return(@mechanize)
|
84
89
|
@mechanize.stub!(:get).and_return(@search_results)
|
85
|
-
agent = MetalArchives::Agent.new
|
90
|
+
agent = MetalArchives::Agent.new(2011)
|
86
91
|
album_link_from_url = "release.php?id=000001"
|
87
92
|
|
88
93
|
agent.album_from_url(album_link_from_url).should == {
|
@@ -103,7 +108,7 @@ describe "MetalArchives" do
|
|
103
108
|
@mechanize = stub('Mechanize')
|
104
109
|
Mechanize.stub!(:new).and_return(@mechanize)
|
105
110
|
@mechanize.stub!(:get).and_return(@search_results)
|
106
|
-
agent = MetalArchives::Agent.new
|
111
|
+
agent = MetalArchives::Agent.new(2011)
|
107
112
|
album_link_from_url = "release.php?id=000001"
|
108
113
|
|
109
114
|
agent.album_from_url(album_link_from_url).should == {
|
metadata
CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
|
|
5
5
|
segments:
|
6
6
|
- 0
|
7
7
|
- 1
|
8
|
-
-
|
9
|
-
version: 0.1.
|
8
|
+
- 3
|
9
|
+
version: 0.1.3
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- Danny Olson
|
@@ -143,7 +143,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
143
143
|
requirements:
|
144
144
|
- - ">="
|
145
145
|
- !ruby/object:Gem::Version
|
146
|
-
hash:
|
146
|
+
hash: 834701885390149642
|
147
147
|
segments:
|
148
148
|
- 0
|
149
149
|
version: "0"
|