yahoo-music 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,3 @@
1
+ == 0.1.0 2008-08-07
2
+
3
+ * Initial release
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2008 Mattt Thompson
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,33 @@
1
+ History.txt
2
+ License.txt
3
+ Manifest.txt
4
+ PostInstall.txt
5
+ README.txt
6
+ Rakefile
7
+ config/hoe.rb
8
+ config/requirements.rb
9
+ lib/rest.rb
10
+ lib/yahoo-music.rb
11
+ lib/yahoo-music/base.rb
12
+ lib/yahoo-music/artist.rb
13
+ lib/yahoo-music/category.rb
14
+ lib/yahoo-music/release.rb
15
+ lib/yahoo-music/review.rb
16
+ lib/yahoo-music/track.rb
17
+ lib/yahoo-music/video.rb
18
+ lib/yahoo-music/version.rb
19
+ script/console
20
+ script/destroy
21
+ script/generate
22
+ script/txt2html
23
+ setup.rb
24
+ tasks/deployment.rake
25
+ tasks/environment.rake
26
+ tasks/website.rake
27
+ test/test_helper.rb
28
+ test/fixtures/artist.xml
29
+ test/fixtures/categories.xml
30
+ test/fixtures/release.xml
31
+ test/test_yahoo_music_artist.rb
32
+ test/test_yahoo_music_release.rb
33
+ website/index.txt
File without changes
@@ -0,0 +1,85 @@
1
+ = yahoo-music
2
+
3
+ A Ruby wrapper for the Yahoo! Music APIs.
4
+
5
+ == Example Usage
6
+
7
+ === Artists:
8
+
9
+ require 'yahoo-music'
10
+ include Yahoo::Music
11
+ Yahoo::Music.app_id = [Your App ID Here]
12
+
13
+ artist = Artist.new("Ben Folds Five")
14
+
15
+ puts artist.name
16
+ puts artist.website
17
+
18
+ puts '*' * 40
19
+ puts
20
+
21
+ puts 'Releases'
22
+ artist.releases.each do |release|
23
+ puts "\t- %s" % release.title
24
+ end
25
+
26
+ === Releases & Tracks:
27
+
28
+ require 'yahoo-music'
29
+ include Yahoo::Music
30
+ Yahoo::Music.app_id = [Your App ID Here]
31
+
32
+ album = Album.search("The White Album").first
33
+
34
+ puts album.title
35
+ puts album.artist
36
+ puts "Release Date:" + album.released_on.strftime("%m/%d/%Y")
37
+
38
+ puts '*' * 40
39
+ puts
40
+
41
+ puts 'Tracks'
42
+ artist.tracks.each_with_index do |track, i|
43
+ puts "\t%d %s \t%2d:%2d" % [i, track.title, track.duration / 60, track.duration % 60]
44
+ end
45
+
46
+
47
+ == REQUIREMENTS:
48
+
49
+ To use this library, you must have a valid Yahoo! App ID.
50
+ You can get one at http://developer.yahoo.com/wsregapp/
51
+
52
+ Additionally, yahoo-music has the following gem dependencies:
53
+
54
+ * Hpricot >= 0.6
55
+ * ActiveSupport >= 2.1.0
56
+ * FlexMock >= 0.8.2
57
+
58
+ == INSTALL:
59
+
60
+ * sudo gem install yahoo-music
61
+
62
+ == LICENSE:
63
+
64
+ (The MIT License)
65
+
66
+ Copyright (c) 2008 Mattt Thompson
67
+
68
+ Permission is hereby granted, free of charge, to any person obtaining
69
+ a copy of this software and associated documentation files (the
70
+ 'Software'), to deal in the Software without restriction, including
71
+ without limitation the rights to use, copy, modify, merge, publish,
72
+ distribute, sublicense, and/or sell copies of the Software, and to
73
+ permit persons to whom the Software is furnished to do so, subject to
74
+ the following conditions:
75
+
76
+ The above copyright notice and this permission notice shall be
77
+ included in all copies or substantial portions of the Software.
78
+
79
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
80
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
81
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
82
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
83
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
84
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
85
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,4 @@
1
+ require 'config/requirements'
2
+ require 'config/hoe' # setup Hoe + all gem configuration
3
+
4
+ Dir['tasks/**/*.rake'].each { |rake| load rake }
@@ -0,0 +1,75 @@
1
+ require 'yahoo-music/version'
2
+
3
+ AUTHOR = "Mattt Thompson"
4
+ EMAIL = "mail@matttthompson.com"
5
+ DESCRIPTION = "yahoo-music is a wrapper to the Y! Music REST APIs"
6
+ GEM_NAME = 'yahoo-music' # what ppl will type to install your gem
7
+ RUBYFORGE_PROJECT = 'yahoo-music' # The unix name for your project
8
+ HOMEPATH = "http://#{RUBYFORGE_PROJECT}.rubyforge.org"
9
+ DOWNLOAD_PATH = "http://rubyforge.org/projects/#{RUBYFORGE_PROJECT}"
10
+ EXTRA_DEPENDENCIES = [
11
+ ['hpricot', '>= 0.6'],
12
+ ['activesupport', '>= 2.1.0'],
13
+ ['flexmock', '>= 0.8.2']
14
+ ] # An array of rubygem dependencies [name, version]
15
+
16
+ @config_file = "~/.rubyforge/user-config.yml"
17
+ @config = nil
18
+ RUBYFORGE_USERNAME = "mattt"
19
+ def rubyforge_username
20
+ unless @config
21
+ begin
22
+ @config = YAML.load(File.read(File.expand_path(@config_file)))
23
+ rescue
24
+ puts <<-EOS
25
+ ERROR: No rubyforge config file found: #{@config_file}
26
+ Run 'rubyforge setup' to prepare your env for access to Rubyforge
27
+ - See http://newgem.rubyforge.org/rubyforge.html for more details
28
+ EOS
29
+ exit
30
+ end
31
+ end
32
+ RUBYFORGE_USERNAME.replace @config["username"]
33
+ end
34
+
35
+
36
+ REV = nil
37
+ # UNCOMMENT IF REQUIRED:
38
+ # REV = YAML.load(`svn info`)['Revision']
39
+ VERS = Yahoo::Music::VERSION::STRING + (REV ? ".#{REV}" : "")
40
+ RDOC_OPTS = ['--quiet', '--title', 'yahoo-music documentation',
41
+ "--opname", "index.html",
42
+ "--line-numbers",
43
+ "--main", "README",
44
+ "--inline-source"]
45
+
46
+ class Hoe
47
+ def extra_deps
48
+ @extra_deps.reject! { |x| Array(x).first == 'hoe' }
49
+ @extra_deps
50
+ end
51
+ end
52
+
53
+ # Generate all the Rake tasks
54
+ # Run 'rake -T' to see list of generated tasks (from gem root directory)
55
+ $hoe = Hoe.new(GEM_NAME, VERS) do |p|
56
+ p.developer(AUTHOR, EMAIL)
57
+ p.description = DESCRIPTION
58
+ p.summary = DESCRIPTION
59
+ p.url = HOMEPATH
60
+ p.rubyforge_name = RUBYFORGE_PROJECT if RUBYFORGE_PROJECT
61
+ p.test_globs = ["test/**/test_*.rb"]
62
+ p.clean_globs |= ['**/.*.sw?', '*.gem', '.config', '**/.DS_Store'] #An array of file patterns to delete on clean.
63
+
64
+ # == Optional
65
+ p.changes = p.paragraphs_of("History.txt", 0..1).join("\n\n")
66
+ #p.extra_deps = EXTRA_DEPENDENCIES
67
+
68
+ #p.spec_extras = {} # A hash of extra values to set in the gemspec.
69
+ end
70
+
71
+ CHANGES = $hoe.paragraphs_of('History.txt', 0..1).join("\\n\\n")
72
+ PATH = (RUBYFORGE_PROJECT == GEM_NAME) ? RUBYFORGE_PROJECT : "#{RUBYFORGE_PROJECT}/#{GEM_NAME}"
73
+ $hoe.remote_rdoc_dir = File.join(PATH.gsub(/^#{RUBYFORGE_PROJECT}\/?/,''), 'rdoc')
74
+ $hoe.rsync_args = '-av --delete --ignore-errors'
75
+ $hoe.spec.post_install_message = File.open(File.dirname(__FILE__) + "/../PostInstall.txt").read rescue ""
@@ -0,0 +1,15 @@
1
+ require 'fileutils'
2
+ include FileUtils
3
+
4
+ require 'rubygems'
5
+ %w[rake hoe newgem rubigen].each do |req_gem|
6
+ begin
7
+ require req_gem
8
+ rescue LoadError
9
+ puts "This Rakefile requires the '#{req_gem}' RubyGem."
10
+ puts "Installation: gem install #{req_gem} -y"
11
+ exit
12
+ end
13
+ end
14
+
15
+ $:.unshift(File.join(File.dirname(__FILE__), %w[.. lib]))
@@ -0,0 +1,48 @@
1
+ # Ported from John Nunemaker's Scrobbler Gem (http://scrobbler.rubyforge.org/)
2
+
3
+ require 'net/https'
4
+
5
+ module REST
6
+ class Connection
7
+ def initialize(base_url, args = {})
8
+ @base_url = base_url
9
+ @username = args['username']
10
+ @password = args['password']
11
+ @app_id = args['app_id']
12
+ end
13
+
14
+ def get(resource, args = nil)
15
+ request(resource, "get", args)
16
+ end
17
+
18
+ def post(resource, args = nil)
19
+ request(resource, "post", args)
20
+ end
21
+
22
+ def request(resource, method = "get", args = nil)
23
+ url = URI.join(@base_url, resource)
24
+
25
+ if args = args.update('appid' => @app_id)
26
+ # TODO: What about keys without value?
27
+ url.query = args.map { |k,v| "%s=%s" % [URI.encode(k), URI.encode(v)] }.join("&")
28
+ end
29
+
30
+ case method
31
+ when "get"
32
+ req = Net::HTTP::Get.new(url.request_uri)
33
+ when "post"
34
+ req = Net::HTTP::Post.new(url.request_uri)
35
+ end
36
+
37
+ if @username and @password
38
+ req.basic_auth(@username, @password)
39
+ end
40
+
41
+ http = Net::HTTP.new(url.host, url.port)
42
+ http.use_ssl = (url.port == 443)
43
+
44
+ res = http.start() { |conn| conn.request(req) }
45
+ res.body
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,29 @@
1
+ %w{rubygems cgi hpricot activesupport}.each { |x| require x }
2
+
3
+ $:.unshift(File.dirname(__FILE__)) unless
4
+ $:.include?(File.dirname(__FILE__)) || $:.include?(File.expand_path(File.dirname(__FILE__)))
5
+
6
+ require 'rest'
7
+ require 'yahoo-music/base'
8
+ require 'yahoo-music/version'
9
+
10
+ require 'yahoo-music/artist'
11
+ require 'yahoo-music/category'
12
+ require 'yahoo-music/release'
13
+ require 'yahoo-music/review'
14
+ require 'yahoo-music/track'
15
+ require 'yahoo-music/video'
16
+
17
+ module Yahoo
18
+ module Music
19
+ LOCALE = "us"
20
+ API_URL = "http://#{LOCALE}.music.yahooapis.com/"
21
+ API_VERSION = 'v1'
22
+
23
+ class << self
24
+ def app_id=(_id)
25
+ Yahoo::Music::Base::connection = REST::Connection.new(API_URL, 'app_id' => _id)
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,31 @@
1
+ # <Artist
2
+ # catzillaID = xs:int
3
+ # flags = xs:int
4
+ # hotzillaID = xs:int
5
+ # id = xs:string
6
+ # name = xs:string
7
+ # rating = xs:int
8
+ # salesGenreCode = xs:int
9
+ # sortName = xs:string
10
+ # trackCount = xs:int
11
+ # website = xs:string
12
+ # >
13
+ #
14
+ # Content:
15
+ # Image*, Category*, Releases?, TopTracks?, TopSimilarArtists?, RadioStations?, Events?, Fans?, NewsArticles?, ReleaseReviews?, ShortBio?, FullBio?, ItemInfo?, Video*
16
+ # </Artist>
17
+
18
+ module Yahoo
19
+ module Music
20
+ class Artist < Base
21
+ attribute :id, Integer
22
+ attribute :name, String
23
+ attribute :sort_name, String, :matcher => "sortName"
24
+ attribute :website, String
25
+
26
+ attribute :releases, Release
27
+ attribute :categories, Category
28
+ attribute :videos, Video
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,147 @@
1
+ module Yahoo
2
+ module Music
3
+ class Base
4
+ class << self
5
+ attr_accessor :attributes, :associations
6
+ cattr_accessor :connection
7
+
8
+ def attribute(*args)
9
+ @attributes ||= {}
10
+ @associations ||= []
11
+
12
+ options = args.extract_options!
13
+ name, type = args
14
+ class_eval %(attr_accessor :#{name})
15
+ @attributes[name] = options.update({:type => type})
16
+
17
+ if Yahoo::Music::Base.subclasses.include?(type.inspect)
18
+ @associations << name
19
+
20
+ # Define plural and singular association methods
21
+ define_method("#{name}".pluralize.to_sym) do
22
+ value = instance_variable_get("@#{name}") || query_association_by_id(name, self.id)
23
+ instance_variable_set("@#{name}", value)
24
+ return value
25
+ end
26
+
27
+ define_method("#{name}".singularize.to_sym) do
28
+ value = instance_variable_get("@#{name}") || query_association_by_id(name, self.id)
29
+ value = value.first
30
+ instance_variable_set("@#{name}", value)
31
+ return value
32
+ end
33
+ end
34
+
35
+ if options[:type] == Boolean
36
+ define_method("#{name}?".to_sym) do
37
+ value = instance_variable_get("@#{name}")
38
+ return value
39
+ end
40
+ end
41
+ end
42
+
43
+ def attributes
44
+ @attributes || {}
45
+ end
46
+
47
+ def associations
48
+ @associations || []
49
+ end
50
+
51
+ def name_with_demodulization
52
+ self.name_without_demodulization.demodulize
53
+ end
54
+
55
+ alias_method_chain :name, :demodulization
56
+
57
+ def fetch_and_parse(resource, options = {})
58
+ raise YahooWebServiceError, "No App ID specified" if connection.nil?
59
+ options = options.update({'response' => self.associations.join(',')}) if self.associations.any?
60
+ return Hpricot::XML(connection.get(resource, options))
61
+ end
62
+
63
+ def api_path(service, resource, method, *args)
64
+ response_type = method.nil? ? :item : :list
65
+ parameters = [service, API_VERSION, response_type, resource, method, *args].compact
66
+ return parameters.collect!{|param| CGI::escape(param.to_s).downcase}.join('/')
67
+ end
68
+
69
+ # Search by a parameter for a specific service
70
+ # Ex. Artist.search(term)
71
+ # options[:search_mode]
72
+ def search(*args)
73
+ options = args.extract_options!
74
+ xml = fetch_and_parse(api_path(self.name, nil, :search, options[:search_mode] || :all, args.join(',')), options)
75
+ return xml.search(self.name).collect{|elem| self.new(elem)}
76
+ end
77
+ end
78
+
79
+ def initialize(xml)
80
+ raise ArgumentError unless xml.kind_of?(Hpricot)
81
+
82
+ self.class.attributes.each do |attribute, options|
83
+ value = xml.attributes[options[:matcher] || attribute.to_s]
84
+ begin
85
+ if options[:type] == Integer
86
+ value = value.to_i
87
+ elsif options[:type] == Float
88
+ value = value.to_f
89
+ elsif options[:type] == Date
90
+ value = Date.parse(value) rescue nil
91
+ elsif options[:type] == Boolean
92
+ value = !! value.to_i.nonzero?
93
+ elsif self.class.associations.include?(attribute)
94
+ klass = options[:type]
95
+ value = xml.search(klass.name).collect{|elem| klass.new(elem)}
96
+ value = nil if value.empty?
97
+ end
98
+ ensure
99
+ self.instance_variable_set("@#{attribute}", value)
100
+ end
101
+ end
102
+ end
103
+
104
+ def initialize_with_polymorphism(arg)
105
+ case arg
106
+ when String
107
+ initialize_without_polymorphism(query_by_string(arg))
108
+ when Integer
109
+ initialize_without_polymorphism(query_by_id(arg))
110
+ when Hpricot
111
+ initialize_without_polymorphism(arg)
112
+ end
113
+ end
114
+
115
+ alias_method_chain :initialize, :polymorphism
116
+
117
+ protected
118
+ def query_by_id(id)
119
+ xml = self.class.fetch_and_parse(self.class.api_path(self.class.name, nil, nil, id))
120
+ return xml.at(self.class.name)
121
+ end
122
+
123
+ def query_by_string(string)
124
+ xml = self.class.fetch_and_parse(self.class.api_path(self.class.name, nil, :search, :all, string ))
125
+ return xml.at(self.class.name)
126
+ end
127
+
128
+ def query_association_by_id(association, id)
129
+ klass = "yahoo/music/#{association.to_s.singularize}".camelize.constantize
130
+ xml = self.query_by_id(id).search(klass.name)
131
+ return xml.collect{|elem| klass.new(elem)}
132
+ end
133
+ end
134
+
135
+ class YahooWebServiceError < StandardError; end
136
+
137
+ class Artist < Base; end
138
+ class Category < Base; end
139
+ class Image < Base; end
140
+ class Release < Base; end
141
+ class Review < Base; end
142
+ class Track < Base; end
143
+ class Video < Base; end
144
+ end
145
+ end
146
+
147
+ class Boolean; end