discogs-wrapper 1.0.0 → 1.1.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.
data/README.markdown CHANGED
@@ -13,6 +13,7 @@ ABOUT
13
13
 
14
14
  * Artists
15
15
  * Releases
16
+ * MasterReleases
16
17
  * Labels
17
18
  * Searching (all of the above)
18
19
 
@@ -24,17 +25,14 @@ INSTALLATION
24
25
  ------------
25
26
  You can install the library via Rubygems:
26
27
 
27
- $ gem sources -a http://gems.github.com
28
- $ sudo gem install buntine-discogs
28
+ $ sudo gem install discogs-wrapper
29
29
 
30
30
  USAGE
31
31
  -----
32
- To use this library, you must supply a valid User-agent value. For example:
32
+ To use this library, you must supply the name of your application. For example:
33
33
 
34
34
  require 'discogs'
35
- wrapper = Discogs::Wrapper.new("Mozilla/5.0 (X11; Linux i686; rv:6.0.2) Gecko/20100101 Firefox/6.0.2")
36
-
37
- I suggest passing on your users User-agent.
35
+ wrapper = Discogs::Wrapper.new("My awesome web app")
38
36
 
39
37
  Accessing information is easy:
40
38
 
data/discogs.gemspec CHANGED
@@ -1,8 +1,8 @@
1
1
  Gem::Specification.new do |s|
2
2
 
3
3
  s.name = "discogs-wrapper"
4
- s.version = "1.0.0"
5
- s.date = "2011-09-27"
4
+ s.version = "1.1.0"
5
+ s.date = "2011-10-22"
6
6
  s.summary = "Discogs::Wrapper is a full wrapper for the http://www.discogs.com API V2"
7
7
  s.homepage = "http://www.github.com/buntine/discogs"
8
8
  s.email = "info@andrewbuntine.com"
@@ -76,4 +76,5 @@ class Discogs::Resource
76
76
 
77
77
  end
78
78
 
79
+ # Load in all of the resource definitions.
79
80
  Dir[File.join(File.dirname(__FILE__), "resources", "*.rb")].each { |file| require file }
@@ -12,7 +12,7 @@ module Discogs::ResourceMappings
12
12
 
13
13
  module ClassMethods
14
14
 
15
- # Helper method to map resource to element in API response.
15
+ # Helper method to map resource to an element in the API response.
16
16
  def map_to(*elements)
17
17
  self.class_eval <<-EOF
18
18
  def self.element_names
@@ -51,11 +51,7 @@ module Discogs::ResourceMappings
51
51
  private
52
52
 
53
53
  def find_resource_for_name(name, type=:singular)
54
- method = if type == :singular
55
- :element_names
56
- else
57
- :plural_element_names
58
- end
54
+ method = (type == :singular) ? :element_names : :plural_element_names
59
55
 
60
56
  find_resource do |klass|
61
57
  klass.constants.find do |const|
@@ -27,10 +27,10 @@ class Discogs::Artist < Discogs::Resource
27
27
 
28
28
  private
29
29
 
30
- # Simple helper for filtering a particular type of release.
31
- def filter_releases(type)
30
+ # Simple helper for filtering a particular role of release.
31
+ def filter_releases(role)
32
32
  self.releases.find_all do |release|
33
- release.type == type
33
+ release.role == role
34
34
  end
35
35
  end
36
36
 
@@ -6,12 +6,23 @@ class Discogs::Artist::Release < Discogs::Resource
6
6
 
7
7
  attr_accessor :id,
8
8
  :status,
9
- :type,
9
+ :role,
10
10
  :title,
11
11
  :artist,
12
12
  :format,
13
13
  :year,
14
14
  :label,
15
- :trackinfo
15
+ :thumb,
16
+ :trackinfo,
17
+ :main_release
18
+
19
+ # Will return either "master" or "release".
20
+ def release_type
21
+ if original_content =~ /^\<(\w+)\s/
22
+ $1
23
+ else
24
+ "release"
25
+ end
26
+ end
16
27
 
17
28
  end
@@ -5,12 +5,14 @@ class Discogs::Release < Discogs::Resource
5
5
  no_mapping
6
6
 
7
7
  attr_accessor :id,
8
+ :master_id,
8
9
  :status,
9
10
  :title,
10
11
  :country,
11
12
  :released,
12
13
  :notes,
13
14
  :images,
15
+ :videos,
14
16
  :artists,
15
17
  :extraartists,
16
18
  :labels,
@@ -1,6 +1,7 @@
1
1
  # Represents an release's artist in the Discogs API.
2
2
 
3
3
  require File.dirname(__FILE__) + "/track"
4
+ require File.dirname(__FILE__) + "/master_release"
4
5
 
5
6
  class Discogs::Release::Artist < Discogs::Resource
6
7
 
@@ -16,3 +17,5 @@ end
16
17
 
17
18
  # Define other classes that also replicate this structure.
18
19
  class Discogs::Release::Track::Artist < Discogs::Release::Artist; end
20
+ class Discogs::MasterRelease::Artist < Discogs::Release::Artist; end
21
+ class Discogs::MasterRelease::Track::Artist < Discogs::Release::Artist; end
@@ -1,6 +1,7 @@
1
1
  # Represents a track in the Discogs API.
2
2
 
3
3
  require File.dirname(__FILE__) + "/release"
4
+ require File.dirname(__FILE__) + "/master_release"
4
5
 
5
6
  class Discogs::Release::Track < Discogs::Resource
6
7
 
@@ -13,3 +14,6 @@ class Discogs::Release::Track < Discogs::Resource
13
14
  :extraartists
14
15
 
15
16
  end
17
+
18
+ # Define other classes that also replicate this structure.
19
+ class Discogs::MasterRelease::Track < Discogs::Release::Track; end
@@ -12,22 +12,26 @@ class Discogs::Wrapper
12
12
 
13
13
  @@root_host = "http://api.discogs.com"
14
14
 
15
- attr_reader :user_agent
15
+ attr_reader :app_name
16
16
 
17
- def initialize(user_agent=nil)
18
- @user_agent = user_agent
17
+ def initialize(app_name=nil)
18
+ @app_name = app_name
19
19
  end
20
20
 
21
21
  def get_release(id)
22
22
  query_and_build "release/#{id}", Discogs::Release
23
23
  end
24
24
 
25
+ def get_master_release(id)
26
+ query_and_build "master/#{id}", Discogs::MasterRelease
27
+ end
28
+
25
29
  def get_artist(name)
26
- query_and_build "artist/#{name}", Discogs::Artist
30
+ query_and_build "artist/#{name}", Discogs::Artist, {:releases => 1}
27
31
  end
28
32
 
29
33
  def get_label(name)
30
- query_and_build "label/#{name}", Discogs::Label
34
+ query_and_build "label/#{name}", Discogs::Label, {:releases => 1}
31
35
  end
32
36
 
33
37
  def search(term, options={})
@@ -42,8 +46,8 @@ class Discogs::Wrapper
42
46
 
43
47
  private
44
48
 
45
- def query_and_build(path, klass)
46
- data = query_api(path)
49
+ def query_and_build(path, klass, params={})
50
+ data = query_api(path, params)
47
51
  resource = klass.send(:new, data)
48
52
  resource.build!
49
53
  end
@@ -73,8 +77,9 @@ class Discogs::Wrapper
73
77
  uri = build_uri(path, params)
74
78
 
75
79
  request = Net::HTTP::Get.new(uri.path + "?" + uri.query)
80
+ request.add_field("Accept", "application/xml")
76
81
  request.add_field("Accept-Encoding", "gzip,deflate")
77
- request.add_field("User-Agent", @user_agent)
82
+ request.add_field("User-Agent", @app_name)
78
83
 
79
84
  Net::HTTP.new(uri.host).start do |http|
80
85
  http.request(request)
@@ -26,8 +26,8 @@ describe Discogs::Artist::Release do
26
26
  @artist_release.status.should == "Accepted"
27
27
  end
28
28
 
29
- it "should have a type attribute" do
30
- @artist_release.type.should == "Main"
29
+ it "should have a role attribute" do
30
+ @artist_release.role.should == "Main"
31
31
  end
32
32
 
33
33
  it "should have a title attribute" do
data/spec/spec_helper.rb CHANGED
@@ -6,6 +6,10 @@ def valid_release_xml
6
6
  read_sample "valid_release.xml"
7
7
  end
8
8
 
9
+ def valid_master_release_xml
10
+ read_sample "valid_master_release.xml"
11
+ end
12
+
9
13
  def valid_artist_xml
10
14
  read_sample "valid_artist.xml"
11
15
  end
@@ -94,6 +94,22 @@ describe Discogs::Wrapper do
94
94
  @artist.releases[0].id.should == "1805661"
95
95
  end
96
96
 
97
+ it "should have a type for the first release" do
98
+ @artist.releases[0].release_type.should == "release"
99
+ end
100
+
101
+ it "should have a type for the second release" do
102
+ @artist.releases[1].release_type.should == "master"
103
+ end
104
+
105
+ it "should have a main release for the second release" do
106
+ @artist.releases[1].main_release.should == "12345"
107
+ end
108
+
109
+ it "should have a thumb for the second release" do
110
+ @artist.releases[1].thumb.should == "http://images"
111
+ end
112
+
97
113
  it "should have a year for the first release" do
98
114
  @artist.releases[0].year.should == "1991"
99
115
  end
@@ -30,6 +30,10 @@ describe Discogs::Wrapper do
30
30
  @release.id.should == "666666"
31
31
  end
32
32
 
33
+ it "should have a master_id attribute" do
34
+ @release.master_id.should == "6111"
35
+ end
36
+
33
37
  it "should have one or more extra artists" do
34
38
  @release.extraartists.should be_instance_of(Array)
35
39
  @release.extraartists[0].should be_instance_of(Discogs::Release::Artist)
@@ -55,6 +59,12 @@ describe Discogs::Wrapper do
55
59
  @release.images[0].should be_instance_of(Discogs::Image)
56
60
  end
57
61
 
62
+ it "should have one or more videos" do
63
+ @release.videos.should be_instance_of(Array)
64
+ @release.videos[0].should be_instance_of(Discogs::Video)
65
+ end
66
+
67
+
58
68
  end
59
69
 
60
70
  describe "when calling complex release attributes" do
@@ -72,6 +82,15 @@ describe Discogs::Wrapper do
72
82
  end
73
83
  end
74
84
 
85
+ it "should have specifications for each video" do
86
+ specs = [ [ '334', 'true', 'http://www.youtube.com/watch?v=QVdDhOnoR8k' ], [ '350', 'true', 'http://www.youtube.com/watch?v=QVdDhOnoR7k' ] ]
87
+ @release.videos.each_with_index do |video, index|
88
+ video.duration.should == specs[index][0]
89
+ video.embed.should == specs[index][1]
90
+ video.uri.should == specs[index][2]
91
+ end
92
+ end
93
+
75
94
  it "should have a traversible list of styles" do
76
95
  @release.styles.should be_instance_of(Array)
77
96
  @release.styles[0].should == "Black Metal"
data/spec/wrapper_spec.rb CHANGED
@@ -23,8 +23,8 @@ describe Discogs::Wrapper do
23
23
  end
24
24
 
25
25
  before do
26
- @user_agent = "some_app"
27
- @wrapper = Discogs::Wrapper.new(@user_agent)
26
+ @app_name = "some_app"
27
+ @wrapper = Discogs::Wrapper.new(@app_name)
28
28
  @release_id = "666666"
29
29
  @artist_name = "Dark"
30
30
  @label_name = "Monitor"
@@ -32,7 +32,7 @@ describe Discogs::Wrapper do
32
32
  end
33
33
 
34
34
  it "should have an user agent" do
35
- @wrapper.user_agent.should == @user_agent
35
+ @wrapper.app_name.should == @app_name
36
36
  end
37
37
 
38
38
  describe "requested URIs" do
@@ -49,14 +49,14 @@ describe Discogs::Wrapper do
49
49
 
50
50
  it "should generate the correct artist URL to parse" do
51
51
  mock_http_with_response "200", valid_artist_xml
52
- URI.should_receive(:parse).with("http://api.discogs.com/artist/Dark?f=xml").and_return(@uri)
52
+ URI.should_receive(:parse).with("http://api.discogs.com/artist/Dark?f=xml&releases=1").and_return(@uri)
53
53
 
54
54
  @wrapper.get_artist(@artist_name)
55
55
  end
56
56
 
57
57
  it "should generate the correct label URL to parse" do
58
58
  mock_http_with_response "200", valid_label_xml
59
- URI.should_receive(:parse).with("http://api.discogs.com/label/Monitor?f=xml").and_return(@uri)
59
+ URI.should_receive(:parse).with("http://api.discogs.com/label/Monitor?f=xml&releases=1").and_return(@uri)
60
60
 
61
61
  @wrapper.get_label(@label_name)
62
62
  end
@@ -84,7 +84,7 @@ describe Discogs::Wrapper do
84
84
 
85
85
  it "should sanitize the path correctly" do
86
86
  mock_http_with_response "200", valid_artist_xml
87
- URI.should_receive(:parse).with("http://api.discogs.com/artist/A+very+long+band+name?f=xml").and_return(@uri)
87
+ URI.should_receive(:parse).with("http://api.discogs.com/artist/A+very+long+band+name?f=xml&releases=1").and_return(@uri)
88
88
 
89
89
  @wrapper.get_artist("A very long band name")
90
90
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: discogs-wrapper
3
3
  version: !ruby/object:Gem::Version
4
- hash: 23
4
+ hash: 19
5
5
  prerelease: false
6
6
  segments:
7
7
  - 1
8
+ - 1
8
9
  - 0
9
- - 0
10
- version: 1.0.0
10
+ version: 1.1.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Andrew Buntine
@@ -16,7 +16,7 @@ autorequire:
16
16
  bindir: bin
17
17
  cert_chain: []
18
18
 
19
- date: 2011-09-27 00:00:00 +10:00
19
+ date: 2011-10-22 00:00:00 +11:00
20
20
  default_executable:
21
21
  dependencies: []
22
22