rbrainz 0.1.1 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (85) hide show
  1. data/CHANGES +31 -0
  2. data/LICENSE +1 -1
  3. data/README +3 -2
  4. data/Rakefile +40 -22
  5. data/TODO +6 -23
  6. data/doc/README.rdoc +50 -21
  7. data/examples/getartist.rb +6 -4
  8. data/examples/getuser.rb +30 -0
  9. data/examples/searchartists.rb +35 -0
  10. data/lib/rbrainz.rb +12 -7
  11. data/lib/rbrainz/core_ext.rb +8 -0
  12. data/lib/rbrainz/core_ext/mbid.rb +30 -0
  13. data/lib/rbrainz/core_ext/net_http_digest.rb +52 -0
  14. data/lib/rbrainz/core_ext/range.rb +28 -0
  15. data/lib/rbrainz/core_ext/range/equality.rb +232 -0
  16. data/lib/rbrainz/data/countrynames.rb +7 -5
  17. data/lib/rbrainz/data/languagenames.rb +8 -5
  18. data/lib/rbrainz/data/releasetypenames.rb +34 -0
  19. data/lib/rbrainz/data/scriptnames.rb +8 -5
  20. data/lib/rbrainz/model.rb +27 -35
  21. data/lib/rbrainz/model/alias.rb +31 -7
  22. data/lib/rbrainz/model/artist.rb +30 -41
  23. data/lib/rbrainz/model/collection.rb +102 -0
  24. data/lib/rbrainz/model/default_factory.rb +78 -0
  25. data/lib/rbrainz/model/disc.rb +45 -8
  26. data/lib/rbrainz/model/entity.rb +122 -53
  27. data/lib/rbrainz/model/incomplete_date.rb +31 -47
  28. data/lib/rbrainz/model/individual.rb +103 -0
  29. data/lib/rbrainz/model/label.rb +42 -33
  30. data/lib/rbrainz/model/mbid.rb +111 -40
  31. data/lib/rbrainz/model/relation.rb +78 -14
  32. data/lib/rbrainz/model/release.rb +119 -31
  33. data/lib/rbrainz/model/release_event.rb +38 -9
  34. data/lib/rbrainz/model/scored_collection.rb +99 -0
  35. data/lib/rbrainz/model/tag.rb +39 -0
  36. data/lib/rbrainz/model/track.rb +37 -13
  37. data/lib/rbrainz/model/user.rb +48 -0
  38. data/lib/rbrainz/utils.rb +9 -0
  39. data/lib/rbrainz/utils/data.rb +78 -0
  40. data/lib/rbrainz/utils/helper.rb +22 -0
  41. data/lib/rbrainz/version.rb +15 -0
  42. data/lib/rbrainz/webservice.rb +32 -6
  43. data/lib/rbrainz/webservice/filter.rb +124 -47
  44. data/lib/rbrainz/webservice/includes.rb +49 -10
  45. data/lib/rbrainz/webservice/mbxml.rb +228 -173
  46. data/lib/rbrainz/webservice/query.rb +312 -25
  47. data/lib/rbrainz/webservice/webservice.rb +164 -27
  48. data/test/lib/mock_webservice.rb +53 -0
  49. data/test/lib/test_entity.rb +27 -8
  50. data/test/lib/test_factory.rb +47 -0
  51. data/test/lib/testing_helper.rb +7 -5
  52. data/test/test-data/invalid/artist/tags_1.xml +6 -0
  53. data/test/test-data/valid/artist/Tchaikovsky-2.xml +12 -0
  54. data/test/test-data/valid/label/Atlantic_Records_2.xml +3 -0
  55. data/test/test-data/valid/label/Atlantic_Records_3.xml +11 -0
  56. data/test/test-data/valid/release/Highway_61_Revisited_2.xml +12 -0
  57. data/test/test-data/valid/track/Silent_All_These_Years_6.xml +8 -0
  58. data/test/test_alias.rb +13 -7
  59. data/test/test_artist.rb +26 -4
  60. data/test/test_artist_filter.rb +11 -6
  61. data/test/test_artist_includes.rb +11 -6
  62. data/test/test_collection.rb +66 -0
  63. data/test/test_default_factory.rb +75 -0
  64. data/test/test_disc.rb +9 -4
  65. data/test/test_incomplete_date.rb +21 -14
  66. data/test/test_label.rb +56 -18
  67. data/test/test_label_filter.rb +10 -5
  68. data/test/test_label_includes.rb +11 -6
  69. data/test/test_mbid.rb +34 -19
  70. data/test/test_mbxml.rb +242 -72
  71. data/test/test_query.rb +92 -7
  72. data/test/test_range_equality.rb +144 -0
  73. data/test/test_relation.rb +18 -7
  74. data/test/test_release.rb +15 -4
  75. data/test/test_release_event.rb +16 -4
  76. data/test/test_release_filter.rb +11 -5
  77. data/test/test_release_includes.rb +11 -6
  78. data/test/test_scored_collection.rb +86 -0
  79. data/test/test_tag.rb +39 -0
  80. data/test/test_track.rb +15 -4
  81. data/test/test_track_filter.rb +11 -5
  82. data/test/test_track_includes.rb +11 -6
  83. data/test/test_utils.rb +41 -0
  84. data/test/test_webservice.rb +16 -17
  85. metadata +93 -57
@@ -1,23 +1,52 @@
1
- # $Id: release_event.rb 9 2007-05-21 21:16:34Z phw $
2
- # Copyright (c) 2007, Philipp Wolfer
3
- # All rights reserved.
4
- # See LICENSE for permissions.
1
+ # $Id: release_event.rb 141 2007-07-17 14:21:12Z phw $
2
+ #
3
+ # Author:: Philipp Wolfer (mailto:phw@rubyforge.org)
4
+ # Copyright:: Copyright (c) 2007, Nigel Graham, Philipp Wolfer
5
+ # License:: RBrainz is free software distributed under a BSD style license.
6
+ # See LICENSE[file:../LICENSE.html] for permissions.
5
7
 
6
8
  require 'rbrainz/model/label'
7
9
 
8
10
  module MusicBrainz
9
11
  module Model
10
12
 
11
- # A release event in the MusicBrainz DB.
13
+ #
14
+ # A release event in the MusicBrainz DB indicating where and when a release
15
+ # took place.
12
16
  #
13
- # See http://musicbrainz.org/doc/ReleaseEvent.
17
+ # All country codes used must be valid ISO-3166 country codes (i.e. 'DE',
18
+ # 'UK' or 'FR'). The dates are instances of IncompleteDate or strings which
19
+ # must have the format 'YYYY', 'YYYY-MM' or 'YYYY-MM-DD'.
20
+ #
21
+ # See:: http://musicbrainz.org/doc/ReleaseEvent.
22
+ #
14
23
  class ReleaseEvent
15
24
 
16
- attr_accessor :country, :catalog_number,
17
- :barcode, :label
18
-
25
+ # The country in which an album was released.
26
+ # A string containing a ISO 3166 country code like
27
+ # 'GB', 'US' or 'DE'.
28
+ #
29
+ # See:: Utils#get_country_name
30
+ # See:: http://musicbrainz.org/doc/ReleaseCountry
31
+ attr_accessor :country
32
+
33
+ # The catalog number given to the release by the label.
34
+ attr_accessor :catalog_number
35
+
36
+ # The barcode as it is printed on the release.
37
+ attr_accessor :barcode
38
+
39
+ # The label issuing the release.
40
+ attr_accessor :label
41
+
42
+ # The release date. An instance of IncompleteDate.
19
43
  attr_reader :date
20
44
 
45
+ def initialize(country=nil, date=nil)
46
+ self.country = country
47
+ self.date = date if date
48
+ end
49
+
21
50
  # Set the date the release took place.
22
51
  #
23
52
  # Should be an IncompleteDate object or
@@ -0,0 +1,99 @@
1
+ # $Id: scored_collection.rb 136 2007-07-16 15:45:32Z phw $
2
+ #
3
+ # Author:: Philipp Wolfer (mailto:phw@rubyforge.org)
4
+ # Copyright:: Copyright (c) 2007, Nigel Graham, Philipp Wolfer
5
+ # License:: RBrainz is free software distributed under a BSD style license.
6
+ # See LICENSE[file:../LICENSE.html] for permissions.
7
+
8
+ require 'rbrainz/model/collection'
9
+
10
+ module MusicBrainz
11
+ module Model
12
+
13
+ # A ScoredCollection is a list of entities with scores.
14
+ #
15
+ # A ScoredCollection is returned as the result of an entity search (e.g. search
16
+ # for an Artist). It contains an ordered list of Entry objects which wrap
17
+ # the entities and a corresponding search score, which indicates how good
18
+ # the entity matches the search criteria used for the search. The entities
19
+ # in a scored collection are sorted by score (in descending order).
20
+ #
21
+ # A collection object may only store an extract of the complete data
22
+ # available on the server. This is especially the case if the limit or
23
+ # offset filter was used in the query. The collection object makes the
24
+ # total number of elements on the server and the current offset available
25
+ # with the +count+ respective the +offset+ parameter.
26
+ #
27
+ # See http://lucene.apache.org/java/docs/scoring.html for more information
28
+ # about the scoring system used by MusicBrainz.
29
+ class ScoredCollection < Collection
30
+
31
+ # An entry in a ScoredCollection wrapping an entity and a
32
+ # corresponding search result score.
33
+ class Entry
34
+ include Comparable
35
+ attr_accessor :entity, :score
36
+
37
+ def initialize(entity, score=nil)
38
+ @entity = entity
39
+ @score = score
40
+ end
41
+
42
+ def <=>(other)
43
+ if self.score.nil? && other.score.nil?
44
+ return self.entity <=> other.entity
45
+ end
46
+ return -1 if self.score.nil?
47
+ return 1 if other.score.nil?
48
+
49
+ return self.score <=> other.score
50
+ end
51
+ end
52
+
53
+ # Return an array of all entities.
54
+ def entities
55
+ return map {|entry| entry.entity}
56
+ end
57
+
58
+ # Add a new entry to the collection.
59
+ #
60
+ # You may either add a ScoredCollection::Entry, just an Model::Entity
61
+ # or an object responding to +first+ and +last+ where +first+ must
62
+ # return the entity and +last+ the score.
63
+ #
64
+ # Examples:
65
+ # include MusicBrainz
66
+ # artist = Model::Artist.new
67
+ #
68
+ # collection << Model::ScoredCollection::Entry.new(artist, 100)
69
+ # collection << [artist, 100]
70
+ # collection << artist
71
+ def <<(entry)
72
+ super wrap(entry)
73
+ end
74
+
75
+ # Set the entry at the given position.
76
+ #
77
+ # You may either add a ScoredCollection::Entry, just an Model::Entity
78
+ # or an object responding to +first+ and +last+ where +first+ must
79
+ # return the entity and +last+ the score.
80
+ def []=(index, entry)
81
+ super wrap(entry)
82
+ end
83
+
84
+ private #-----------------------------------------------------------------
85
+
86
+ def wrap(entry)
87
+ unless entry.is_a? Entry
88
+ if entry.respond_to?(:first) and entry.respond_to?(:last)
89
+ entry = Entry.new(entry.first, entry.last)
90
+ else
91
+ entry = Entry.new(entry)
92
+ end
93
+ end
94
+ return entry
95
+ end
96
+ end
97
+
98
+ end
99
+ end
@@ -0,0 +1,39 @@
1
+ # $Id: tag.rb 145 2007-07-19 13:11:44Z phw $
2
+ #
3
+ # Author:: Philipp Wolfer (mailto:phw@rubyforge.org)
4
+ # Copyright:: Copyright (c) 2007, Nigel Graham, Philipp Wolfer
5
+ # License:: RBrainz is free software distributed under a BSD style license.
6
+ # See LICENSE[file:../LICENSE.html] for permissions.
7
+
8
+ module MusicBrainz
9
+ module Model
10
+
11
+ #
12
+ # Represents a tag assigned to an entity.
13
+ #
14
+ # Tags are arbitrary labels assigned to entities by the users of MusicBrainz.
15
+ #
16
+ # Note:: Tagging is not supported by the current MusicBrainz server but will
17
+ # be added in the near future.
18
+ class Tag
19
+ # The tag text.
20
+ attr_accessor :text
21
+
22
+ # The tag count indicating how often the tag was used for the entity it's
23
+ # assigned to.
24
+ attr_accessor :count
25
+
26
+ def initialize(text=nil, count=nil)
27
+ self.text = text
28
+ self.count = count
29
+ end
30
+
31
+ # Convert this tag into a String. Will return text.
32
+ def to_s
33
+ return text.to_s
34
+ end
35
+
36
+ end
37
+
38
+ end
39
+ end
@@ -1,7 +1,9 @@
1
- # $Id: track.rb 30 2007-05-29 02:12:33Z phw $
2
- # Copyright (c) 2007, Philipp Wolfer
3
- # All rights reserved.
4
- # See LICENSE for permissions.
1
+ # $Id: track.rb 145 2007-07-19 13:11:44Z phw $
2
+ #
3
+ # Author:: Philipp Wolfer (mailto:phw@rubyforge.org)
4
+ # Copyright:: Copyright (c) 2007, Nigel Graham, Philipp Wolfer
5
+ # License:: RBrainz is free software distributed under a BSD style license.
6
+ # See LICENSE[file:../LICENSE.html] for permissions.
5
7
 
6
8
  require 'rbrainz/model/entity'
7
9
 
@@ -10,17 +12,39 @@ module MusicBrainz
10
12
 
11
13
  # A track in the MusicBrainz DB.
12
14
  #
13
- # See http://musicbrainz.org/doc/Track.
15
+ # See:: http://musicbrainz.org/doc/Track.
14
16
  class Track < Entity
15
17
 
16
- attr_accessor :title, :duration, :artist
17
-
18
- attr_reader :puids, :releases
19
-
20
- def initialize
21
- super
22
- @puids = Array.new
23
- @releases = Array.new
18
+ # See Entity::ENTITY_TYPE.
19
+ ENTITY_TYPE = :track # :nodoc:
20
+
21
+ # The track's title.
22
+ attr_accessor :title
23
+
24
+ # The duration in milliseconds.
25
+ attr_accessor :duration
26
+
27
+ # The track's main artist.
28
+ attr_accessor :artist
29
+
30
+ # The list of associated PUIDs.
31
+ attr_reader :puids
32
+
33
+ # The releases on which this track appears.
34
+ attr_reader :releases
35
+
36
+ def initialize(id=nil, title=nil)
37
+ super id
38
+ self.title = title
39
+ @puids = Collection.new
40
+ @releases = Collection.new
41
+ end
42
+
43
+ # Returns the string representation for this track.
44
+ #
45
+ # Returns #title converted into a string.
46
+ def to_s
47
+ title.to_s
24
48
  end
25
49
 
26
50
  end
@@ -0,0 +1,48 @@
1
+ # $Id: user.rb 145 2007-07-19 13:11:44Z phw $
2
+ #
3
+ # Author:: Nigel Graham (mailto:nigel_graham@rubyforge.org)
4
+ # Copyright:: Copyright (c) 2007, Nigel Graham, Philipp Wolfer
5
+ # License:: RBrainz is free software distributed under a BSD style license.
6
+ # See LICENSE[file:../LICENSE.html] for permissions.
7
+
8
+ module MusicBrainz
9
+ module Model
10
+
11
+ # A MusicBrainz user.
12
+ class User
13
+ # The MusicBrainz user name.
14
+ attr_accessor :name
15
+
16
+ # The user's types.
17
+ #
18
+ # Most users' type list is empty. Currently, the following types
19
+ # are defined:
20
+ #
21
+ # - 'http://musicbrainz.org/ns/ext-1.0#AutoEditor'
22
+ # - 'http://musicbrainz.org/ns/ext-1.0#RelationshipEditor'
23
+ # - 'http://musicbrainz.org/ns/ext-1.0#Bot'
24
+ # - 'http://musicbrainz.org/ns/ext-1.0#NotNaggable'
25
+ attr_reader :types
26
+
27
+ def initialize(name=nil)
28
+ self.name = name
29
+ @types = []
30
+ end
31
+
32
+ # The setter for the nag screen flag.
33
+ def show_nag=(value)
34
+ @show_nag = (value.to_s == 'true')
35
+ end
36
+
37
+ # The value of the nag screen flag.
38
+ def show_nag?
39
+ @show_nag
40
+ end
41
+
42
+ def to_s
43
+ return name.to_s
44
+ end
45
+ end
46
+
47
+ end
48
+ end
@@ -0,0 +1,9 @@
1
+ # $Id: utils.rb 143 2007-07-18 11:40:55Z phw $
2
+ #
3
+ # Author:: Philipp Wolfer (mailto:phw@rubyforge.org)
4
+ # Copyright:: Copyright (c) 2007, Nigel Graham, Philipp Wolfer
5
+ # License:: RBrainz is free software distributed under a BSD style license.
6
+ # See LICENSE[file:../LICENSE.html] for permissions.
7
+
8
+ require 'rbrainz/version'
9
+ Dir[File.dirname(__FILE__) + "/utils/*.rb"].each { |file| require(file) }
@@ -0,0 +1,78 @@
1
+ # $Id: data.rb 141 2007-07-17 14:21:12Z phw $
2
+ #
3
+ # Helper methods to deal with MusicBrainz data.
4
+ #
5
+ # Author:: Philipp Wolfer (mailto:phw@rubyforge.org)
6
+ # Copyright:: Copyright (c) 2007, Nigel Graham, Philipp Wolfer
7
+ # License:: RBrainz is free software distributed under a BSD style license.
8
+ # See LICENSE[file:../LICENSE.html] for permissions.
9
+
10
+ require 'rbrainz/data/countrynames'
11
+ require 'rbrainz/data/languagenames'
12
+ require 'rbrainz/data/scriptnames'
13
+ require 'rbrainz/data/releasetypenames'
14
+
15
+ module MusicBrainz
16
+
17
+ # This module contains helper functions to make common tasks easier.
18
+ module Utils
19
+
20
+ # Returns a country's name based on an ISO-3166 country code.
21
+ #
22
+ # The country table this function is based on has been modified for
23
+ # MusicBrainz purposes by using the extension mechanism defined in ISO-3166.
24
+ # All IDs are still valid ISO-3166 country codes, but some IDs have been
25
+ # added to include historic countries and some of the country names have
26
+ # been modified to make them better suited for display purposes.
27
+ #
28
+ # If the country ID is not found, +nil+ is returned. This may happen for
29
+ # example, when new countries are added to the MusicBrainz web service which
30
+ # aren't known to this library yet.
31
+ #
32
+ # See:: #Data::COUNTRY_NAMES
33
+ def self.get_country_name(id)
34
+ return Data::COUNTRY_NAMES[id]
35
+ end
36
+
37
+ # Returns a language name based on an ISO-639-2/T code.
38
+ #
39
+ # This function uses a subset of the ISO-639-2/T code table to map language
40
+ # IDs (terminologic, not bibliographic ones!) to names.
41
+ #
42
+ # If the language ID is not found, +nil+ is returned. This may happen for
43
+ # example, when new languages are added to the MusicBrainz web service which
44
+ # aren't known to this library yet.
45
+ #
46
+ # See:: #Data::LANGUAGE_NAMES
47
+ def self.get_language_name(id)
48
+ return Data::LANGUAGE_NAMES[id]
49
+ end
50
+
51
+ # Returns a script name based on an ISO-15924 code.
52
+ #
53
+ # This function uses a subset of the ISO-15924 code table to map script
54
+ # IDs to names.
55
+ #
56
+ # If the script ID is not found, +nil+ is returned. This may happen for
57
+ # example, when new scripts are added to the MusicBrainz web service which
58
+ # aren't known to this library yet.
59
+ #
60
+ # See:: #Data::SCRIPT_NAMES
61
+ def self.get_script_name(id)
62
+ return Data::SCRIPT_NAMES[id]
63
+ end
64
+
65
+ # Returns the name of a release type URI.
66
+ #
67
+ # If the release type is not found, +nil+ is returned. This may happen for
68
+ # example, when new release types are added to the MusicBrainz web service
69
+ # which aren't known to this library yet.
70
+ #
71
+ # See:: #Data::RELEASE_TYPE_NAMES
72
+ # See:: Model::Release
73
+ def self.get_release_type_name(id)
74
+ return Data::RELEASE_TYPE_NAMES[id]
75
+ end
76
+
77
+ end
78
+ end
@@ -0,0 +1,22 @@
1
+ # $Id: helper.rb 141 2007-07-17 14:21:12Z phw $
2
+ #
3
+ # Author:: Nigel Graham, Philipp Wolfer
4
+ # Copyright:: Copyright (c) 2007, Nigel Graham, Philipp Wolfer
5
+ # License:: RBrainz is free software distributed under a BSD style license.
6
+ # See LICENSE[file:../LICENSE.html] for permissions.
7
+
8
+ module MusicBrainz
9
+ module Utils
10
+
11
+ # Check an options hash for required options.
12
+ # Raises an ArgumentError if unknown options are present in the hash.
13
+ def self.check_options(options, *optdecl) # :nodoc:
14
+ h = options.dup
15
+ optdecl.each do |name|
16
+ h.delete name
17
+ end
18
+ raise ArgumentError, "no such option: #{h.keys.join(' ')}" unless h.empty?
19
+ end
20
+
21
+ end
22
+ end
@@ -0,0 +1,15 @@
1
+ # $Id: version.rb 143 2007-07-18 11:40:55Z phw $
2
+ #
3
+ # Version information.
4
+ #
5
+ # Author:: Philipp Wolfer (mailto:phw@rubyforge.org)
6
+ # Copyright:: Copyright (c) 2007, Nigel Graham, Philipp Wolfer
7
+ # License:: RBrainz is free software distributed under a BSD style license.
8
+ # See LICENSE[file:../LICENSE.html] for permissions.
9
+
10
+ module MusicBrainz
11
+
12
+ # The version of the RBrainz library.
13
+ RBRAINZ_VERSION = '0.2.0'
14
+
15
+ end