carldr-scrobble.rb 0.0.1

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 ADDED
File without changes
@@ -0,0 +1,40 @@
1
+ require "rubygems"
2
+ require "cgi"
3
+ require "hpricot"
4
+ require "active_support"
5
+ require "net/http"
6
+
7
+ module Scrobble
8
+ API_URL = "http://ws.audioscrobbler.com/"
9
+ API_VERSION = "2.0"
10
+
11
+ class Base
12
+ def self.get_and_parse( resource )
13
+ Hpricot::XML( connection.get( resource ) )
14
+ end
15
+
16
+
17
+ private
18
+
19
+ def self.connection
20
+ @connection ||= REST::Connection.new( API_URL )
21
+ end
22
+
23
+ def get_instance(api_method, instance_name, element)
24
+ scrobble_class = "scrobble/#{element.to_s}".camelize.constantize
25
+
26
+ if instance_variable_get("@#{instance_name}").nil?
27
+ doc = self.class.get_and_parse("#{resource_path}/#{api_method}.xml")
28
+
29
+ elements = (doc/element).map do |el|
30
+ scrobble_class.new_from_element( el, doc )
31
+ end
32
+
33
+ instance_variable_set("@#{instance_name}", elements)
34
+ end
35
+
36
+ instance_variable_get("@#{instance_name}")
37
+ end
38
+
39
+ end
40
+ end
@@ -0,0 +1,40 @@
1
+ module Scrobble
2
+ module REST
3
+ class Connection
4
+ def initialize( base_url )
5
+ @base_url = base_url
6
+ end
7
+
8
+ def get( resource )
9
+ request( resource, "get" )
10
+ end
11
+
12
+ def post( resource )
13
+ request( resource, "post" )
14
+ end
15
+
16
+
17
+ protected
18
+
19
+ # TODO: Sleep for a second if necessary.
20
+ def request( resource, method )
21
+ uri = URI.join( @base_url, resource )
22
+
23
+ req = case method
24
+ when "get"
25
+ puts "Getting #{uri}" if $DEBUG
26
+ Net::HTTP::Get.new( uri.request_uri, { "User-Agent" => "Scobble.rb 0.0.1" } )
27
+
28
+ when "post"
29
+ puts "Posting #{uri}" if $DEBUG
30
+ Net::HTTP::Post.new( uri.request_uri, { "User-Agent" => "Scobble.rb 0.0.1" } )
31
+ end
32
+
33
+ http = Net::HTTP.new( uri.host, uri.port )
34
+
35
+ res = http.start { |conn| conn.request( req ) }
36
+ res.body
37
+ end
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,19 @@
1
+ module Scrobble
2
+ class Track < Base
3
+ # FIXME: We really want this to be attr_reader, but new_from_element needs to write. Perhaps emulate Rails' ability to pass a block.
4
+ attr_accessor :name, :now_playing
5
+
6
+ def self.new_from_element( element, doc )
7
+ t = Track.new
8
+ t.now_playing = !!element[ "nowplaying" ]
9
+ t.name = (element/"name").inner_text
10
+
11
+ t
12
+ end
13
+
14
+
15
+ def now_playing?
16
+ now_playing
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,29 @@
1
+ module Scrobble
2
+ class User < Base
3
+ def self.new_from_element( element, doc )
4
+ u = User.new( element[ "username" ] )
5
+ end
6
+
7
+ def initialize( username )
8
+ @username = username
9
+ end
10
+
11
+ def resource_path
12
+ "/#{API_VERSION}/user/#{CGI::escape( @username )}"
13
+ end
14
+
15
+ def currently_playing
16
+ track = recent_tracks.first
17
+
18
+ if track.now_playing?
19
+ track
20
+ else
21
+ nil
22
+ end
23
+ end
24
+
25
+ def recent_tracks
26
+ get_instance( :recenttracks, :recent_tracks, :track )
27
+ end
28
+ end
29
+ end
data/lib/scrobble.rb ADDED
@@ -0,0 +1,5 @@
1
+ require File.dirname( __FILE__ ) + "/scrobble/base"
2
+ require File.dirname( __FILE__ ) + "/scrobble/rest"
3
+
4
+ require File.dirname( __FILE__ ) + "/scrobble/user"
5
+ require File.dirname( __FILE__ ) + "/scrobble/track"
metadata ADDED
@@ -0,0 +1,75 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: carldr-scrobble.rb
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Carl Drinkwater
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2008-10-06 00:00:00 -07:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: hpricot
17
+ version_requirement:
18
+ version_requirements: !ruby/object:Gem::Requirement
19
+ requirements:
20
+ - - ">="
21
+ - !ruby/object:Gem::Version
22
+ version: "0"
23
+ version:
24
+ - !ruby/object:Gem::Dependency
25
+ name: activesupport
26
+ version_requirement:
27
+ version_requirements: !ruby/object:Gem::Requirement
28
+ requirements:
29
+ - - ">="
30
+ - !ruby/object:Gem::Version
31
+ version: "0"
32
+ version:
33
+ description: A library to enable access to last.fm's API.
34
+ email: github@carldr.com
35
+ executables: []
36
+
37
+ extensions: []
38
+
39
+ extra_rdoc_files: []
40
+
41
+ files:
42
+ - README
43
+ - lib/scrobble.rb
44
+ - lib/scrobble/base.rb
45
+ - lib/scrobble/track.rb
46
+ - lib/scrobble/user.rb
47
+ - lib/scrobble/rest.rb
48
+ has_rdoc: false
49
+ homepage: https://github.com/carldr/scrobble.rb
50
+ post_install_message:
51
+ rdoc_options: []
52
+
53
+ require_paths:
54
+ - lib
55
+ required_ruby_version: !ruby/object:Gem::Requirement
56
+ requirements:
57
+ - - ">="
58
+ - !ruby/object:Gem::Version
59
+ version: "0"
60
+ version:
61
+ required_rubygems_version: !ruby/object:Gem::Requirement
62
+ requirements:
63
+ - - ">="
64
+ - !ruby/object:Gem::Version
65
+ version: "0"
66
+ version:
67
+ requirements: []
68
+
69
+ rubyforge_project:
70
+ rubygems_version: 1.2.0
71
+ signing_key:
72
+ specification_version: 2
73
+ summary: A library to enable access to last.fm's API
74
+ test_files: []
75
+