Permalink
Browse files

rename {get,has}_resource; introduce has_singleton_resource; use has_…

…singleton_resource for 'top tags'
  • Loading branch information...
1 parent 6c45027 commit d04400d0da87d5a73512d0c068d9a48b1c630a21 @bts bts committed with Sep 23, 2009
@@ -5,7 +5,7 @@ module Scrobbler2
@query = {}#TODO eg. {:artist => artist, :track => track}
end
<% funcs.each do |func| %>
- get_resource :<%=func%>, :root => "<%=func.camelize.downcase%>"
+ has_resource :<%=func%>, :root => "<%=func.camelize.downcase%>"
<% end %>
end
end
View
@@ -5,7 +5,7 @@ def initialize(artist, album)
@query = {:artist => artist, :album => album}
end
- get_resource :info, :root => 'album'
+ has_resource :info, :root => 'album'
end
end
View
@@ -6,16 +6,16 @@ def initialize(artist)
@query = {:artist => artist}
end
- get_resource :events, :root => 'events'
- get_resource :info, :root => 'artist'
- get_resource :shouts, :root => 'shouts'
- get_resource :similar, :root => 'similarartists'
- get_resource :top_albums, :root => 'topalbums'
- get_resource :top_fans, :root => 'topfans'
- get_resource :top_tags, :root => 'toptags'
- get_resource :top_tracks, :root => 'toptracks'
+ has_resource :events, :root => 'events'
+ has_resource :info, :root => 'artist'
+ has_resource :shouts, :root => 'shouts'
+ has_resource :similar, :root => 'similarartists'
+ has_resource :top_albums, :root => 'topalbums'
+ has_resource :top_fans, :root => 'topfans'
+ has_resource :top_tags, :root => 'toptags'
+ has_resource :top_tracks, :root => 'toptracks'
- get_resource :tags, :root=> "tags", :auth => true
+ has_resource :tags, :root=> "tags", :auth => true
def add_tags(tags)
query = @query.merge :tags => tags.join(",")
View
@@ -13,28 +13,55 @@ def self.get(method, query={}, options={})
response = HTTParty.get('http://ws.audioscrobbler.com/2.0/', options)
end
- def self.get_resource(name, options = {})
+ def self.has_resource(name, options = {})
define_method name do |*args|
query = args[1] || {}
local_options = args[0] || {}
local_value = instance_variable_get("@#{name.to_s}")
return local_value if local_value && !options[:force]
- query = query.merge options[:query] if options[:query]
- query = query.merge(@query) if @query && !options[:query]
-
+ if options[:query]
+ query.merge!(options[:query])
+ elsif @query
+ query.merge!(@query)
+ end
+
method_name = options[:resource_name] || self.class.name.split("::").last.downcase + ".get#{name.to_s.tr('_', '').downcase}"
if options[:auth]
value = self.class.get_with_auth(method_name, query, local_options)
else
- value = self.class.get(method_name, query, local_options)
+ value = self.class.get(method_name, query, local_options)
end
value = value[options[:root]] if options[:root]
instance_variable_set("@#{name}", value)
- end
+ end
end
-
+
+ # TODO this needs to be DRYed up, wrt has_resource
+ def self.has_singleton_resource(name, options={})
+ (class << self; self; end).class_eval do
+ define_method(name) do |*args|
+ query = args[1] || {}
+ local_options = args[0] || {}
+ local_value = instance_variable_get("@#{name.to_s}")
+ return local_value if local_value && !options[:force]
+
+ query.merge!(options[:query]) if options[:query]
+
+ method_name = options[:resource_name] || self.name.split("::").last.downcase + ".get#{name.to_s.tr('_', '').downcase}"
+ if options[:auth]
+ value = self.get_with_auth(method_name, query, local_options)
+ else
+ value = self.get(method_name, query, local_options)
+ end
+ value = value[options[:root]] if options[:root]
+
+ instance_variable_set("@#{name}", value)
+ end
+ end
+ end
+
#implements signed requests
def self.get_with_auth(method, query={}, options={})
http_with_auth(:get, method, query, options)["lfm"]
View
@@ -5,8 +5,8 @@ def initialize(event_id)
@query = {:event => event_id}
end
- get_resource :info, :root => 'event'
- get_resource :shouts, :root => 'shouts'
+ has_resource :info, :root => 'event'
+ has_resource :shouts, :root => 'shouts'
end
end
View
@@ -5,11 +5,11 @@ def initialize()
@query = {}#TODO eg. {:artist => artist, :track => track}
end
- get_resource :events, :root => "events"
+ has_resource :events, :root => "events"
- get_resource :top_artists, :root => "topartists"
+ has_resource :top_artists, :root => "topartists"
- get_resource :top_tracks, :root => "toptracks"
+ has_resource :top_tracks, :root => "toptracks"
end
end
View
@@ -5,10 +5,10 @@ def initialize(group_name)
@query = {:group => group_name}
end
- get_resource :members, :root => 'members'
- get_resource :weekly_album_chart, :root => 'weeklyalbumchart'
- get_resource :weekly_artist_chart, :root => 'weeklyartistchart'
- get_resource :weekly_track_chart, :root => 'weeklytrackchart'
- get_resource :weekly_chart_list, :root => 'weeklychartlist'
+ has_resource :members, :root => 'members'
+ has_resource :weekly_album_chart, :root => 'weeklyalbumchart'
+ has_resource :weekly_artist_chart, :root => 'weeklyartistchart'
+ has_resource :weekly_track_chart, :root => 'weeklytrackchart'
+ has_resource :weekly_chart_list, :root => 'weeklychartlist'
end
end
@@ -5,8 +5,8 @@ def initialize(user)
@query = {:user => user}
end
- get_resource :artists, :root => 'artists'
- get_resource :albums, :root => 'albums'
- get_resource :tracks, :root => 'tracks'
+ has_resource :artists, :root => 'artists'
+ has_resource :albums, :root => 'albums'
+ has_resource :tracks, :root => 'tracks'
end
end
@@ -5,6 +5,6 @@ def initialize(playlist_url)
@query = {:playlistURL => playlist_url}
end
- get_resource :fetch, :root => 'playlist', :resource_name => 'playlist.fetch'
+ has_resource :fetch, :root => 'playlist', :resource_name => 'playlist.fetch'
end
end
View
@@ -1,17 +1,18 @@
module Scrobbler2
class Tag < Base
-
+
def initialize(tag)
@query = {:tag => tag}
end
- get_resource :similar, :root => 'similar'
- get_resource :top_artists, :root => 'topartists'
- get_resource :top_albums, :root => 'topalbums'
- get_resource :top_tracks, :root => 'toptracks'
- get_resource :top_tags, :root => 'toptags'
- get_resource :weekly_artist_chart, :root => 'weeklyartistchart'
- get_resource :weekly_chart_list, :root => 'weeklychartlist'
+ has_singleton_resource :top_tags, :root => 'toptags'
+ has_resource :similar, :root => 'similar'
+ has_resource :top_artists, :root => 'topartists'
+ has_resource :top_albums, :root => 'topalbums'
+ has_resource :top_tracks, :root => 'toptracks'
+ has_resource :weekly_artist_chart, :root => 'weeklyartistchart'
+ has_resource :weekly_chart_list, :root => 'weeklychartlist'
+
end
end
View
@@ -5,9 +5,9 @@ def initialize(artist, track)
@query = {:artist => artist, :track => track}
end
- get_resource :info, :root => 'track'
- get_resource :similar, :root => 'similartracks'
- get_resource :top_fans, :root => 'topfans'
- get_resource :top_tags, :root => 'toptags'
+ has_resource :info, :root => 'track'
+ has_resource :similar, :root => 'similartracks'
+ has_resource :top_fans, :root => 'topfans'
+ has_resource :top_tags, :root => 'toptags'
end
end
View
@@ -5,25 +5,25 @@ def initialize(username)
@query = {:user => username}
end
- get_resource :events, :root => "events"
- get_resource :info, :root=>"user", :auth=>true, :query => {}
- get_resource :friends, :root => "friends"
- get_resource :loved_tracks, :root => "lovedtracks"
- get_resource :neighbours, :root => "neighbours"
- get_resource :past_events, :root => "events"
- get_resource :playlists, :root => "playlists"
- get_resource :recent_tracks, :root => "recenttracks"
- get_resource :recommended_artists, :root => "recommendedartists", :auth => true, :query => {}
- get_resource :recommended_events, :root => "recommendedevents", :auth => true, :query => {}
- get_resource :shouts, :root => "shouts"
- get_resource :top_albums, :root => "topalbums"
- get_resource :top_artists, :root => "topartists"
- get_resource :top_tags, :root => "toptags"
- get_resource :top_tracks, :root => "toptracks"
- get_resource :weekly_album_chart, :root => "weeklyalbumchart"
- get_resource :weekly_artist_chart, :root => "weeklyartistchart"
- get_resource :weekly_chart_list, :root => "weeklychartlist"
- get_resource :weekly_track_chart, :root => "weeklytrackchart"
+ has_resource :events, :root => "events"
+ has_resource :info, :root=>"user", :auth=>true, :query => {}
+ has_resource :friends, :root => "friends"
+ has_resource :loved_tracks, :root => "lovedtracks"
+ has_resource :neighbours, :root => "neighbours"
+ has_resource :past_events, :root => "events"
+ has_resource :playlists, :root => "playlists"
+ has_resource :recent_tracks, :root => "recenttracks"
+ has_resource :recommended_artists, :root => "recommendedartists", :auth => true, :query => {}
+ has_resource :recommended_events, :root => "recommendedevents", :auth => true, :query => {}
+ has_resource :shouts, :root => "shouts"
+ has_resource :top_albums, :root => "topalbums"
+ has_resource :top_artists, :root => "topartists"
+ has_resource :top_tags, :root => "toptags"
+ has_resource :top_tracks, :root => "toptracks"
+ has_resource :weekly_album_chart, :root => "weeklyalbumchart"
+ has_resource :weekly_artist_chart, :root => "weeklyartistchart"
+ has_resource :weekly_chart_list, :root => "weeklychartlist"
+ has_resource :weekly_track_chart, :root => "weeklytrackchart"
end
end
View
@@ -5,8 +5,8 @@ def initialize(id)
@query = {:venue => id}
end
- get_resource :events, :root => "events"
- get_resource :past_events, :root => "events"
+ has_resource :events, :root => "events"
+ has_resource :past_events, :root => "events"
end
end
@@ -1,10 +1,9 @@
require File.dirname(__FILE__) + '/../test_helper.rb'
-describe "Top Tags for Metallica" do
+describe "Top Tags" do
before(:all) do #We only want to hit the webservice once.
- @tag = Scrobbler2::Tag.new "Disco"
- @tags = @tag.top_tags
+ @tags = Scrobbler2::Tag.top_tags
end
it "should return a Hash" do
@tags.should be_kind_of(Hash)
@@ -32,8 +32,8 @@ def test_generator_without_options
run_generator('resource', funcs.unshift(resource), sources)
assert_generated_file "lib/scrobbler2/resource.rb" do |file|
assert file.include?("class Resource < Base")
- assert file.include?("get_resource :method1, :root => \"method1\"")
- assert file.include?("get_resource :method2, :root => \"method2\"")
+ assert file.include?("has_resource :method1, :root => \"method1\"")
+ assert file.include?("has_resource :method2, :root => \"method2\"")
end
assert_generated_file "test/unit/resource_spec.rb" do |file|
View
@@ -26,10 +26,10 @@ class TestBaseImplementation < Scrobbler2::Base
Scrobbler2::Base.api_secret.should == "blah"
end
- describe "get_resource" do
+ describe "has_resource" do
before(:each) do
- TestBaseImplementation.module_eval("get_resource :test_info, :root => :test");
+ TestBaseImplementation.module_eval("has_resource :test_info, :root => :test");
TestBaseImplementation.stub!(:get).and_return({})
@test = TestBaseImplementation.new
end
@@ -63,9 +63,9 @@ class TestBaseImplementation < Scrobbler2::Base
end
- describe "get_resource with options = {:resource_name => resource.method}" do
+ describe "has_resource with options = {:resource_name => resource.method}" do
before(:each) do
- TestBaseImplementation.module_eval("get_resource :test_info, :root => :test, :resource_name => 'resource.method'");
+ TestBaseImplementation.module_eval("has_resource :test_info, :root => :test, :resource_name => 'resource.method'");
TestBaseImplementation.stub!(:get).and_return({})
@test = TestBaseImplementation.new
end

0 comments on commit d04400d

Please sign in to comment.