active_lastfm 1.0.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/lib/active_lastfm.rb +36 -0
- data/lib/patch.rb +32 -0
- data/test/abstract_unit.rb +26 -0
- data/test/active_lastfm_test.rb +55 -0
- data/test/setter_trap.rb +27 -0
- metadata +65 -0
@@ -0,0 +1,36 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'activeresource'
|
3
|
+
require 'patch'
|
4
|
+
|
5
|
+
class ActiveLastfm < ActiveResource::Base
|
6
|
+
self.site = 'http://ws.audioscrobbler.com/2.0/'
|
7
|
+
# This abstract base-class extends ActiveResource::Base to make requests to last.fm
|
8
|
+
# Based largely on the ActiveYouTube examples found at:
|
9
|
+
# - http://www.quarkruby.com/2008/1/15/activeresource-and-youtube
|
10
|
+
# - http://www.quarkruby.com/2008/3/11/consume-non-rails-style-rest-apis
|
11
|
+
|
12
|
+
class << self
|
13
|
+
## Remove format from the url.
|
14
|
+
def element_path(id, prefix_options = {}, query_options = nil)
|
15
|
+
prefix_options, query_options = split_options(prefix_options) if query_options.nil?
|
16
|
+
"#{prefix(prefix_options)}#{query_string(query_options)}"
|
17
|
+
end
|
18
|
+
|
19
|
+
## Remove format from the url.
|
20
|
+
def collection_path(prefix_options = {}, query_options = nil)
|
21
|
+
prefix_options, query_options = split_options(prefix_options) if query_options.nil?
|
22
|
+
"#{prefix(prefix_options)}#{query_string(query_options)}"
|
23
|
+
end
|
24
|
+
|
25
|
+
## For a collection call, ActiveResource formatting is not
|
26
|
+
## compliant with YouTube's output.
|
27
|
+
def instantiate_collection(collection, prefix_options = {})
|
28
|
+
puts collection.inspect
|
29
|
+
unless collection.kind_of? Array
|
30
|
+
[instantiate_record(collection, prefix_options)]
|
31
|
+
else
|
32
|
+
collection.collect! { |record| instantiate_record(record, prefix_options) }
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
data/lib/patch.rb
ADDED
@@ -0,0 +1,32 @@
|
|
1
|
+
## Patched ActiveResouce code
|
2
|
+
## Will follow edge code and remove when included in a release
|
3
|
+
module ActiveResource
|
4
|
+
class Base
|
5
|
+
def load(attributes)
|
6
|
+
raise ArgumentError, "expected an attributes Hash, got #{attributes.inspect}" unless attributes.is_a?(Hash)
|
7
|
+
@prefix_options, attributes = split_options(attributes)
|
8
|
+
attributes.each do |key, value|
|
9
|
+
@attributes[key.to_s] =
|
10
|
+
case value
|
11
|
+
when Array
|
12
|
+
resource = find_or_create_resource_for_collection(key)
|
13
|
+
#<<< value.map { |attrs| resource.new(attrs) }
|
14
|
+
value.map { |attrs| attrs.is_a?(String) ? attrs.dup : resource.new(attrs) }
|
15
|
+
when Hash
|
16
|
+
resource = find_or_create_resource_for(key)
|
17
|
+
resource.new(value)
|
18
|
+
else
|
19
|
+
value.dup rescue value
|
20
|
+
end
|
21
|
+
end
|
22
|
+
self
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
class ConnectionError < StandardError
|
27
|
+
def build_request_headers(headers, http_method=nil)
|
28
|
+
#<<< authorization_header.update(default_header).update(headers).update(http_format_header(http_method))
|
29
|
+
authorization_header.update(default_header).update(headers)
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
require 'test/unit'
|
2
|
+
require 'rubygems'
|
3
|
+
|
4
|
+
$:.unshift "#{File.dirname(__FILE__)}/../lib"
|
5
|
+
require 'active_resource'
|
6
|
+
require 'active_resource/http_mock'
|
7
|
+
|
8
|
+
$:.unshift "#{File.dirname(__FILE__)}/../test"
|
9
|
+
require 'setter_trap'
|
10
|
+
|
11
|
+
ActiveResource::Base.logger = Logger.new("#{File.dirname(__FILE__)}/debug.log")
|
12
|
+
|
13
|
+
def uses_gem(gem_name, test_name, version = '> 0')
|
14
|
+
gem gem_name.to_s, version
|
15
|
+
require gem_name.to_s
|
16
|
+
yield
|
17
|
+
rescue LoadError
|
18
|
+
$stderr.puts "Skipping #{test_name} tests. `gem install #{gem_name}` and try again."
|
19
|
+
end
|
20
|
+
|
21
|
+
# Wrap tests that use Mocha and skip if unavailable.
|
22
|
+
unless defined? uses_mocha
|
23
|
+
def uses_mocha(test_name, &block)
|
24
|
+
uses_gem('mocha', test_name, '>= 0.5.5', &block)
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,55 @@
|
|
1
|
+
require 'abstract_unit'
|
2
|
+
require 'active_lastfm'
|
3
|
+
|
4
|
+
class ActiveLastfmTest < Test::Unit::TestCase
|
5
|
+
def feed(name)
|
6
|
+
path = File.dirname(__FILE__) + "/../test/fixtures/#{name}.xml"
|
7
|
+
return nil unless File.exists?(path)
|
8
|
+
File.read path
|
9
|
+
end
|
10
|
+
|
11
|
+
def setup
|
12
|
+
ActiveResource::HttpMock.respond_to do |mock|
|
13
|
+
mock.get "/2.0/?api_key=b25b959554ed76058ac220b7b2e0a026&artist=cher&method=artist.search", {}, feed("artist.search")
|
14
|
+
mock.get "/2.0/?api_key=b25b959554ed76058ac220b7b2e0a026&method=track.search&track=Belive", {}, feed("track.search")
|
15
|
+
mock.get "/2.0/?api_key=b25b959554ed76058ac220b7b2e0a026&artist=cher&method=artist.gettoptracks", {}, feed("artist.track.search")
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
def test_site
|
20
|
+
assert_equal(URI.parse('http://ws.audioscrobbler.com/2.0/'), ActiveLastfm.site)
|
21
|
+
end
|
22
|
+
|
23
|
+
def test_artist_search
|
24
|
+
result = ActiveLastfm.find(:first, :params => {
|
25
|
+
:api_key => 'b25b959554ed76058ac220b7b2e0a026',
|
26
|
+
:artist => 'cher',
|
27
|
+
:method => 'artist.search',
|
28
|
+
})
|
29
|
+
assert_not_nil result
|
30
|
+
assert_not_nil result.results.artistmatches
|
31
|
+
assert_equal 20, result.results.artistmatches.artist.size
|
32
|
+
end
|
33
|
+
|
34
|
+
def test_track_search
|
35
|
+
result = ActiveLastfm.find(:first, :params => {
|
36
|
+
:api_key => 'b25b959554ed76058ac220b7b2e0a026',
|
37
|
+
:track => 'Belive',
|
38
|
+
:method => 'track.search',
|
39
|
+
})
|
40
|
+
assert_not_nil result
|
41
|
+
assert_not_nil result.results.trackmatches
|
42
|
+
assert_equal 20, result.results.trackmatches.track.size
|
43
|
+
end
|
44
|
+
|
45
|
+
def test_artist_track_search
|
46
|
+
result = ActiveLastfm.find(:first, :params => {
|
47
|
+
:api_key => 'b25b959554ed76058ac220b7b2e0a026',
|
48
|
+
:artist => 'cher',
|
49
|
+
:method => 'artist.gettoptracks',
|
50
|
+
})
|
51
|
+
assert_not_nil result
|
52
|
+
assert_not_nil result.toptracks
|
53
|
+
assert_equal 50, result.toptracks.track.size
|
54
|
+
end
|
55
|
+
end
|
data/test/setter_trap.rb
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
class SetterTrap < ActiveSupport::BasicObject
|
2
|
+
class << self
|
3
|
+
def rollback_sets(obj)
|
4
|
+
returning yield(setter_trap = new(obj)) do
|
5
|
+
setter_trap.rollback_sets
|
6
|
+
end
|
7
|
+
end
|
8
|
+
end
|
9
|
+
|
10
|
+
def initialize(obj)
|
11
|
+
@cache = {}
|
12
|
+
@obj = obj
|
13
|
+
end
|
14
|
+
|
15
|
+
def respond_to?(method)
|
16
|
+
@obj.respond_to?(method)
|
17
|
+
end
|
18
|
+
|
19
|
+
def method_missing(method, *args, &proc)
|
20
|
+
@cache[method] ||= @obj.send($`) if method.to_s =~ /=$/
|
21
|
+
@obj.send method, *args, &proc
|
22
|
+
end
|
23
|
+
|
24
|
+
def rollback_sets
|
25
|
+
@cache.each { |k, v| @obj.send k, v }
|
26
|
+
end
|
27
|
+
end
|
metadata
ADDED
@@ -0,0 +1,65 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: active_lastfm
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Ben Smith
|
8
|
+
autorequire: activeresource
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2008-02-03 00:00:00 +00:00
|
13
|
+
default_executable:
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: activeresource
|
17
|
+
type: :runtime
|
18
|
+
version_requirement:
|
19
|
+
version_requirements: !ruby/object:Gem::Requirement
|
20
|
+
requirements:
|
21
|
+
- - ">="
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: 2.0.2
|
24
|
+
version:
|
25
|
+
description: Last.fm client for Ruby (and Rails) based on ActiveResource
|
26
|
+
email: ben@thesmith.co.uk
|
27
|
+
executables: []
|
28
|
+
|
29
|
+
extensions: []
|
30
|
+
|
31
|
+
extra_rdoc_files: []
|
32
|
+
|
33
|
+
files:
|
34
|
+
- lib/active_lastfm.rb
|
35
|
+
- lib/patch.rb
|
36
|
+
has_rdoc: false
|
37
|
+
homepage: http://thesmith.co.uk/
|
38
|
+
post_install_message:
|
39
|
+
rdoc_options: []
|
40
|
+
|
41
|
+
require_paths:
|
42
|
+
- lib
|
43
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: "0"
|
48
|
+
version:
|
49
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
50
|
+
requirements:
|
51
|
+
- - ">="
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: "0"
|
54
|
+
version:
|
55
|
+
requirements: []
|
56
|
+
|
57
|
+
rubyforge_project:
|
58
|
+
rubygems_version: 1.2.0
|
59
|
+
signing_key:
|
60
|
+
specification_version: 2
|
61
|
+
summary: Last.fm client for Ruby (and Rails) based on ActiveResource
|
62
|
+
test_files:
|
63
|
+
- test/abstract_unit.rb
|
64
|
+
- test/active_lastfm_test.rb
|
65
|
+
- test/setter_trap.rb
|