bassnode-discogs 0.3.2

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.
Files changed (59) hide show
  1. data/.gitignore +5 -0
  2. data/COPYING +619 -0
  3. data/LICENSE +5 -0
  4. data/README.markdown +75 -0
  5. data/Rakefile +27 -0
  6. data/bassnode-discogs.gemspec +18 -0
  7. data/lib/discogs.rb +21 -0
  8. data/lib/wrapper/resource.rb +79 -0
  9. data/lib/wrapper/resource_mappings.rb +84 -0
  10. data/lib/wrapper/resources/artist.rb +37 -0
  11. data/lib/wrapper/resources/artist_release.rb +17 -0
  12. data/lib/wrapper/resources/format.rb +11 -0
  13. data/lib/wrapper/resources/generic_list.rb +29 -0
  14. data/lib/wrapper/resources/image.rb +11 -0
  15. data/lib/wrapper/resources/label.rb +16 -0
  16. data/lib/wrapper/resources/label_release.rb +17 -0
  17. data/lib/wrapper/resources/release.rb +22 -0
  18. data/lib/wrapper/resources/release_artist.rb +18 -0
  19. data/lib/wrapper/resources/release_label.rb +10 -0
  20. data/lib/wrapper/resources/search.rb +61 -0
  21. data/lib/wrapper/resources/search_result.rb +16 -0
  22. data/lib/wrapper/resources/track.rb +15 -0
  23. data/lib/wrapper/wrapper.rb +109 -0
  24. data/spec/resource_spec.rb +27 -0
  25. data/spec/resources/artist_release_spec.rb +59 -0
  26. data/spec/resources/artist_spec.rb +15 -0
  27. data/spec/resources/format_spec.rb +41 -0
  28. data/spec/resources/generic_list_spec.rb +66 -0
  29. data/spec/resources/image_spec.rb +43 -0
  30. data/spec/resources/label_release_spec.rb +55 -0
  31. data/spec/resources/label_spec.rb +15 -0
  32. data/spec/resources/release_artist_spec.rb +43 -0
  33. data/spec/resources/release_label_spec.rb +31 -0
  34. data/spec/resources/release_spec.rb +15 -0
  35. data/spec/resources/search_result_spec.rb +47 -0
  36. data/spec/resources/search_spec.rb +15 -0
  37. data/spec/resources/track_spec.rb +56 -0
  38. data/spec/samples/valid_artist.xml +76 -0
  39. data/spec/samples/valid_artist_release.xml +8 -0
  40. data/spec/samples/valid_description_list.xml +5 -0
  41. data/spec/samples/valid_format.xml +7 -0
  42. data/spec/samples/valid_genre_list.xml +5 -0
  43. data/spec/samples/valid_image.xml +1 -0
  44. data/spec/samples/valid_label.xml +34 -0
  45. data/spec/samples/valid_label_release.xml +8 -0
  46. data/spec/samples/valid_release.xml +99 -0
  47. data/spec/samples/valid_release_artist.xml +8 -0
  48. data/spec/samples/valid_release_label.xml +1 -0
  49. data/spec/samples/valid_search_result.xml +6 -0
  50. data/spec/samples/valid_search_results_1.xml +142 -0
  51. data/spec/samples/valid_search_results_2.xml +206 -0
  52. data/spec/samples/valid_track.xml +16 -0
  53. data/spec/spec_helper.rb +36 -0
  54. data/spec/wrapper_methods/get_artist_spec.rb +109 -0
  55. data/spec/wrapper_methods/get_label_spec.rb +89 -0
  56. data/spec/wrapper_methods/get_release_spec.rb +123 -0
  57. data/spec/wrapper_methods/search_spec.rb +330 -0
  58. data/spec/wrapper_spec.rb +180 -0
  59. metadata +125 -0
@@ -0,0 +1,109 @@
1
+ # Core API wrapper class.
2
+
3
+ require 'uri'
4
+ require 'net/http'
5
+ require 'rexml/document'
6
+ require 'zlib'
7
+ require 'stringio'
8
+
9
+ require File.dirname(__FILE__) + "/resource"
10
+
11
+ class Discogs::Wrapper
12
+
13
+ @@root_host = "http://www.discogs.com"
14
+
15
+ attr_reader :api_key
16
+
17
+ def initialize(api_key=nil)
18
+ @api_key = api_key
19
+ end
20
+
21
+ def get_release(id)
22
+ query_and_build "release/#{id}", Discogs::Release
23
+ end
24
+
25
+ def get_artist(name)
26
+ query_and_build "artist/#{name}", Discogs::Artist
27
+ end
28
+
29
+ def get_label(name)
30
+ query_and_build "label/#{name}", Discogs::Label
31
+ end
32
+
33
+ def search(term, options={})
34
+ opts = { :type => :all, :page => 1 }.merge(options)
35
+ params = { :q => term, :type => opts[:type], :page => opts[:page] }
36
+
37
+ data = query_api("search", params)
38
+ resource = Discogs::Search.new(data)
39
+
40
+ resource.build_with_resp!
41
+ end
42
+
43
+ private
44
+
45
+ def query_and_build(path, klass)
46
+ data = query_api(path)
47
+ resource = klass.send(:new, data)
48
+ resource.build!
49
+ end
50
+
51
+ # Queries the API and handles the response.
52
+ def query_api(path, params={})
53
+ response = make_request(path, params)
54
+
55
+ raise_unknown_resource(path) if response.code == "404"
56
+ raise_invalid_api_key if response.code == "400"
57
+ raise_internal_server_error if response.code == "500"
58
+
59
+ # Unzip the response data, or just read it in directly
60
+ # if the API responds without gzipping.
61
+ response_body = nil
62
+ begin
63
+ inflated_data = Zlib::GzipReader.new(StringIO.new(response.body))
64
+ response_body = inflated_data.read
65
+ rescue Zlib::GzipFile::Error
66
+ response_body = response.body
67
+ end
68
+
69
+ response_body
70
+ end
71
+
72
+ # Generates a HTTP request and returns the response.
73
+ def make_request(path, params={})
74
+ uri = build_uri(path, params)
75
+
76
+ request = Net::HTTP::Get.new(uri.path + "?" + uri.query)
77
+ request.add_field("Accept-Encoding", "gzip,deflate")
78
+
79
+ Net::HTTP.new(uri.host).start do |http|
80
+ http.request(request)
81
+ end
82
+ end
83
+
84
+ def build_uri(path, params={})
85
+ parameters = { :f => "xml", :api_key => @api_key }.merge(params)
86
+ querystring = "?" + parameters.map { |key, value| "#{key}=#{value}" }.sort.join("&")
87
+
88
+ URI.parse(File.join(@@root_host, sanitize_path(path, querystring)))
89
+ end
90
+
91
+ def sanitize_path(*path_parts)
92
+ clean_path = path_parts.map { |part| part.gsub(/\s/, '+') }
93
+
94
+ clean_path.join
95
+ end
96
+
97
+ def raise_invalid_api_key
98
+ raise Discogs::InvalidAPIKey, "Invalid API key: #{@api_key}"
99
+ end
100
+
101
+ def raise_unknown_resource(path='')
102
+ raise Discogs::UnknownResource, "Unknown Discogs resource: #{path}"
103
+ end
104
+
105
+ def raise_internal_server_error
106
+ raise Discogs::InternalServerError, "The remote server cannot complete the request"
107
+ end
108
+
109
+ end
@@ -0,0 +1,27 @@
1
+ require File.dirname(__FILE__) + "/spec_helper"
2
+
3
+ describe Discogs::Resource do
4
+
5
+ before do
6
+ @resource = Discogs::Resource.new(sample_valid_binary)
7
+ end
8
+
9
+ it "should have a default element name" do
10
+ Discogs::Resource.element_names.should == [ :resource ]
11
+ end
12
+
13
+ it "should have a default plural element name" do
14
+ Discogs::Resource.plural_element_names.should == [ :resources ]
15
+ end
16
+
17
+ it "should have an original_content method" do
18
+ @resource.original_content.should == sample_valid_binary
19
+ end
20
+
21
+ it "should be able to build the content"
22
+
23
+ it "should remove the <resp> element by default"
24
+
25
+ it "should leave the <resp> element if told to"
26
+
27
+ end
@@ -0,0 +1,59 @@
1
+ require File.dirname(__FILE__) + "/../spec_helper"
2
+
3
+ describe Discogs::Artist::Release do
4
+
5
+ it "should map to release" do
6
+ Discogs::Artist::Release.element_names.should == [ :release ]
7
+ end
8
+
9
+ it "should map to plural releases" do
10
+ Discogs::Artist::Release.plural_element_names.should == [ :releases ]
11
+ end
12
+
13
+ describe "when asking for artist-release information" do
14
+
15
+ before do
16
+ data = File.read(File.join(File.dirname(__FILE__), "..", "samples", "valid_artist_release.xml"))
17
+ @artist_release = Discogs::Artist::Release.new(data)
18
+ @artist_release.build!
19
+ end
20
+
21
+ it "should have a ID attribute" do
22
+ @artist_release.id.should == "999333"
23
+ end
24
+
25
+ it "should have a status attribute" do
26
+ @artist_release.status.should == "Accepted"
27
+ end
28
+
29
+ it "should have a type attribute" do
30
+ @artist_release.type.should == "Main"
31
+ end
32
+
33
+ it "should have a title attribute" do
34
+ @artist_release.title.should == "Temple of the Underworld"
35
+ end
36
+
37
+ it "should have a format attribute" do
38
+ @artist_release.format.should == "LP"
39
+ end
40
+
41
+ it "should have a label attribute" do
42
+ @artist_release.label.should == "Monitor Records"
43
+ end
44
+
45
+ it "should have a year attribute" do
46
+ @artist_release.year.should == "1992"
47
+ end
48
+
49
+ it "should have a trackinfo attribute" do
50
+ @artist_release.trackinfo.should be_nil
51
+ end
52
+
53
+ it "should have an artist attribute" do
54
+ @artist_release.artist.should == "ArtIst"
55
+ end
56
+
57
+ end
58
+
59
+ end
@@ -0,0 +1,15 @@
1
+ require File.dirname(__FILE__) + "/../spec_helper"
2
+
3
+ describe Discogs::Artist do
4
+
5
+ it "should map to empty array" do
6
+ Discogs::Artist.element_names.should == []
7
+ end
8
+
9
+ it "should map plural to empty array" do
10
+ Discogs::Artist.plural_element_names.should == []
11
+ end
12
+
13
+ ## See ./spec/wrapper_methods/get_artist_spec.rb for extensive tests of this class.
14
+
15
+ end
@@ -0,0 +1,41 @@
1
+ require File.dirname(__FILE__) + "/../spec_helper"
2
+
3
+ describe Discogs::Release::Format do
4
+
5
+ it "should map to format" do
6
+ Discogs::Release::Format.element_names.should == [ :format ]
7
+ end
8
+
9
+ it "should map to plural formats" do
10
+ Discogs::Release::Format.plural_element_names.should == [ :formats ]
11
+ end
12
+
13
+ describe "when asking for format information" do
14
+
15
+ before do
16
+ data = File.read(File.join(File.dirname(__FILE__), "..", "samples", "valid_format.xml"))
17
+ @format = Discogs::Release::Format.new(data)
18
+ @format.build!
19
+ end
20
+
21
+ it "should have a name attribute" do
22
+ @format.name.should == "CD"
23
+ end
24
+
25
+ it "should have a quantity attribute" do
26
+ @format.qty.should == "1"
27
+ end
28
+
29
+ it "should have an array of descriptions" do
30
+ @format.descriptions.should be_instance_of(Array)
31
+ end
32
+
33
+ it "should have built each description" do
34
+ @format.descriptions[0].should == "Album"
35
+ @format.descriptions[1].should == "Digipak"
36
+ @format.descriptions[9].should be_nil
37
+ end
38
+
39
+ end
40
+
41
+ end
@@ -0,0 +1,66 @@
1
+ require File.dirname(__FILE__) + "/../spec_helper"
2
+
3
+ describe Discogs::GenericList do
4
+
5
+ it "should map to all generic lists" do
6
+ Discogs::GenericList.element_names.should be_instance_of(Array)
7
+
8
+ Discogs::GenericList.element_names.include?(:aliases).should be_true
9
+ Discogs::GenericList.element_names.include?(:urls).should be_true
10
+ end
11
+
12
+ it "should map to plural lists" do
13
+ Discogs::GenericList.plural_element_names.should == [ :lists ]
14
+ end
15
+
16
+ describe "when asking for description information" do
17
+
18
+ before do
19
+ data = File.read(File.join(File.dirname(__FILE__), "..", "samples", "valid_description_list.xml"))
20
+ @descriptions = Discogs::GenericList.new(data)
21
+ end
22
+
23
+ it "should return an array on build" do
24
+ @descriptions.build!.should be_instance_of(Array)
25
+ end
26
+
27
+ it "should have access to the items after build" do
28
+ @descriptions.build!
29
+
30
+ @descriptions[0].should == "Album"
31
+ @descriptions[1].should == "LP"
32
+ @descriptions[2].should == "33 rpm"
33
+ end
34
+
35
+ it "should return nil on unknown index" do
36
+ @descriptions[9].should be_nil
37
+ end
38
+
39
+ end
40
+
41
+ describe "when asking for genre information" do
42
+
43
+ before do
44
+ data = File.read(File.join(File.dirname(__FILE__), "..", "samples", "valid_genre_list.xml"))
45
+ @genres = Discogs::GenericList.new(data)
46
+ end
47
+
48
+ it "should return an array on build" do
49
+ @genres.build!.should be_instance_of(Array)
50
+ end
51
+
52
+ it "should have access to the items after build" do
53
+ @genres.build!
54
+
55
+ @genres[0].should == "Heavy Metal"
56
+ @genres[1].should == "Neofolk"
57
+ @genres[2].should == "Medieval folk"
58
+ end
59
+
60
+ it "should return nil on unknown index" do
61
+ @genres[9].should be_nil
62
+ end
63
+
64
+ end
65
+
66
+ end
@@ -0,0 +1,43 @@
1
+ require File.dirname(__FILE__) + "/../spec_helper"
2
+
3
+ describe Discogs::Image do
4
+
5
+ it "should map to image" do
6
+ Discogs::Image.element_names.should == [ :image ]
7
+ end
8
+
9
+ it "should map to plural images" do
10
+ Discogs::Image.plural_element_names.should == [ :images ]
11
+ end
12
+
13
+ describe "when asking for image information" do
14
+
15
+ before do
16
+ data = File.read(File.join(File.dirname(__FILE__), "..", "samples", "valid_image.xml"))
17
+ @image = Discogs::Image.new(data)
18
+ @image.build!
19
+ end
20
+
21
+ it "should have a type attribute" do
22
+ @image.type.should == "primary"
23
+ end
24
+
25
+ it "should have a width attribute" do
26
+ @image.width.should == "600"
27
+ end
28
+
29
+ it "should have a height attribute" do
30
+ @image.height.should == "595"
31
+ end
32
+
33
+ it "should have a uri attribute" do
34
+ @image.uri.should == "http://www.discogs.com/image/R-666-1222413500.jpeg"
35
+ end
36
+
37
+ it "should have a uri150 attribute" do
38
+ @image.uri150.should == "http://www.discogs.com/image/R-150-666-1222413500.jpeg"
39
+ end
40
+
41
+ end
42
+
43
+ end
@@ -0,0 +1,55 @@
1
+ require File.dirname(__FILE__) + "/../spec_helper"
2
+
3
+ describe Discogs::Label::Release do
4
+
5
+ it "should map to release" do
6
+ Discogs::Label::Release.element_names.should == [ :release ]
7
+ end
8
+
9
+ it "should map to plural releases" do
10
+ Discogs::Label::Release.plural_element_names.should == [ :releases ]
11
+ end
12
+
13
+ describe "when asking for label-release information" do
14
+
15
+ before do
16
+ data = File.read(File.join(File.dirname(__FILE__), "..", "samples", "valid_label_release.xml"))
17
+ @label_release = Discogs::Label::Release.new(data)
18
+ @label_release.build!
19
+ end
20
+
21
+ it "should have a ID attribute" do
22
+ @label_release.id.should == "116322"
23
+ end
24
+
25
+ it "should have a status attribute" do
26
+ @label_release.status.should == "Accepted"
27
+ end
28
+
29
+ it "should have a catno attribute" do
30
+ @label_release.catno.should == "SMB09"
31
+ end
32
+
33
+ it "should have an artist attribute" do
34
+ @label_release.artist.should == "Morrior"
35
+ end
36
+
37
+ it "should have a title attribute" do
38
+ @label_release.title.should == "Death Metal Session"
39
+ end
40
+
41
+ it "should have a format attribute" do
42
+ @label_release.format.should == "12\""
43
+ end
44
+
45
+ it "should have a year attribute" do
46
+ @label_release.year.should == "1991"
47
+ end
48
+
49
+ it "should have a trackinfo attribute" do
50
+ @label_release.trackinfo.should == "Side A, track 4"
51
+ end
52
+
53
+ end
54
+
55
+ end
@@ -0,0 +1,15 @@
1
+ require File.dirname(__FILE__) + "/../spec_helper"
2
+
3
+ describe Discogs::Label do
4
+
5
+ it "should map to empty array" do
6
+ Discogs::Label.element_names.should == []
7
+ end
8
+
9
+ it "should map plural to empty array" do
10
+ Discogs::Label.plural_element_names.should == []
11
+ end
12
+
13
+ ## See ./spec/wrapper_methods/get_label_spec.rb for extensive tests of this class.
14
+
15
+ end
@@ -0,0 +1,43 @@
1
+ require File.dirname(__FILE__) + "/../spec_helper"
2
+
3
+ describe Discogs::Release::Artist do
4
+
5
+ it "should map to artist" do
6
+ Discogs::Release::Artist.element_names.should == [ :artist ]
7
+ end
8
+
9
+ it "should map to plural artists and extraartists" do
10
+ Discogs::Release::Artist.plural_element_names.should == [ :artists, :extraartists ]
11
+ end
12
+
13
+ describe "when asking for release-artist information" do
14
+
15
+ before do
16
+ data = File.read(File.join(File.dirname(__FILE__), "..", "samples", "valid_release_artist.xml"))
17
+ @release_artist = Discogs::Release::Artist.new(data)
18
+ @release_artist.build!
19
+ end
20
+
21
+ it "should have a name attribute" do
22
+ @release_artist.name.should == "Master's Hammer"
23
+ end
24
+
25
+ it "should have a role attribute" do
26
+ @release_artist.role.should == "Shrieks"
27
+ end
28
+
29
+ it "should have a join attribute" do
30
+ @release_artist.join.should == "Relatives"
31
+ end
32
+
33
+ it "should have an anv attribute" do
34
+ @release_artist.anv.should == "wtf?"
35
+ end
36
+
37
+ it "should have a tracks attribute" do
38
+ @release_artist.tracks.should == "1,2,4"
39
+ end
40
+
41
+ end
42
+
43
+ end
@@ -0,0 +1,31 @@
1
+ require File.dirname(__FILE__) + "/../spec_helper"
2
+
3
+ describe Discogs::Release::Label do
4
+
5
+ it "should map to label" do
6
+ Discogs::Release::Label.element_names.should == [ :label ]
7
+ end
8
+
9
+ it "should map to plural labels" do
10
+ Discogs::Release::Label.plural_element_names.should == [ :labels ]
11
+ end
12
+
13
+ describe "when asking for release-label information" do
14
+
15
+ before do
16
+ data = File.read(File.join(File.dirname(__FILE__), "..", "samples", "valid_release_label.xml"))
17
+ @release_label = Discogs::Release::Label.new(data)
18
+ @release_label.build!
19
+ end
20
+
21
+ it "should have a name attribute" do
22
+ @release_label.name.should == "Toxic Diffusion"
23
+ end
24
+
25
+ it "should have a catno attribute" do
26
+ @release_label.catno.should == "clp89"
27
+ end
28
+
29
+ end
30
+
31
+ end
@@ -0,0 +1,15 @@
1
+ require File.dirname(__FILE__) + "/../spec_helper"
2
+
3
+ describe Discogs::Release do
4
+
5
+ it "should map to empty array" do
6
+ Discogs::Release.element_names.should == []
7
+ end
8
+
9
+ it "should map plural to empty array" do
10
+ Discogs::Release.plural_element_names.should == []
11
+ end
12
+
13
+ ## See ./spec/wrapper_methods/get_release_spec.rb for extensive tests of this class.
14
+
15
+ end
@@ -0,0 +1,47 @@
1
+ require File.dirname(__FILE__) + "/../spec_helper"
2
+
3
+ describe Discogs::Search::Result do
4
+
5
+ it "should map to result" do
6
+ Discogs::Search::Result.element_names.should == [ :result ]
7
+ end
8
+
9
+ it "should map to plural exactresults and searchresults" do
10
+ Discogs::Search::Result.plural_element_names.should == [ :exactresults, :searchresults ]
11
+ end
12
+
13
+ describe "when asking for search result information" do
14
+
15
+ before do
16
+ data = File.read(File.join(File.dirname(__FILE__), "..", "samples", "valid_search_result.xml"))
17
+ @search_result = Discogs::Search::Result.new(data)
18
+ @search_result.build!
19
+ end
20
+
21
+ it "should have a number attribute" do
22
+ @search_result.num.should == "2"
23
+ end
24
+
25
+ it "should have a type attribute" do
26
+ @search_result.type.should == "artist"
27
+ end
28
+
29
+ it "should have a title attribute" do
30
+ @search_result.title.should == "Karin Slaughter"
31
+ end
32
+
33
+ it "should have a uri attribute" do
34
+ @search_result.uri.should == "http://www.discogs.com/artist/Karin+Slaughter"
35
+ end
36
+
37
+ it "should have a summary attribute" do
38
+ @search_result.summary.should == "Karin Slaughter Karin Slaughter"
39
+ end
40
+
41
+ it "should have a anv attribute" do
42
+ @search_result.anv.should == "Slaughter"
43
+ end
44
+
45
+ end
46
+
47
+ end
@@ -0,0 +1,15 @@
1
+ require File.dirname(__FILE__) + "/../spec_helper"
2
+
3
+ describe Discogs::Search do
4
+
5
+ it "should map to empty array" do
6
+ Discogs::Search.element_names.should == []
7
+ end
8
+
9
+ it "should map plural to empty array" do
10
+ Discogs::Search.plural_element_names.should == []
11
+ end
12
+
13
+ ## See ./spec/wrapper_methods/search_spec.rb for extensive tests of this class.
14
+
15
+ end
@@ -0,0 +1,56 @@
1
+ require File.dirname(__FILE__) + "/../spec_helper"
2
+
3
+ describe Discogs::Release::Track do
4
+
5
+ it "should map to track" do
6
+ Discogs::Release::Track.element_names.should == [ :track ]
7
+ end
8
+
9
+ it "should map to plural tracklist" do
10
+ Discogs::Release::Track.plural_element_names.should == [ :tracklist ]
11
+ end
12
+
13
+ describe "when asking for track information" do
14
+
15
+ before do
16
+ data = File.read(File.join(File.dirname(__FILE__), "..", "samples", "valid_track.xml"))
17
+ @track = Discogs::Release::Track.new(data)
18
+ @track.build!
19
+ end
20
+
21
+ it "should have a title attribute" do
22
+ @track.title.should == "A dark forest spreads all around"
23
+ end
24
+
25
+ it "should have a duration attribute" do
26
+ @track.duration.should == "7:31"
27
+ end
28
+
29
+ it "should have a position attribute" do
30
+ @track.position.should == "2"
31
+ end
32
+
33
+ it "should have an array of artists" do
34
+ @track.artists.should be_instance_of(Array)
35
+ end
36
+
37
+ it "should have an array of extra artists" do
38
+ @track.extraartists.should be_instance_of(Array)
39
+ end
40
+
41
+ it "should have built each artist" do
42
+ @track.artists[0].should be_instance_of(Discogs::Release::Track::Artist)
43
+
44
+ @track.artists[0].name.should == "Master's Hammer"
45
+ end
46
+
47
+ it "should have built each extra artist" do
48
+ @track.extraartists[0].should be_instance_of(Discogs::Release::Track::Artist)
49
+
50
+ @track.extraartists[0].name.should == "Root"
51
+ @track.extraartists[0].role.should == "Imagery"
52
+ end
53
+
54
+ end
55
+
56
+ end