buntine-discogs 0.3.1
Sign up to get free protection for your applications and to get access to all the features.
- data/README +61 -0
- data/Rakefile +27 -0
- data/discogs.gemspec +19 -0
- data/lib/discogs.rb +10 -0
- data/lib/wrapper/resource.rb +75 -0
- data/lib/wrapper/resource_mappings.rb +80 -0
- data/lib/wrapper/resources/artist.rb +16 -0
- data/lib/wrapper/resources/artist_release.rb +16 -0
- data/lib/wrapper/resources/format.rb +11 -0
- data/lib/wrapper/resources/generic_list.rb +29 -0
- data/lib/wrapper/resources/image.rb +11 -0
- data/lib/wrapper/resources/label.rb +16 -0
- data/lib/wrapper/resources/label_release.rb +17 -0
- data/lib/wrapper/resources/release.rb +22 -0
- data/lib/wrapper/resources/release_artist.rb +18 -0
- data/lib/wrapper/resources/release_label.rb +10 -0
- data/lib/wrapper/resources/search.rb +17 -0
- data/lib/wrapper/resources/search_result.rb +16 -0
- data/lib/wrapper/resources/track.rb +15 -0
- data/lib/wrapper/wrapper.rb +88 -0
- data/spec/resource_spec.rb +27 -0
- data/spec/resources/artist_release_spec.rb +55 -0
- data/spec/resources/artist_spec.rb +15 -0
- data/spec/resources/format_spec.rb +41 -0
- data/spec/resources/generic_list_spec.rb +66 -0
- data/spec/resources/image_spec.rb +43 -0
- data/spec/resources/label_release_spec.rb +55 -0
- data/spec/resources/label_spec.rb +15 -0
- data/spec/resources/release_artist_spec.rb +43 -0
- data/spec/resources/release_label_spec.rb +31 -0
- data/spec/resources/release_spec.rb +15 -0
- data/spec/resources/search_result_spec.rb +47 -0
- data/spec/resources/search_spec.rb +15 -0
- data/spec/resources/track_spec.rb +56 -0
- data/spec/spec_helper.rb +35 -0
- data/spec/wrapper_methods/get_artist_spec.rb +95 -0
- data/spec/wrapper_methods/get_label_spec.rb +91 -0
- data/spec/wrapper_methods/get_release_spec.rb +125 -0
- data/spec/wrapper_methods/search_spec.rb +99 -0
- data/spec/wrapper_spec.rb +100 -0
- metadata +93 -0
@@ -0,0 +1,17 @@
|
|
1
|
+
# Represents a search resultset in the Discogs API.
|
2
|
+
|
3
|
+
class Discogs::Search < Discogs::Resource
|
4
|
+
|
5
|
+
no_mapping
|
6
|
+
|
7
|
+
attr_accessor :exactresults,
|
8
|
+
:searchresults,
|
9
|
+
:start,
|
10
|
+
:end,
|
11
|
+
:numResults
|
12
|
+
|
13
|
+
def total_results
|
14
|
+
self.numResults
|
15
|
+
end
|
16
|
+
|
17
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
# Represents a single search result in the Discogs API.
|
2
|
+
|
3
|
+
require File.dirname(__FILE__) + "/search"
|
4
|
+
|
5
|
+
class Discogs::Search::Result < Discogs::Resource
|
6
|
+
|
7
|
+
map_to_plural :exactresults, :searchresults
|
8
|
+
|
9
|
+
attr_accessor :num,
|
10
|
+
:type,
|
11
|
+
:title,
|
12
|
+
:uri,
|
13
|
+
:anv,
|
14
|
+
:summary
|
15
|
+
|
16
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
# Represents a track in the Discogs API.
|
2
|
+
|
3
|
+
require File.dirname(__FILE__) + "/release"
|
4
|
+
|
5
|
+
class Discogs::Release::Track < Discogs::Resource
|
6
|
+
|
7
|
+
map_to_plural :tracklist
|
8
|
+
|
9
|
+
attr_accessor :position,
|
10
|
+
:title,
|
11
|
+
:duration,
|
12
|
+
:artists,
|
13
|
+
:extraartists
|
14
|
+
|
15
|
+
end
|
@@ -0,0 +1,88 @@
|
|
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, type=:all)
|
34
|
+
params = { :q => term, :type => type }
|
35
|
+
data = query_api("search", params)
|
36
|
+
resource = Discogs::Search.new(data)
|
37
|
+
|
38
|
+
resource.build_with_resp!
|
39
|
+
end
|
40
|
+
|
41
|
+
private
|
42
|
+
|
43
|
+
def query_and_build(path, klass)
|
44
|
+
data = query_api(path)
|
45
|
+
resource = klass.send(:new, data)
|
46
|
+
resource.build!
|
47
|
+
end
|
48
|
+
|
49
|
+
# Queries the API and handles the response.
|
50
|
+
def query_api(path, params={})
|
51
|
+
response = make_request(path, params)
|
52
|
+
|
53
|
+
raise_unknown_resource(path) if response.code == "404"
|
54
|
+
raise_invalid_api_key if response.code == "400"
|
55
|
+
|
56
|
+
# Unzip the response data.
|
57
|
+
inflated_data = Zlib::GzipReader.new(StringIO.new(response.body))
|
58
|
+
inflated_data.read
|
59
|
+
end
|
60
|
+
|
61
|
+
# Generates a HTTP request and returns the response.
|
62
|
+
def make_request(path, params={})
|
63
|
+
uri = build_uri(path, params)
|
64
|
+
|
65
|
+
request = Net::HTTP::Get.new(uri.path + "?" + uri.query)
|
66
|
+
request.add_field("Accept-Encoding", "gzip,deflate")
|
67
|
+
|
68
|
+
response = Net::HTTP.new(uri.host).start do |http|
|
69
|
+
http.request(request)
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
def build_uri(path, params={})
|
74
|
+
parameters = { :f => "xml", :api_key => @api_key }.merge(params)
|
75
|
+
querystring = "?" + parameters.map { |key, value| "#{key}=#{value}" }.join("&")
|
76
|
+
|
77
|
+
URI.parse(File.join(@@root_host, path + querystring))
|
78
|
+
end
|
79
|
+
|
80
|
+
def raise_invalid_api_key
|
81
|
+
raise Discogs::InvalidAPIKey, "Invalid API key: #{@api_key}"
|
82
|
+
end
|
83
|
+
|
84
|
+
def raise_unknown_resource(path='')
|
85
|
+
raise Discogs::UnknownResource, "Unknown Discogs resource: #{path}"
|
86
|
+
end
|
87
|
+
|
88
|
+
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,55 @@
|
|
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
|
+
end
|
54
|
+
|
55
|
+
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
|