thesmith-active_lastfm 1.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/README +54 -0
- data/active_lastfm.gemspec +17 -0
- data/lib/active_lastfm.rb +38 -0
- data/lib/patch.rb +32 -0
- data/test/active_lastfm_test.rb +56 -0
- metadata +65 -0
data/README
ADDED
@@ -0,0 +1,54 @@
|
|
1
|
+
= Active Last.fm
|
2
|
+
|
3
|
+
link:http://github.com/thesmith/active_lastfm/tree/master
|
4
|
+
|
5
|
+
Active Last.fm is a client wrapper around the [http://last.fm last.fm] [http://www.last.fm/api/intro 2.0 web-services] based around ActiveResource.
|
6
|
+
|
7
|
+
== Overview
|
8
|
+
|
9
|
+
This wrapper is really simple at the moment. Last.fm 2.0's web-services provide an XML-RPC over XML/HTTP style interface to a load of interesting music and user information. ActiveResource is well tailored for purist RESTful interfaces but not for action-oriented services. This project basically overrides ActiveResource's URL creation for Last.fm's static URL so that the developer can simply wrap ActiveResource's default methods to create their own models around Last.fm action types.
|
10
|
+
|
11
|
+
== References
|
12
|
+
|
13
|
+
I really haven't done all that much myself here, here are a list of articles / projects that I've borrowed from:
|
14
|
+
* link:http://www.quarkruby.com/2008/1/15/activeresource-and-youtube
|
15
|
+
* link:http://www.quarkruby.com/2008/3/11/consume-non-rails-style-rest-apis
|
16
|
+
* link:http://www.quarkruby.com/2008/2/12/active-youtube
|
17
|
+
* link:http://github.com/rails/rails/tree/master/activeresource
|
18
|
+
|
19
|
+
== Usage
|
20
|
+
|
21
|
+
For example, if you wanted to use Last.fm's music ontology to create an Artist model then you might set it up something like this:
|
22
|
+
|
23
|
+
require 'active_lastfm'
|
24
|
+
class Artist < ActiveLastfm
|
25
|
+
def search(artist)
|
26
|
+
search_result = self.find(:first, :params => {
|
27
|
+
:method => 'artist.search',
|
28
|
+
:artist => artist,
|
29
|
+
:api_key => 'b25b959554ed76058ac220b7b2e0a026'
|
30
|
+
})
|
31
|
+
|
32
|
+
search_result.results.artistmatches.artist
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
Now, obviously, that's fairly buggy, what with the lack of result checking and coercing, but you get the idea. The API key used is the one that Last.fm have in their examples, so I'd suggest you go get your own one.
|
37
|
+
|
38
|
+
== Testing
|
39
|
+
|
40
|
+
The current unit tests use ActiveResource::HttpMock to have the mock service respond to requests and return cached results from Last.fm, stored in 'test/fixtures/'. The current methods tested are:
|
41
|
+
* track.search
|
42
|
+
* artist.search
|
43
|
+
* artist.gettoptracks
|
44
|
+
|
45
|
+
These tests pass when run under Rake from a checked out copy, but they fail, horribly, when you test under gem (say with a: sudo gem install active_lastfm -t). Hopefully I'll get round to fixing that!
|
46
|
+
|
47
|
+
== ToDo
|
48
|
+
|
49
|
+
* Proper documentation.
|
50
|
+
* Set up a proper release process from link:http://github.com/thesmith/active_lastfm/tree/master to link:http://rubyforge.org/projects/active-lastfm/.
|
51
|
+
* Start working out / test all the ActiveResource methods other than 'find'.
|
52
|
+
* Create re-usable models.
|
53
|
+
* Make sure I remove the ActiveResource patches that I've had to use.
|
54
|
+
|
@@ -0,0 +1,17 @@
|
|
1
|
+
Gem::Specification.new do |s|
|
2
|
+
s.name = %q{active_lastfm}
|
3
|
+
s.version = "1.0.1"
|
4
|
+
s.date = %q{2008-10-12}
|
5
|
+
s.authors = ["Ben Smith"]
|
6
|
+
s.email = %q{ben [at] thesmith [dot] co [dot] uk}
|
7
|
+
s.has_rdoc = true
|
8
|
+
s.rdoc_options = ["--main", "README"]
|
9
|
+
s.extra_rdoc_files = ["README"]
|
10
|
+
s.summary = %q{Last.fm client for Ruby (and Rails) based on ActiveResource}
|
11
|
+
s.homepage = %q{http://github.com/thesmith/active_lastfm}
|
12
|
+
s.description = %q{Last.fm client for Ruby (and Rails) based on ActiveResource}
|
13
|
+
s.files = ["README", "active_lastfm.gemspec", "lib/active_lastfm.rb", "lib/patch.rb"]
|
14
|
+
s.add_dependency('activeresource', '>= 2.0.2')
|
15
|
+
s.test_files = ('test/active_lastfm_test.rb')
|
16
|
+
end
|
17
|
+
|
@@ -0,0 +1,38 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'activeresource'
|
3
|
+
require 'patch'
|
4
|
+
|
5
|
+
# This abstract base-class extends ActiveResource::Base to make requests to last.fm
|
6
|
+
# Based largely on the ActiveYouTube examples found at:
|
7
|
+
# * link:http://www.quarkruby.com/2008/1/15/activeresource-and-youtube
|
8
|
+
# * link:http://www.quarkruby.com/2008/3/11/consume-non-rails-style-rest-apis
|
9
|
+
# * link:http://www.quarkruby.com/2008/2/12/active-youtube
|
10
|
+
class ActiveLastfm < ActiveResource::Base
|
11
|
+
# The core Last.fm WS URL is the only URI used in this interface
|
12
|
+
self.site = 'http://ws.audioscrobbler.com/2.0/'
|
13
|
+
|
14
|
+
class << self
|
15
|
+
## Remove format from the url.
|
16
|
+
def element_path(id, prefix_options = {}, query_options = nil)
|
17
|
+
prefix_options, query_options = split_options(prefix_options) if query_options.nil?
|
18
|
+
"#{prefix(prefix_options)}#{query_string(query_options)}"
|
19
|
+
end
|
20
|
+
|
21
|
+
## Remove format from the url.
|
22
|
+
def collection_path(prefix_options = {}, query_options = nil)
|
23
|
+
prefix_options, query_options = split_options(prefix_options) if query_options.nil?
|
24
|
+
"#{prefix(prefix_options)}#{query_string(query_options)}"
|
25
|
+
end
|
26
|
+
|
27
|
+
## For a collection call, ActiveResource formatting is not
|
28
|
+
## compliant with YouTube's output.
|
29
|
+
def instantiate_collection(collection, prefix_options = {})
|
30
|
+
puts collection.inspect
|
31
|
+
unless collection.kind_of? Array
|
32
|
+
[instantiate_record(collection, prefix_options)]
|
33
|
+
else
|
34
|
+
collection.collect! { |record| instantiate_record(record, prefix_options) }
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
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,56 @@
|
|
1
|
+
$:.unshift "#{File.dirname(__FILE__)}/../test"
|
2
|
+
require 'abstract_unit'
|
3
|
+
require 'active_lastfm'
|
4
|
+
|
5
|
+
class ActiveLastfmTest < Test::Unit::TestCase
|
6
|
+
def feed(name)
|
7
|
+
path = File.dirname(__FILE__) + "/../test/fixtures/#{name}.xml"
|
8
|
+
return nil unless File.exists?(path)
|
9
|
+
File.read path
|
10
|
+
end
|
11
|
+
|
12
|
+
def setup
|
13
|
+
ActiveResource::HttpMock.respond_to do |mock|
|
14
|
+
mock.get "/2.0/?api_key=b25b959554ed76058ac220b7b2e0a026&artist=cher&method=artist.search", {}, feed("artist.search")
|
15
|
+
mock.get "/2.0/?api_key=b25b959554ed76058ac220b7b2e0a026&method=track.search&track=Belive", {}, feed("track.search")
|
16
|
+
mock.get "/2.0/?api_key=b25b959554ed76058ac220b7b2e0a026&artist=cher&method=artist.gettoptracks", {}, feed("artist.track.search")
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
def test_site
|
21
|
+
assert_equal(URI.parse('http://ws.audioscrobbler.com/2.0/'), ActiveLastfm.site)
|
22
|
+
end
|
23
|
+
|
24
|
+
def test_artist_search
|
25
|
+
result = ActiveLastfm.find(:first, :params => {
|
26
|
+
:api_key => 'b25b959554ed76058ac220b7b2e0a026',
|
27
|
+
:artist => 'cher',
|
28
|
+
:method => 'artist.search',
|
29
|
+
})
|
30
|
+
assert_not_nil result
|
31
|
+
assert_not_nil result.results.artistmatches
|
32
|
+
assert_equal 20, result.results.artistmatches.artist.size
|
33
|
+
end
|
34
|
+
|
35
|
+
def test_track_search
|
36
|
+
result = ActiveLastfm.find(:first, :params => {
|
37
|
+
:api_key => 'b25b959554ed76058ac220b7b2e0a026',
|
38
|
+
:track => 'Belive',
|
39
|
+
:method => 'track.search',
|
40
|
+
})
|
41
|
+
assert_not_nil result
|
42
|
+
assert_not_nil result.results.trackmatches
|
43
|
+
assert_equal 20, result.results.trackmatches.track.size
|
44
|
+
end
|
45
|
+
|
46
|
+
def test_artist_track_search
|
47
|
+
result = ActiveLastfm.find(:first, :params => {
|
48
|
+
:api_key => 'b25b959554ed76058ac220b7b2e0a026',
|
49
|
+
:artist => 'cher',
|
50
|
+
:method => 'artist.gettoptracks',
|
51
|
+
})
|
52
|
+
assert_not_nil result
|
53
|
+
assert_not_nil result.toptracks
|
54
|
+
assert_equal 50, result.toptracks.track.size
|
55
|
+
end
|
56
|
+
end
|
metadata
ADDED
@@ -0,0 +1,65 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: thesmith-active_lastfm
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Ben Smith
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2008-10-12 00:00:00 -07:00
|
13
|
+
default_executable:
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: activeresource
|
17
|
+
version_requirement:
|
18
|
+
version_requirements: !ruby/object:Gem::Requirement
|
19
|
+
requirements:
|
20
|
+
- - ">="
|
21
|
+
- !ruby/object:Gem::Version
|
22
|
+
version: 2.0.2
|
23
|
+
version:
|
24
|
+
description: Last.fm client for Ruby (and Rails) based on ActiveResource
|
25
|
+
email: ben [at] thesmith [dot] co [dot] uk
|
26
|
+
executables: []
|
27
|
+
|
28
|
+
extensions: []
|
29
|
+
|
30
|
+
extra_rdoc_files:
|
31
|
+
- README
|
32
|
+
files:
|
33
|
+
- README
|
34
|
+
- active_lastfm.gemspec
|
35
|
+
- lib/active_lastfm.rb
|
36
|
+
- lib/patch.rb
|
37
|
+
has_rdoc: true
|
38
|
+
homepage: http://github.com/thesmith/active_lastfm
|
39
|
+
post_install_message:
|
40
|
+
rdoc_options:
|
41
|
+
- --main
|
42
|
+
- README
|
43
|
+
require_paths:
|
44
|
+
- lib
|
45
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
46
|
+
requirements:
|
47
|
+
- - ">="
|
48
|
+
- !ruby/object:Gem::Version
|
49
|
+
version: "0"
|
50
|
+
version:
|
51
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
52
|
+
requirements:
|
53
|
+
- - ">="
|
54
|
+
- !ruby/object:Gem::Version
|
55
|
+
version: "0"
|
56
|
+
version:
|
57
|
+
requirements: []
|
58
|
+
|
59
|
+
rubyforge_project:
|
60
|
+
rubygems_version: 1.2.0
|
61
|
+
signing_key:
|
62
|
+
specification_version: 2
|
63
|
+
summary: Last.fm client for Ruby (and Rails) based on ActiveResource
|
64
|
+
test_files:
|
65
|
+
- test/active_lastfm_test.rb
|