scrobbler2 0.0.4

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 (117) hide show
  1. data/.gitignore +1 -0
  2. data/README.rdoc +155 -0
  3. data/Rakefile +8 -0
  4. data/TODO +23 -0
  5. data/VERSION.yml +4 -0
  6. data/generators/acceptance_test/USAGE +9 -0
  7. data/generators/acceptance_test/acceptance_test_generator.rb +57 -0
  8. data/generators/acceptance_test/templates/acceptance_test.rb.erb +14 -0
  9. data/generators/resource/USAGE +8 -0
  10. data/generators/resource/resource_generator.rb +47 -0
  11. data/generators/resource/templates/resource.rb.erb +11 -0
  12. data/generators/resource/templates/spec.rb.erb +22 -0
  13. data/lib/scrobbler2.rb +23 -0
  14. data/lib/scrobbler2/album.rb +11 -0
  15. data/lib/scrobbler2/artist.rb +26 -0
  16. data/lib/scrobbler2/auth.rb +19 -0
  17. data/lib/scrobbler2/base.rb +100 -0
  18. data/lib/scrobbler2/event.rb +12 -0
  19. data/lib/scrobbler2/geo.rb +15 -0
  20. data/lib/scrobbler2/group.rb +14 -0
  21. data/lib/scrobbler2/library.rb +12 -0
  22. data/lib/scrobbler2/playlist.rb +10 -0
  23. data/lib/scrobbler2/tag.rb +18 -0
  24. data/lib/scrobbler2/track.rb +13 -0
  25. data/lib/scrobbler2/user.rb +29 -0
  26. data/lib/scrobbler2/venue.rb +12 -0
  27. data/script/console +10 -0
  28. data/script/destroy +14 -0
  29. data/script/generate +14 -0
  30. data/scrobbler2.gemspec +243 -0
  31. data/tasks/jeweler.rake +15 -0
  32. data/tasks/rcov.rake +9 -0
  33. data/tasks/rdoc.rake +7 -0
  34. data/tasks/tests.rake +10 -0
  35. data/test/acceptance/album/info_test.rb +56 -0
  36. data/test/acceptance/artist/add_tags_test.rb +14 -0
  37. data/test/acceptance/artist/events_test.rb +47 -0
  38. data/test/acceptance/artist/info_test.rb +53 -0
  39. data/test/acceptance/artist/shouts_test.rb +48 -0
  40. data/test/acceptance/artist/similar_artists_test.rb +20 -0
  41. data/test/acceptance/artist/tags_test.rb +24 -0
  42. data/test/acceptance/artist/top_albums_test.rb +34 -0
  43. data/test/acceptance/artist/top_fans.rb +19 -0
  44. data/test/acceptance/artist/top_tags.rb +18 -0
  45. data/test/acceptance/artist/top_tracks.rb +47 -0
  46. data/test/acceptance/auth/auth_test.rb +21 -0
  47. data/test/acceptance/event/info_test.rb +66 -0
  48. data/test/acceptance/event/shouts_test.rb +49 -0
  49. data/test/acceptance/geo/events_test.rb +14 -0
  50. data/test/acceptance/geo/top_artists_test.rb +16 -0
  51. data/test/acceptance/geo/top_tracks_test.rb +16 -0
  52. data/test/acceptance/group/members_test.rb +60 -0
  53. data/test/acceptance/group/weekly_album_chart_test.rb +61 -0
  54. data/test/acceptance/group/weekly_artist_chart_test.rb +54 -0
  55. data/test/acceptance/group/weekly_chart_list_test.rb +39 -0
  56. data/test/acceptance/group/weekly_track_chart_test.rb +61 -0
  57. data/test/acceptance/library/albums_test.rb +34 -0
  58. data/test/acceptance/library/artists_test.rb +33 -0
  59. data/test/acceptance/library/tracks_test.rb +33 -0
  60. data/test/acceptance/playlist/fetch_test.rb +68 -0
  61. data/test/acceptance/shared/album.rb +41 -0
  62. data/test/acceptance/shared/artist.rb +16 -0
  63. data/test/acceptance/shared/paged_result.rb +16 -0
  64. data/test/acceptance/shared/top_fans.rb +29 -0
  65. data/test/acceptance/shared/top_tags.rb +23 -0
  66. data/test/acceptance/shared/track.rb +23 -0
  67. data/test/acceptance/tag/similar_test.rb +15 -0
  68. data/test/acceptance/tag/top_albums_test.rb +37 -0
  69. data/test/acceptance/tag/top_artists_test.rb +31 -0
  70. data/test/acceptance/tag/top_tags_test.rb +32 -0
  71. data/test/acceptance/tag/top_tracks_test.rb +33 -0
  72. data/test/acceptance/tag/weekly_artist_chart.rb +54 -0
  73. data/test/acceptance/tag/weekly_chart_list.rb +39 -0
  74. data/test/acceptance/test_helper.rb +15 -0
  75. data/test/acceptance/track/info_test.rb +47 -0
  76. data/test/acceptance/track/similar_test.rb +22 -0
  77. data/test/acceptance/track/top_fans_test.rb +23 -0
  78. data/test/acceptance/track/top_tags_test.rb +24 -0
  79. data/test/acceptance/user/events_test.rb +27 -0
  80. data/test/acceptance/user/friends_test.rb +14 -0
  81. data/test/acceptance/user/info_test.rb +19 -0
  82. data/test/acceptance/user/loved_tracks_test.rb +14 -0
  83. data/test/acceptance/user/neighbours_test.rb +14 -0
  84. data/test/acceptance/user/past_events_test.rb +14 -0
  85. data/test/acceptance/user/playlists_test.rb +14 -0
  86. data/test/acceptance/user/recent_tracks_test.rb +14 -0
  87. data/test/acceptance/user/recommended_artists_test.rb +16 -0
  88. data/test/acceptance/user/recommended_events_test.rb +16 -0
  89. data/test/acceptance/user/shouts_test.rb +14 -0
  90. data/test/acceptance/user/top_albums_test.rb +14 -0
  91. data/test/acceptance/user/top_artists_test.rb +14 -0
  92. data/test/acceptance/user/top_tags_test.rb +14 -0
  93. data/test/acceptance/user/top_tracks_test.rb +14 -0
  94. data/test/acceptance/user/weekly_album_chart_test.rb +14 -0
  95. data/test/acceptance/user/weekly_artist_chart_test.rb +14 -0
  96. data/test/acceptance/user/weekly_chart_list_test.rb +14 -0
  97. data/test/acceptance/user/weekly_track_chart_test.rb +14 -0
  98. data/test/acceptance/venue/events_test.rb +14 -0
  99. data/test/acceptance/venue/past_events_test.rb +14 -0
  100. data/test/generators/test_acceptance_test_generator.rb +46 -0
  101. data/test/generators/test_generator_helper.rb +29 -0
  102. data/test/generators/test_resource_generator.rb +71 -0
  103. data/test/unit/album_spec.rb +17 -0
  104. data/test/unit/artist_spec.rb +62 -0
  105. data/test/unit/auth_spec.rb +22 -0
  106. data/test/unit/base_spec.rb +193 -0
  107. data/test/unit/event_spec.rb +23 -0
  108. data/test/unit/geo_spec.rb +36 -0
  109. data/test/unit/group_spec.rb +38 -0
  110. data/test/unit/library_spec.rb +27 -0
  111. data/test/unit/playlist_spec.rb +18 -0
  112. data/test/unit/spec_helper.rb +7 -0
  113. data/test/unit/tag_spec.rb +50 -0
  114. data/test/unit/track_spec.rb +33 -0
  115. data/test/unit/user_spec.rb +148 -0
  116. data/test/unit/venue_spec.rb +29 -0
  117. metadata +272 -0
@@ -0,0 +1,14 @@
1
+ require File.dirname(__FILE__) + '/../test_helper.rb'
2
+
3
+ describe "shouts for user gingerhendrix" do
4
+
5
+ before(:all) do #We only want to hit the webservice once.
6
+ @user = Scrobbler2::User.new "gingerhendrix"
7
+ @shouts = @user.shouts
8
+ end
9
+
10
+ it "should be hash" do
11
+ @shouts.should be_kind_of Hash
12
+ end
13
+
14
+ end
@@ -0,0 +1,14 @@
1
+ require File.dirname(__FILE__) + '/../test_helper.rb'
2
+
3
+ describe "top_albums for user gingerhendrix" do
4
+
5
+ before(:all) do #We only want to hit the webservice once.
6
+ @user = Scrobbler2::User.new "gingerhendrix"
7
+ @top_albums = @user.top_albums
8
+ end
9
+
10
+ it "should be hash" do
11
+ @top_albums.should be_kind_of Hash
12
+ end
13
+
14
+ end
@@ -0,0 +1,14 @@
1
+ require File.dirname(__FILE__) + '/../test_helper.rb'
2
+
3
+ describe "top_artists for user gingerhendrix" do
4
+
5
+ before(:all) do #We only want to hit the webservice once.
6
+ @user = Scrobbler2::User.new "gingerhendrix"
7
+ @top_artists = @user.top_artists
8
+ end
9
+
10
+ it "should be hash" do
11
+ @top_artists.should be_kind_of Hash
12
+ end
13
+
14
+ end
@@ -0,0 +1,14 @@
1
+ require File.dirname(__FILE__) + '/../test_helper.rb'
2
+
3
+ describe "top_tags for user gingerhendrix" do
4
+
5
+ before(:all) do #We only want to hit the webservice once.
6
+ @user = Scrobbler2::User.new "gingerhendrix"
7
+ @top_tags = @user.top_tags
8
+ end
9
+
10
+ it "should be hash" do
11
+ @top_tags.should be_kind_of Hash
12
+ end
13
+
14
+ end
@@ -0,0 +1,14 @@
1
+ require File.dirname(__FILE__) + '/../test_helper.rb'
2
+
3
+ describe "top_tracks for user gingerhendrix" do
4
+
5
+ before(:all) do #We only want to hit the webservice once.
6
+ @user = Scrobbler2::User.new "gingerhendrix"
7
+ @top_tracks = @user.top_tracks
8
+ end
9
+
10
+ it "should be hash" do
11
+ @top_tracks.should be_kind_of Hash
12
+ end
13
+
14
+ end
@@ -0,0 +1,14 @@
1
+ require File.dirname(__FILE__) + '/../test_helper.rb'
2
+
3
+ describe "weekly_album_chart for user gingerhendrix" do
4
+
5
+ before(:all) do #We only want to hit the webservice once.
6
+ @user = Scrobbler2::User.new "gingerhendrix"
7
+ @weekly_album_chart = @user.weekly_album_chart
8
+ end
9
+
10
+ it "should be hash" do
11
+ @weekly_album_chart.should be_kind_of Hash
12
+ end
13
+
14
+ end
@@ -0,0 +1,14 @@
1
+ require File.dirname(__FILE__) + '/../test_helper.rb'
2
+
3
+ describe "weekly_artist_chart for user gingerhendrix" do
4
+
5
+ before(:all) do #We only want to hit the webservice once.
6
+ @user = Scrobbler2::User.new "gingerhendrix"
7
+ @weekly_artist_chart = @user.weekly_artist_chart
8
+ end
9
+
10
+ it "should be hash" do
11
+ @weekly_artist_chart.should be_kind_of Hash
12
+ end
13
+
14
+ end
@@ -0,0 +1,14 @@
1
+ require File.dirname(__FILE__) + '/../test_helper.rb'
2
+
3
+ describe "weekly_chart_list for user gingerhendrix" do
4
+
5
+ before(:all) do #We only want to hit the webservice once.
6
+ @user = Scrobbler2::User.new "gingerhendrix"
7
+ @weekly_chart_list = @user.weekly_chart_list
8
+ end
9
+
10
+ it "should be hash" do
11
+ @weekly_chart_list.should be_kind_of Hash
12
+ end
13
+
14
+ end
@@ -0,0 +1,14 @@
1
+ require File.dirname(__FILE__) + '/../test_helper.rb'
2
+
3
+ describe "weekly_track_chart for user gingerhendrix" do
4
+
5
+ before(:all) do #We only want to hit the webservice once.
6
+ @user = Scrobbler2::User.new "gingerhendrix"
7
+ @weekly_track_chart = @user.weekly_track_chart
8
+ end
9
+
10
+ it "should be hash" do
11
+ @weekly_track_chart.should be_kind_of Hash
12
+ end
13
+
14
+ end
@@ -0,0 +1,14 @@
1
+ require File.dirname(__FILE__) + '/../test_helper.rb'
2
+
3
+ describe "events for venue 8929268 (The Menagerie)" do
4
+
5
+ before(:all) do #We only want to hit the webservice once.
6
+ @venue = Scrobbler2::Venue.new "8929268"
7
+ @events = @venue.events
8
+ end
9
+
10
+ it "should be hash" do
11
+ @events.should be_kind_of Hash
12
+ end
13
+
14
+ end
@@ -0,0 +1,14 @@
1
+ require File.dirname(__FILE__) + '/../test_helper.rb'
2
+
3
+ describe "past_events for venue 8929268 (The Menagerie)" do
4
+
5
+ before(:all) do #We only want to hit the webservice once.
6
+ @venue = Scrobbler2::Venue.new "8929268"
7
+ @past_events = @venue.past_events
8
+ end
9
+
10
+ it "should be hash" do
11
+ @past_events.should be_kind_of Hash
12
+ end
13
+
14
+ end
@@ -0,0 +1,46 @@
1
+ require File.join(File.dirname(__FILE__), "test_generator_helper.rb")
2
+
3
+
4
+ class TestAcceptanceTestGenerator < Test::Unit::TestCase
5
+ include RubiGen::GeneratorTestHelper
6
+
7
+ def setup
8
+ bare_setup
9
+ end
10
+
11
+ def teardown
12
+ bare_teardown
13
+ end
14
+
15
+ # Some generator-related assertions:
16
+ # assert_generated_file(name, &block) # block passed the file contents
17
+ # assert_directory_exists(name)
18
+ # assert_generated_class(name, &block)
19
+ # assert_generated_module(name, &block)
20
+ # assert_generated_test_for(name, &block)
21
+ # The assert_generated_(class|module|test_for) &block is passed the body of the class/module within the file
22
+ # assert_has_method(body, *methods) # check that the body has a list of methods (methods with parentheses not supported yet)
23
+ #
24
+ # Other helper methods are:
25
+ # app_root_files - put this in teardown to show files generated by the test method (e.g. p app_root_files)
26
+ # bare_setup - place this in setup method to create the APP_ROOT folder for each test
27
+ # bare_teardown - place this in teardown method to destroy the TMP_ROOT or APP_ROOT folder after each test
28
+
29
+ def test_generator_without_options
30
+ resource = "resource"
31
+ method = "method"
32
+ run_generator('acceptance_test', [resource, method], sources)
33
+ assert_directory_exists "test/acceptance/#{resource}"
34
+ assert_generated_file "test/acceptance/#{resource}/#{method}_test.rb"
35
+ end
36
+
37
+ private
38
+ def sources
39
+ [RubiGen::PathSource.new(:test, File.join(File.dirname(__FILE__),"../..", generator_path))
40
+ ]
41
+ end
42
+
43
+ def generator_path
44
+ "generators"
45
+ end
46
+ end
@@ -0,0 +1,29 @@
1
+ begin
2
+ require File.dirname(__FILE__) + '/test_helper'
3
+ rescue LoadError
4
+ require 'test/unit'
5
+ end
6
+ require 'fileutils'
7
+
8
+ # Must set before requiring generator libs.
9
+ TMP_ROOT = File.dirname(__FILE__) + "/tmp" unless defined?(TMP_ROOT)
10
+ PROJECT_NAME = "myproject" unless defined?(PROJECT_NAME)
11
+ app_root = File.join(TMP_ROOT, PROJECT_NAME)
12
+ if defined?(APP_ROOT)
13
+ APP_ROOT.replace(app_root)
14
+ else
15
+ APP_ROOT = app_root
16
+ end
17
+ if defined?(RAILS_ROOT)
18
+ RAILS_ROOT.replace(app_root)
19
+ else
20
+ RAILS_ROOT = app_root
21
+ end
22
+
23
+ begin
24
+ require 'rubigen'
25
+ rescue LoadError
26
+ require 'rubygems'
27
+ require 'rubigen'
28
+ end
29
+ require 'rubigen/helpers/generator_test_helper'
@@ -0,0 +1,71 @@
1
+ require File.join(File.dirname(__FILE__), "test_generator_helper.rb")
2
+
3
+
4
+ class TestResourceGenerator < Test::Unit::TestCase
5
+ include RubiGen::GeneratorTestHelper
6
+
7
+ def setup
8
+ bare_setup
9
+ end
10
+
11
+ def teardown
12
+ bare_teardown
13
+ end
14
+
15
+ # Some generator-related assertions:
16
+ # assert_generated_file(name, &block) # block passed the file contents
17
+ # assert_directory_exists(name)
18
+ # assert_generated_class(name, &block)
19
+ # assert_generated_module(name, &block)
20
+ # assert_generated_test_for(name, &block)
21
+ # The assert_generated_(class|module|test_for) &block is passed the body of the class/module within the file
22
+ # assert_has_method(body, *methods) # check that the body has a list of methods (methods with parentheses not supported yet)
23
+ #
24
+ # Other helper methods are:
25
+ # app_root_files - put this in teardown to show files generated by the test method (e.g. p app_root_files)
26
+ # bare_setup - place this in setup method to create the APP_ROOT folder for each test
27
+ # bare_teardown - place this in teardown method to destroy the TMP_ROOT or APP_ROOT folder after each test
28
+
29
+ def test_generator_without_options
30
+ resource = "resource"
31
+ funcs = ["method1", "method2"]
32
+ run_generator('resource', funcs.unshift(resource), sources)
33
+ assert_generated_file "lib/scrobbler2/resource.rb" do |file|
34
+ assert file.include?("class Resource < Base")
35
+ assert file.include?("has_resource :method1, :root => \"method1\"")
36
+ assert file.include?("has_resource :method2, :root => \"method2\"")
37
+ end
38
+
39
+ assert_generated_file "test/unit/resource_spec.rb" do |file|
40
+ assert file.include?("describe \"Resource\"")
41
+
42
+ assert file.include?("@resource = Scrobbler2::Resource.new \"...\"")
43
+ assert file.include?("@resource.class.stub!(:get).and_return Hash.new")
44
+
45
+ assert file.include?("it \"should have a default query {TODO}\" do")
46
+ assert file.include?("@resource.instance_variable_get(:@query).should == {}")
47
+
48
+ assert file.include?("it \"method1 should call get with 'resource.getmethod1'\" do")
49
+ assert file.include?("@resource.class.should_receive(:get).with('resource.getmethod1', anything(), anything()).and_return Hash.new")
50
+ assert file.include?("@resource.method1")
51
+
52
+ assert file.include?("it \"method2 should call get with 'resource.getmethod2'\" do")
53
+ assert file.include?("@resource.class.should_receive(:get).with('resource.getmethod2', anything(), anything()).and_return Hash.new")
54
+ assert file.include?("@resource.method2")
55
+ end
56
+
57
+ assert_directory_exists("test/acceptance/resource")
58
+ assert_generated_file "test/acceptance/resource/method1_test.rb"
59
+ assert_generated_file "test/acceptance/resource/method2_test.rb"
60
+ end
61
+
62
+ private
63
+ def sources
64
+ [RubiGen::PathSource.new(:test, File.join(File.dirname(__FILE__),"../..", generator_path))
65
+ ]
66
+ end
67
+
68
+ def generator_path
69
+ "generators"
70
+ end
71
+ end
@@ -0,0 +1,17 @@
1
+ require File.dirname(__FILE__) + "/spec_helper.rb"
2
+
3
+ describe "Album" do
4
+ before(:each) do
5
+ @album = Scrobbler2::Album.new "Metallica", "Ride The Lightning"
6
+ @album.class.stub!(:get).and_return Hash.new
7
+ end
8
+
9
+ it "should have a default query {:artist => 'Metallica', :album => 'Ride The Lightning'}" do
10
+ @album.instance_variable_get(:@query).should == {:artist => 'Metallica', :album => 'Ride The Lightning'}
11
+ end
12
+
13
+ it "info should call get with 'album.getInfo' with query params :artist => @artist, :album => @album" do
14
+ @album.class.should_receive(:get).with('album.getinfo', {:artist => "Metallica", :album => "Ride The Lightning"}, {}).and_return Hash.new
15
+ @album.info
16
+ end
17
+ end
@@ -0,0 +1,62 @@
1
+ require File.dirname(__FILE__) + "/spec_helper.rb"
2
+
3
+ describe "Artist" do
4
+ before(:each) do
5
+ @artist = Scrobbler2::Artist.new "Metallica"
6
+ @artist.class.stub!(:get).and_return Hash.new
7
+ end
8
+
9
+ it "should have a default query {:artist => 'Metallica'}" do
10
+ @artist.instance_variable_get(:@query).should == {:artist => 'Metallica'}
11
+ end
12
+
13
+ it "info should call get with 'artist.getInfo' with query params :artist => @name" do
14
+ @artist.class.should_receive(:get).with('artist.getinfo', {:artist => "Metallica"}, {}).and_return Hash.new
15
+ @artist.info
16
+ end
17
+
18
+ it "similar should call get with 'artist.getSimilar' with query params :artist => @name" do
19
+ @artist.class.should_receive(:get).with('artist.getsimilar', {:artist => "Metallica"}, {}).and_return Hash.new
20
+ @artist.similar
21
+ end
22
+
23
+ it "top_albums should call get with 'artist.getTopAlbums' with query params :artist => @name" do
24
+ @artist.class.should_receive(:get).with('artist.gettopalbums', {:artist => "Metallica"}, {}).and_return Hash.new
25
+ @artist.top_albums
26
+ end
27
+
28
+ it "events should call get with 'artist.getEvents' with query params :artist => @name" do
29
+ @artist.class.should_receive(:get).with('artist.getevents', {:artist => "Metallica"}, {}).and_return Hash.new
30
+ @artist.events
31
+ end
32
+
33
+ it "shouts should call get with 'artist.getShouts' with query params :artist => @name" do
34
+ @artist.class.should_receive(:get).with('artist.getshouts', {:artist => "Metallica"}, {}).and_return Hash.new
35
+ @artist.shouts
36
+ end
37
+
38
+ it "top_fans should call get with 'artist.getTopFans' with query params :artist => @name" do
39
+ @artist.class.should_receive(:get).with('artist.gettopfans', {:artist => "Metallica"}, {}).and_return Hash.new
40
+ @artist.top_fans
41
+ end
42
+
43
+ it "top_tags should call get with 'artist.getTopTags' with query params :artist => @name" do
44
+ @artist.class.should_receive(:get).with('artist.gettoptags', {:artist => "Metallica"}, {}).and_return Hash.new
45
+ @artist.top_tags
46
+ end
47
+
48
+ it "top_tracks should call get with 'artist.getTopTrakcs' with query params :artist => @name" do
49
+ @artist.class.should_receive(:get).with('artist.gettoptracks', {:artist => "Metallica"}, {}).and_return Hash.new
50
+ @artist.top_tracks
51
+ end
52
+
53
+ it "tags should call get_with_auth with 'artist.getTags', and query params :artist => @name" do
54
+ @artist.class.should_receive(:get_with_auth).with('artist.gettags', {:artist => "Metallica"}).and_return Hash.new :tags => []
55
+ @artist.tags
56
+ end
57
+
58
+ it "add tags should post_with_auth with 'artist.addTags', and query params :artist => @name, :tags => 'test1, test2'" do
59
+ @artist.class.should_receive(:post_with_auth).with('artist.addtags', {:artist => "Metallica", :tags => "test1,test2"}).and_return Hash.new
60
+ @artist.add_tags ["test1", "test2"]
61
+ end
62
+ end
@@ -0,0 +1,22 @@
1
+ require File.dirname(__FILE__) + "/spec_helper.rb"
2
+
3
+
4
+ describe "Auth" do
5
+ before(:each) do
6
+ @auth = Scrobbler2::Auth.new
7
+ @artist.class.stub!(:get_with_auth).and_return Hash.new
8
+ end
9
+
10
+ it "token should call get_with_auth with auth.gettoken" do
11
+ @auth.class.should_receive(:get_with_auth).with('auth.gettoken').and_return Hash.new :lfm => {}
12
+ @auth.token
13
+ end
14
+
15
+ it "session should call get_with_auth with auth.getsession and token=token" do
16
+ @auth.should_receive(:token).and_return :test_token
17
+ @auth.class.should_receive(:get_with_auth).with('auth.getsession', {:token => :test_token}).and_return Hash.new :lfm => {}
18
+ @auth.session
19
+ end
20
+
21
+
22
+ end
@@ -0,0 +1,193 @@
1
+ require File.dirname(__FILE__) + "/spec_helper.rb"
2
+
3
+ class TestBaseImplementation < Scrobbler2::Base
4
+ end
5
+
6
+ describe "Base" do
7
+
8
+ before(:each) do
9
+ HTTParty.stub!(:get)
10
+ Scrobbler2::Base.api_key = "api_key"
11
+ Scrobbler2::Base.api_secret = "api_secret"
12
+ end
13
+
14
+ it "should have attribute api key" do
15
+ Scrobbler2::Base.api_key = "blah"
16
+ Scrobbler2::Base.api_key.should == "blah"
17
+ end
18
+
19
+ it "should have attribute session_key" do
20
+ Scrobbler2::Base.session_key = "blah"
21
+ Scrobbler2::Base.session_key.should == "blah"
22
+ end
23
+
24
+ it "should have attribute api secret" do
25
+ Scrobbler2::Base.api_secret = "blah"
26
+ Scrobbler2::Base.api_secret.should == "blah"
27
+ end
28
+
29
+ describe "has_resource" do
30
+
31
+ before(:each) do
32
+ TestBaseImplementation.module_eval("has_resource :test_info, :root => :test");
33
+ TestBaseImplementation.stub!(:get).and_return({})
34
+ @test = TestBaseImplementation.new
35
+ end
36
+
37
+ it "should create a named method" do
38
+ @test.method(:test_info).should_not be_nil
39
+ end
40
+
41
+ describe "the method" do
42
+ it "the method should call self.get" do
43
+ TestBaseImplementation.should_receive(:get)
44
+ @test.test_info
45
+ end
46
+
47
+ it "should call get with resource name got from the class name and the method name" do
48
+ TestBaseImplementation.should_receive(:get).with("testbaseimplementation.gettestinfo", an_instance_of(Hash), an_instance_of(Hash))
49
+ @test.test_info
50
+ end
51
+
52
+ it "should get with query options from @query" do
53
+ @test.instance_variable_set(:@query, {:name => "test"})
54
+ TestBaseImplementation.should_receive(:get).with("testbaseimplementation.gettestinfo", {:name => "test"}, an_instance_of(Hash))
55
+ @test.test_info
56
+ end
57
+
58
+ it "should return the root element" do
59
+ TestBaseImplementation.should_receive(:get).and_return({:test=>:root_element});
60
+ @test.test_info.should == :root_element
61
+ end
62
+ end
63
+
64
+ end
65
+
66
+ describe "has_resource with options = {:resource_name => resource.method}" do
67
+ before(:each) do
68
+ TestBaseImplementation.module_eval("has_resource :test_info, :root => :test, :resource_name => 'resource.method'");
69
+ TestBaseImplementation.stub!(:get).and_return({})
70
+ @test = TestBaseImplementation.new
71
+ end
72
+
73
+ it "should call get with resource name got from options" do
74
+ TestBaseImplementation.should_receive(:get).with("resource.method", an_instance_of(Hash), an_instance_of(Hash))
75
+ @test.test_info
76
+ end
77
+ end
78
+
79
+ describe "sign" do
80
+ it "should return an md5 digest of the 'query_signature'" do
81
+ Scrobbler2::Base.should_receive(:query_signature).with(:query).and_return('query_signature')
82
+ Scrobbler2::Base.sign(:query).should == '4987b19cfbded59cd717b8ffd9f1a6bf'
83
+ end
84
+ end
85
+
86
+ describe "query_signature" do
87
+ it "should return an unencoded api_signature" do
88
+ query = {:method => "artist.getInfo", :api_key => "api_key", :format => "json"}
89
+ Scrobbler2::Base.query_signature(query).should == "api_keyapi_keyformatjsonmethodartist.getInfoapi_secret"
90
+ end
91
+
92
+ end
93
+
94
+ describe "get" do
95
+ it "should call HTTParty.get" do
96
+ HTTParty.should_receive(:get).with('http://ws.audioscrobbler.com/2.0/', {:query => {:method=>'artist.getInfo', :format=>'json', :api_key => 'api_key'}})
97
+ Scrobbler2::Base.get('artist.getInfo', {})
98
+ end
99
+ end
100
+
101
+
102
+ describe "auth_request", :shared => true do
103
+
104
+ it "should set the query method" do
105
+ HTTParty.should_receive(http_method) do |url, options|
106
+ options[options_param][:method].should == 'artist.getInfo'
107
+ end
108
+ action
109
+ end
110
+
111
+ it "should set the api_key" do
112
+ HTTParty.should_receive(http_method) do |url, options|
113
+ options[options_param][:api_key].should == 'api_key'
114
+ end
115
+ action
116
+ end
117
+
118
+ it "should set the api_sginature" do
119
+ Scrobbler2::Base.should_receive(:sign).with(an_instance_of(Hash)).and_return('signature')
120
+ HTTParty.should_receive(http_method) do |url, options|
121
+ options[options_param][:api_sig].should == 'signature'
122
+ end
123
+ action
124
+ end
125
+
126
+ it "should set the session_key, if available" do
127
+ Scrobbler2::Base.should_receive(:session_key).at_least(1).and_return("SESSIONKEY");
128
+ HTTParty.should_receive(http_method) do |url, options|
129
+ options[options_param][:sk].should == 'SESSIONKEY'
130
+ end
131
+ action
132
+ end
133
+
134
+ it "should not set the session_key, if unavailable" do
135
+ Scrobbler2::Base.should_receive(:session_key).at_least(1).and_return(nil);
136
+ HTTParty.should_receive(http_method) do |url, options|
137
+ options[options_param].should_not have_key(:sk)
138
+ end
139
+ action
140
+ end
141
+
142
+ end
143
+
144
+
145
+ describe "get_with_auth" do
146
+ it "should call HTTParty.get" do
147
+ HTTParty.should_receive(:get).with('http://ws.audioscrobbler.com/2.0/', an_instance_of(Hash))
148
+ Scrobbler2::Base.get_with_auth('artist.getInfo', {})
149
+ end
150
+
151
+ def action
152
+ Scrobbler2::Base.get_with_auth('artist.getInfo', {})
153
+ end
154
+
155
+ def http_method
156
+ :get
157
+ end
158
+
159
+ def options_param
160
+ :query
161
+ end
162
+
163
+ it_should_behave_like "auth_request"
164
+ end
165
+
166
+
167
+ describe "post_with_auth" do
168
+ it "should call HTTParty.post" do
169
+ HTTParty.should_receive(:post).with('http://ws.audioscrobbler.com/2.0/', an_instance_of(Hash))
170
+ Scrobbler2::Base.post_with_auth('artist.getInfo', {})
171
+ end
172
+
173
+ def action
174
+ Scrobbler2::Base.post_with_auth('artist.getInfo', {})
175
+ end
176
+
177
+ def http_method
178
+ :post
179
+ end
180
+
181
+ def options_param
182
+ :body
183
+ end
184
+
185
+
186
+ it_should_behave_like "auth_request"
187
+
188
+
189
+ end
190
+
191
+ end
192
+
193
+