metal_archives 0.2.0

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,6 @@
1
+ module MetalArchives
2
+ ##
3
+ # MetalArchives API version
4
+ #
5
+ VERSION = '0.2.0'
6
+ end
@@ -0,0 +1,28 @@
1
+ require File.expand_path('../lib/metal_archives/version', __FILE__)
2
+
3
+ Gem::Specification.new do |gem|
4
+ gem.authors = ["Florian Dejonckheere"]
5
+ gem.email = ["florian@floriandejonckheere.be"]
6
+ gem.summary = %q{Metal Archives Ruby API}
7
+ gem.homepage = "http://github.com/floriandejonckheere/metal_archives"
8
+
9
+ gem.files = %x{git ls-files}.split($\)
10
+ gem.executables = []
11
+ gem.test_files = gem.files.grep(%r{^spec/})
12
+ gem.name = "metal_archives"
13
+ gem.require_paths = %w[lib]
14
+ gem.version = MetalArchives::VERSION
15
+ gem.license = "MIT"
16
+
17
+ gem.add_development_dependency 'byebug'
18
+ gem.add_development_dependency 'rake'
19
+ gem.add_development_dependency 'rdoc'
20
+ gem.add_development_dependency 'test-unit'
21
+ gem.add_development_dependency 'activesupport'
22
+
23
+ gem.add_dependency 'faraday'
24
+ gem.add_dependency 'faraday-http-cache'
25
+ gem.add_dependency 'faraday_throttler'
26
+ gem.add_dependency 'nokogiri'
27
+ gem.add_dependency 'countries'
28
+ end
@@ -0,0 +1,87 @@
1
+ require 'test_helper'
2
+
3
+ require 'metal_archives/models/base_model'
4
+
5
+ ##
6
+ # Sample model without :id property
7
+ #
8
+ class ModelOne < MetalArchives::BaseModel
9
+ property :property_one
10
+ end
11
+
12
+ ##
13
+ # Sample model without :assemble method
14
+ #
15
+ class ModelTwo < MetalArchives::BaseModel
16
+ property :id
17
+ property :property_one
18
+ end
19
+
20
+ ##
21
+ # Sample model
22
+ #
23
+ class ModelThree < MetalArchives::BaseModel
24
+ property :id
25
+ property :property_one
26
+
27
+ def assemble
28
+ instance_variable_set "@property_one", 'Property One'
29
+ end
30
+ end
31
+
32
+ ##
33
+ # BaseModel tests
34
+ #
35
+ class BaseModelTest < Test::Unit::TestCase
36
+ def test_need_id
37
+ assert_raise MetalArchives::Errors::NotImplementedError do
38
+ ModelOne.new(:property_one => 'foo')
39
+ end
40
+ end
41
+
42
+ def test_need_assemble
43
+ assert_raise MetalArchives::Errors::NotImplementedError do
44
+ ModelTwo.new(:id => 'foo').property_one
45
+ end
46
+ end
47
+
48
+ def test_constructor
49
+ model = ModelThree.new(:id => 'foo', :property_one => 'bar')
50
+
51
+ assert model.instance_variable_defined? '@id'
52
+ assert model.instance_variable_defined? '@property_one'
53
+ assert !model.instance_variable_defined?('@property_two')
54
+ end
55
+
56
+ def test_methods
57
+ model = ModelThree.new(:id => 'foo')
58
+
59
+ assert_respond_to model, :id
60
+ assert_respond_to model, :id?
61
+ assert_respond_to model, :id=
62
+
63
+ assert_respond_to model, :property_one
64
+ assert_respond_to model, :property_one?
65
+ assert_respond_to model, :property_one=
66
+ end
67
+
68
+ def test_assemble
69
+ model = ModelThree.new :id => 'foo'
70
+
71
+ assert_equal 'foo', model.id
72
+ assert_equal 'Property One', model.property_one
73
+ end
74
+
75
+ def test_lazy_load
76
+ model = ModelThree.new :id => 'foo'
77
+
78
+ assert model.instance_variable_defined? '@id'
79
+ assert !model.instance_variable_defined?('@property_one')
80
+
81
+ model.property_one
82
+
83
+ assert model.instance_variable_defined? '@id'
84
+ assert model.instance_variable_defined? '@property_one'
85
+ assert_equal 'Property One', model.property_one
86
+ end
87
+ end
@@ -0,0 +1,57 @@
1
+ $:.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
2
+ require 'test/unit'
3
+
4
+ require 'metal_archives'
5
+ require 'metal_archives/error'
6
+
7
+ ##
8
+ # Configuration tests
9
+ #
10
+ class ConfigurationTest < Test::Unit::TestCase
11
+ def setup
12
+ MetalArchives.config = nil
13
+ end
14
+
15
+ def test_no_configuration
16
+ assert_raise MetalArchives::Errors::InvalidConfigurationError do
17
+ MetalArchives::Band.search 'some_name'
18
+ end
19
+ end
20
+
21
+ def test_missing_app_name
22
+ assert_raise MetalArchives::Errors::InvalidConfigurationError do
23
+ MetalArchives.configure do |c|
24
+ c.app_version = MetalArchives::VERSION
25
+ c.app_contact = `git config user.email`.chomp || 'user@example.com'
26
+ end
27
+ end
28
+ end
29
+
30
+ def test_missing_app_version
31
+ assert_raise MetalArchives::Errors::InvalidConfigurationError do
32
+ MetalArchives.configure do |c|
33
+ c.app_name = 'MetalArchivesGemTestSuite'
34
+ c.app_contact = `git config user.email`.chomp || 'user@example.com'
35
+ end
36
+ end
37
+ end
38
+
39
+ def test_missing_app_contact
40
+ assert_raise MetalArchives::Errors::InvalidConfigurationError do
41
+ MetalArchives.configure do |c|
42
+ c.app_name = 'MetalArchivesGemTestSuite'
43
+ c.app_version = MetalArchives::VERSION
44
+ end
45
+ end
46
+ end
47
+
48
+ def test_full_configuration
49
+ assert_nothing_raised do
50
+ MetalArchives.configure do |c|
51
+ c.app_name = 'MetalArchivesGemTestSuite'
52
+ c.app_version = MetalArchives::VERSION
53
+ c.app_contact = `git config user.email`.chomp || 'user@example.com'
54
+ end
55
+ end
56
+ end
57
+ end
@@ -0,0 +1,37 @@
1
+ require 'test_helper'
2
+
3
+ require 'date'
4
+ require 'countries'
5
+
6
+ require 'metal_archives/parsers/parser_helper.rb'
7
+
8
+ class ParserHelperTest < Test::Unit::TestCase
9
+ def test_parse_country
10
+ assert_equal ISO3166::Country['US'], MetalArchives::Parsers::ParserHelper.parse_country('United States')
11
+ assert_equal ISO3166::Country['DE'], MetalArchives::Parsers::ParserHelper.parse_country('Germany')
12
+ assert_equal ISO3166::Country['BE'], MetalArchives::Parsers::ParserHelper.parse_country('Belgium')
13
+ end
14
+
15
+ def test_parse_genre
16
+ assert_equal ['Black', 'Death', 'Power'].sort,
17
+ MetalArchives::Parsers::ParserHelper.parse_genre('Death, Power, Black').sort
18
+
19
+ assert_equal ['Black', 'Death', 'Power'].sort,
20
+ MetalArchives::Parsers::ParserHelper.parse_genre('Death, Power, Black').sort
21
+
22
+ assert_equal ['Black', 'Death', 'Heavy', 'Power'].sort,
23
+ MetalArchives::Parsers::ParserHelper.parse_genre('Death (early), Heavy/Power Metal, Black (later)').sort
24
+
25
+ assert_equal ['Death', 'Power'].sort,
26
+ MetalArchives::Parsers::ParserHelper.parse_genre(' Death , Power Metal, Power, Power').sort
27
+
28
+ assert_equal ['Heavy Power', 'Speed Power'].sort,
29
+ MetalArchives::Parsers::ParserHelper.parse_genre('Heavy/Speed Power Metal').sort
30
+
31
+ assert_equal ['Traditional Heavy', 'Traditional Power'].sort,
32
+ MetalArchives::Parsers::ParserHelper.parse_genre('Traditional Heavy/Power Metal').sort
33
+
34
+ assert_equal ['Traditional Heavy', 'Traditional Power', 'Classical Heavy', 'Classical Power'].sort,
35
+ MetalArchives::Parsers::ParserHelper.parse_genre('Traditional/Classical Heavy/Power Metal').sort
36
+ end
37
+ end
@@ -0,0 +1,43 @@
1
+ require_relative '../test_helper'
2
+
3
+ require 'metal_archives/parsers/band'
4
+
5
+ ##
6
+ # Property testing
7
+ #
8
+ # If a test fails, please check the online results to make sure it's not
9
+ # the content itself that has changed.
10
+ #
11
+ class ArtistPropertyTest < Test::Unit::TestCase
12
+ def test_basic_attributes
13
+ artist = MetalArchives::Artist.find 60908
14
+
15
+ assert artist.is_a? MetalArchives::Artist
16
+ assert_equal 60908, artist.id
17
+ assert_equal 'Alberto Rionda', artist.name
18
+ assert_empty artist.aliases
19
+ assert_equal ISO3166::Country['ES'], artist.country
20
+ assert_equal 'Oviedo, Asturias', artist.location
21
+ assert_equal Date.new(1972, 9, 2), artist.date_of_birth
22
+ assert_equal :male, artist.gender
23
+ assert_match 'Avalanch', artist.biography
24
+ assert_match 'Sanctuarium Estudios', artist.trivia
25
+
26
+ artist = MetalArchives::Artist.find 260
27
+
28
+ assert artist.is_a? MetalArchives::Artist
29
+ assert_equal 'Ian Fraser Kilmister', artist.name
30
+ assert_equal ['Lemmy Kilmister'], artist.aliases
31
+ assert_equal Date.new(2015, 12, 28), artist.date_of_death
32
+ assert_equal 4, artist.links.length
33
+ assert_equal 1, artist.links.count { |l| l[:type] == :official }
34
+ assert_equal 2, artist.links.count { |l| l[:type] == :unofficial }
35
+ assert_equal 1, artist.links.count { |l| l[:type] == :unlisted_bands }
36
+ assert_equal 'https://www.facebook.com/OfficialLemmy', artist.links.select { |l| l[:type] == :official }.first[:url]
37
+ assert_equal 'Facebook', artist.links.select { |l| l[:type] == :official }.first[:title]
38
+ end
39
+
40
+ def test_map_params
41
+ assert_equal 'name', MetalArchives::Parsers::Artist.map_params(:name => 'name')[:query]
42
+ end
43
+ end
@@ -0,0 +1,94 @@
1
+ require_relative '../test_helper'
2
+
3
+ require 'metal_archives/parsers/band'
4
+
5
+ ##
6
+ # Property testing
7
+ #
8
+ # If a test fails, please check the online results to make sure it's not
9
+ # the content itself that has changed.
10
+ #
11
+ class BandPropertyTest < Test::Unit::TestCase
12
+ def test_basic_attributes
13
+ band = MetalArchives::Band.find 122302
14
+
15
+ assert_equal 'Pathfinder', band.name
16
+ assert_empty band.aliases
17
+ assert_equal ISO3166::Country['PL'], band.country
18
+ assert_equal 'Poznań', band.location
19
+ assert_equal Date.new(2006), band.date_formed
20
+ assert_equal [MetalArchives::Range.new(Date.new(2006), nil)], band.date_active
21
+ assert_equal :active, band.status
22
+ assert_equal ['Symphonic Power'], band.genres
23
+ assert_equal ['Fantasy', 'Battles', 'Glory', 'The Four Elements', 'Metal'].sort, band.lyrical_themes.sort
24
+ assert_match 'Pathfinder was founded by', band.comment
25
+ assert_equal 'http://www.metal-archives.com/images/1/2/2/3/122302_logo.jpg', band.logo
26
+ assert_equal 'http://www.metal-archives.com/images/1/2/2/3/122302_photo.jpg?5400', band.photo
27
+ assert !band.independent
28
+ assert_equal 19, band.similar.length
29
+ assert_equal 15, band.links.length
30
+ assert_equal 12, band.links.count { |l| l[:type] == :official }
31
+ assert_equal 3, band.links.count { |l| l[:type] == :merchandise }
32
+ assert_equal 'http://www.amazon.com/Fifth-Element-Pathfinder/dp/B007MNNCVW', band.links.select { |l| l[:type] == :merchandise }.first[:url]
33
+ assert_equal 'Amazon', band.links.select { |l| l[:type] == :merchandise }.first[:title]
34
+
35
+
36
+ band = MetalArchives::Band.find 32
37
+
38
+ assert_equal 'Rhapsody of Fire', band.name
39
+ assert_equal ['Thundercross', 'Rhapsody'].sort, band.aliases.sort
40
+ end
41
+
42
+ def test_map_status
43
+ assert_equal '', MetalArchives::Parsers::Band.send(:map_status, nil)
44
+ assert_equal 'Active', MetalArchives::Parsers::Band.send(:map_status, :active)
45
+ assert_equal 'Split-up', MetalArchives::Parsers::Band.send(:map_status, :split_up)
46
+ assert_equal 'On hold', MetalArchives::Parsers::Band.send(:map_status, :on_hold)
47
+ assert_equal 'Unknown', MetalArchives::Parsers::Band.send(:map_status, :unknown)
48
+ assert_equal 'Changed name', MetalArchives::Parsers::Band.send(:map_status, :changed_name)
49
+ assert_equal 'Disputed', MetalArchives::Parsers::Band.send(:map_status, :disputed)
50
+
51
+ assert_raises MetalArchives::Errors::ParserError do
52
+ MetalArchives::Parsers::Band.send(:map_status, :invalid_status)
53
+ end
54
+ end
55
+
56
+ def test_map_params
57
+ assert_equal 'name', MetalArchives::Parsers::Band.map_params(:name => 'name')[:bandName]
58
+
59
+ assert_equal 1, MetalArchives::Parsers::Band.map_params(:exact => true)[:exactBandMatch]
60
+ assert_equal 1, MetalArchives::Parsers::Band.map_params(:exact => 'somevalue')[:exactBandMatch]
61
+ assert_equal 0, MetalArchives::Parsers::Band.map_params(:exact => false)[:exactBandMatch]
62
+ assert_equal 0, MetalArchives::Parsers::Band.map_params(:exact => nil)[:exactBandMatch]
63
+
64
+ assert_equal '', MetalArchives::Parsers::Band.map_params({})[:bandName]
65
+ assert_equal 0, MetalArchives::Parsers::Band.map_params({})[:exactBandMatch]
66
+ assert_equal 'genre', MetalArchives::Parsers::Band.map_params({ :genre => 'genre'})[:genre]
67
+ assert_equal '', MetalArchives::Parsers::Band.map_params({})[:genre]
68
+
69
+ range = Range.new Date.new(2016), Date.new(2017)
70
+ assert_equal 2016, MetalArchives::Parsers::Band.map_params({ :year => range })[:yearCreationFrom]
71
+ assert_equal 2017, MetalArchives::Parsers::Band.map_params({ :year => range })[:yearCreationTo]
72
+ assert_equal '', MetalArchives::Parsers::Band.map_params({})[:yearCreationFrom]
73
+ assert_equal '', MetalArchives::Parsers::Band.map_params({})[:yearCreationTo]
74
+
75
+ assert_equal 'comment', MetalArchives::Parsers::Band.map_params({ :comment => 'comment' })[:bandNotes]
76
+ # :status is tested in test_map_status
77
+ assert_equal 'themes', MetalArchives::Parsers::Band.map_params({ :lyrical_themes => 'themes' })[:themes]
78
+ assert_equal 'location', MetalArchives::Parsers::Band.map_params({ :location => 'location' })[:location]
79
+ assert_equal 'label', MetalArchives::Parsers::Band.map_params({ :label => 'label' })[:bandLabelName]
80
+ assert_equal 1, MetalArchives::Parsers::Band.map_params({ :independent => true })[:indieLabelBand]
81
+ assert_equal 1, MetalArchives::Parsers::Band.map_params({ :independent => 'some value' })[:indieLabelBand]
82
+ assert_equal 1, MetalArchives::Parsers::Band.map_params({ :independent => '' })[:indieLabelBand]
83
+ assert_equal 0, MetalArchives::Parsers::Band.map_params({ :independent => false })[:indieLabelBand]
84
+ assert_equal 0, MetalArchives::Parsers::Band.map_params({ :independent => nil })[:indieLabelBand]
85
+
86
+ country = ISO3166::Country['BE']
87
+ country2 = ISO3166::Country['NL']
88
+ assert_equal 'BE', MetalArchives::Parsers::Band.map_params({ :country => country })[:country]
89
+ assert_equal ['BE', 'NL'], MetalArchives::Parsers::Band.map_params({ :country => [country, country2] })[:country]
90
+
91
+ assert_equal 'BE', MetalArchives::Parsers::Band.map_params({ :country => ['BE'] })[:country]
92
+ assert_equal ['BE', 'NL'], MetalArchives::Parsers::Band.map_params({ :country => ['BE', 'NL'] })[:country]
93
+ end
94
+ end
@@ -0,0 +1,44 @@
1
+ require_relative '../test_helper'
2
+
3
+ ##
4
+ # Query method testing
5
+ #
6
+ # If a test fails, please check the online results to make sure it's not
7
+ # the content itself that has changed.
8
+ #
9
+ class ArtistQueryTest < Test::Unit::TestCase
10
+ def test_find
11
+ artist = MetalArchives::Artist.find 60908
12
+
13
+ assert_not_nil artist
14
+ assert artist.is_a? MetalArchives::Artist
15
+ assert_equal 'Alberto Rionda', artist.name
16
+ assert_equal ISO3166::Country['ES'], artist.country
17
+
18
+ artist = MetalArchives::Artist.find(999999)
19
+
20
+ assert artist.is_a? MetalArchives::Artist
21
+ end
22
+
23
+ def test_find_by
24
+ artist = MetalArchives::Artist.find_by :name => 'Alberto Rionda'
25
+
26
+ assert_not_nil artist
27
+ assert artist.is_a? MetalArchives::Artist
28
+ assert_equal 'Alberto Rionda', artist.name
29
+ assert_equal 60908, artist.id
30
+ assert_equal ISO3166::Country['ES'], artist.country
31
+
32
+
33
+ artist = MetalArchives::Artist.find_by :name => 'SomeNonExistantName'
34
+
35
+ assert_nil artist
36
+ end
37
+
38
+ def test_search
39
+ assert_equal 1, MetalArchives::Artist.search('Alberto Rionda').length
40
+ assert_equal 9, MetalArchives::Artist.search('Name').length
41
+ assert_equal 0, MetalArchives::Artist.search('SomeNonExistantName').length
42
+ assert_equal 293, MetalArchives::Artist.search('Filip').length
43
+ end
44
+ end
@@ -0,0 +1,84 @@
1
+ require_relative '../test_helper'
2
+
3
+ ##
4
+ # Query method testing
5
+ #
6
+ # If a test fails, please check the online results to make sure it's not
7
+ # the content itself that has changed.
8
+ #
9
+ class BandQueryTest < Test::Unit::TestCase
10
+ def test_find
11
+ band = MetalArchives::Band.find 3540361100
12
+
13
+ assert_not_nil band
14
+ assert band.is_a? MetalArchives::Band
15
+ assert_equal 'Alquimia', band.name
16
+ assert_equal ISO3166::Country['ES'], band.country
17
+
18
+ assert_match 'http', band.logo
19
+ assert_match 'http', band.photo
20
+
21
+ band = MetalArchives::Band.find(2)
22
+
23
+ assert band.is_a? MetalArchives::Band
24
+ end
25
+
26
+ def test_find_by
27
+ band = MetalArchives::Band.find_by :name => 'Falconer'
28
+
29
+ assert_not_nil band
30
+ assert band.is_a? MetalArchives::Band
31
+ assert_equal 'Falconer', band.name
32
+ assert_equal 74, band.id
33
+ assert_equal ISO3166::Country['SE'], band.country
34
+
35
+
36
+ band = MetalArchives::Band.find_by :name => 'SomeNonExistantName'
37
+
38
+ assert_nil band
39
+
40
+
41
+ band = MetalArchives::Band.find_by(:name => 'Alquimia', :country => ISO3166::Country['ES'])
42
+
43
+ assert_not_nil band
44
+ assert band.is_a? MetalArchives::Band
45
+ assert_equal 3540361100, band.id
46
+ end
47
+
48
+ def test_search_by
49
+ assert_equal 5, MetalArchives::Band.search_by(:name => 'Alquimia').length
50
+
51
+ assert_equal 3, MetalArchives::Band.search_by(:name => 'Lost Horizon').length
52
+ assert_equal 2, MetalArchives::Band.search_by(:name => 'Lost Horizon', :exact => true).length
53
+
54
+ assert_equal 2, MetalArchives::Band.search_by(:name => 'Alquimia', :genre => 'Melodic Power').length
55
+
56
+
57
+ countries = MetalArchives::Band.search('Alquimia').map { |a| a.country }.sort
58
+
59
+ assert_equal 5, countries.size
60
+ assert countries.include? ISO3166::Country['AR']
61
+ assert countries.include? ISO3166::Country['ES']
62
+ assert countries.include? ISO3166::Country['CL']
63
+
64
+ assert_equal 5, MetalArchives::Band.search_by(
65
+ :name => 'Alquimia', :year => MetalArchives::Range.new(nil, nil)).length
66
+ assert_equal 1, MetalArchives::Band.search_by(
67
+ :name => 'Alquimia', :year => MetalArchives::Range.new(Date.new(2013), nil)).length
68
+ assert_equal 1, MetalArchives::Band.search_by(
69
+ :name => 'Alquimia', :year => MetalArchives::Range.new(Date.new(2008), Date.new(2008))).length
70
+ assert_equal 2, MetalArchives::Band.search_by(
71
+ :name => 'Alquimia', :year => MetalArchives::Range.new(Date.new(2008), Date.new(2013))).length
72
+ assert_equal 5, MetalArchives::Band.search_by(
73
+ :name => 'Alquimia', :year => MetalArchives::Range.new(nil, Date.new(2013))).length
74
+
75
+ assert_equal 1, MetalArchives::Band.search_by(:name => 'Alquimia', :country => ISO3166::Country['ES']).length
76
+ assert_equal 3, MetalArchives::Band.search_by(:name => 'Alquimia', :country => ISO3166::Country['AR']).length
77
+ assert_equal 3, MetalArchives::Band.search_by(:name => 'Alquimia', :country => ISO3166::Country['AR']).length
78
+ assert_equal 1, MetalArchives::Band.search_by(:name => 'Alquimia', :label => 'Mutus Liber').length
79
+
80
+ assert_empty MetalArchives::Band.search_by(:name => 'SomeNonExistantName')
81
+
82
+ assert_equal 274, MetalArchives::Band.search_by(:country => ISO3166::Country['CN']).length
83
+ end
84
+ end