kschrader-mtv-music 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
data/README ADDED
@@ -0,0 +1,75 @@
1
+ = mtv-music
2
+
3
+ A Ruby wrapper for the MTV Music API.
4
+ See http://developer.mtvnservices.com/ for more information about the API.
5
+
6
+ == Example Usage
7
+
8
+ === Artists:
9
+
10
+ require 'mtv-music'
11
+ include MTV::Music
12
+
13
+ artist = Artist.new("Radiohead")
14
+
15
+ puts artist.name
16
+ puts artist.website
17
+
18
+ puts '*' * 40
19
+ puts
20
+
21
+ puts 'Videos'
22
+ artist.videos.each do |video|
23
+ puts "\t- %s" % video.title
24
+ end
25
+
26
+ === Videos:
27
+
28
+ require 'mtv-music'
29
+ include MTV::Music
30
+
31
+ video = Video.new("hznHivqrbHHZBZNXB") # Radiohead's "There There"
32
+
33
+ puts video.title
34
+ puts video.artist.name
35
+ puts video.released_on
36
+
37
+ # The MTV Music API makes it easy to embed videos into any webpage
38
+ puts video.embed_code
39
+ # <embed src="http://media.mtvnservices.com/mgid:uma:video:api.mtvnservices.com:202930"
40
+ # width="448" height="366" type="application/x-shockwave-flash"
41
+ # allowFullScreen="true" allowScriptAccess="always" flashvars="autoPlay=false" />
42
+
43
+
44
+ == REQUIREMENTS:
45
+
46
+ mtv-music has the following gem dependencies:
47
+
48
+ * Hpricot >= 0.6
49
+ * ActiveSupport >= 2.1.0
50
+ * FlexMock >= 0.8.2
51
+
52
+ == LICENSE:
53
+
54
+ (The MIT License)
55
+
56
+ Copyright (c) 2008 Mattt Thompson
57
+
58
+ Permission is hereby granted, free of charge, to any person obtaining
59
+ a copy of this software and associated documentation files (the
60
+ 'Software'), to deal in the Software without restriction, including
61
+ without limitation the rights to use, copy, modify, merge, publish,
62
+ distribute, sublicense, and/or sell copies of the Software, and to
63
+ permit persons to whom the Software is furnished to do so, subject to
64
+ the following conditions:
65
+
66
+ The above copyright notice and this permission notice shall be
67
+ included in all copies or substantial portions of the Software.
68
+
69
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
70
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
71
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
72
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
73
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
74
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
75
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,20 @@
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 'mtv-music/base'
8
+ require 'mtv-music/version'
9
+
10
+ require 'mtv-music/artist'
11
+ require 'mtv-music/video'
12
+
13
+ module MTV
14
+ module Music
15
+ API_VERSION = '1'
16
+ API_URL = "http://api.mtvnservices.com/#{API_VERSION}/"
17
+
18
+ MTV::Music::Base::connection = REST::Connection.new(API_URL, :rate_limit => 0.68)
19
+ end
20
+ end
@@ -0,0 +1,18 @@
1
+ module MTV
2
+ module Music
3
+ class Artist < Base
4
+ attribute :name, String, :matcher => "author/name"
5
+ attribute :thumbnail_url, String, :matcher => "media:thumbnail", :attribute => "url"
6
+
7
+ def videos
8
+ xml = Video.fetch_and_parse(self.class.api_path(:videos, self.id))
9
+ return xml.search("entry/id").collect{|elem| Video.new(Video.fetch_and_parse(elem.inner_text))}
10
+ end
11
+
12
+ def related
13
+ xml = Artist.fetch_and_parse(self.class.api_path(:related, self.id))
14
+ return xml.search("entry/id").collect{|elem| Artist.new(Artist.fetch_and_parse(elem.inner_text))}
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,140 @@
1
+ module MTV
2
+ module Music
3
+ class Base
4
+ class << self
5
+
6
+ cattr_accessor :attributes
7
+ cattr_accessor :connection
8
+
9
+ def attribute(*args)
10
+ @attributes ||= {}
11
+
12
+ options = args.extract_options!
13
+ name, type = args
14
+ class_eval %(attr_accessor :#{name})
15
+ @attributes[name] = options.update({:type => type})
16
+ end
17
+
18
+ def attributes
19
+ @attributes || {}
20
+ end
21
+
22
+ def name_with_demodulization
23
+ self.name_without_demodulization.demodulize
24
+ end
25
+
26
+ alias_method_chain :name, :demodulization
27
+
28
+ def fetch_and_parse(resource, options = {})
29
+ return Hpricot::XML(connection.get(resource, options))
30
+ end
31
+
32
+ def api_path(method, id = nil)
33
+ parameters = [self.name, id, method].compact
34
+ return parameters.collect!{|param| CGI::escape(param.to_s).downcase}.join('/')
35
+ end
36
+
37
+ # Generic Feed Parameters
38
+
39
+ # feed-format Specifies the format of the feed to be returned.
40
+ # value: atom, mrss
41
+ # example: ?feed-format=mrss
42
+ # max-results Limit the maximum number of results. The default value is 25. The maximum value is 100
43
+ # value: 25, 1-100
44
+ # example: ?max-results=25
45
+ # start-index Choose the first result to display in the returns. The first return is 1.
46
+ # When used with the max-results parameter, multiple pages of results can be created.
47
+ # value: 1, %d
48
+ # example: ?start-index=26
49
+ # date Limit the returns to date range specified.
50
+ # value: MMDDYYYY-MMDDYYYY
51
+ # example: ?date=01011980-12311989
52
+ # sort Specifies the sort order for returns.
53
+ # value: relevance, date_ascending, date_descending
54
+ # example: ?sort=date_ascending
55
+ def search(term, options = {})
56
+ options = options.update({:term => term})
57
+ case date = options[:date]
58
+ when Range
59
+ options[:date] = "%s-%s" % [date.first, date.last].collect{|d| d.strftime("%m%d%Y")}
60
+ when Time
61
+ options[:date] = "%s-%s" % [date, Time.now].collect{|d| d.strftime("%m%d%Y")}
62
+ end
63
+
64
+ xml = fetch_and_parse(api_path(:search), options)
65
+ results = xml.search(:entry).collect{|elem| self.new(fetch_and_parse((elem/'id').inner_text))}
66
+ return results.delete_if{|e| e.uri.blank?}
67
+ end
68
+ end
69
+
70
+ attr_accessor :id, :uri
71
+
72
+ def initialize(xml)
73
+ raise ArgumentError unless xml.kind_of?(Hpricot)
74
+
75
+ if xml.at(:uri).inner_text == "http://api.mtvnservices.com/1/"
76
+ raise MTVNetworkServiceError, xml.at(:title).inner_text, xml.at(:content).inner_text
77
+ end
78
+
79
+ @uri = xml.at(:id).inner_text
80
+ @id = @uri.split('/').last
81
+
82
+ self.class.attributes.each do |attribute, options|
83
+ element = xml.search(options[:matcher]).first
84
+ next if element.nil?
85
+ value = options[:attribute] ? element.attributes[options[:attribute]] : element.inner_text# rescue nil
86
+ begin
87
+ if options[:type] == Integer
88
+ value = value.to_i
89
+ elsif options[:type] == Float
90
+ value = value.to_f
91
+ elsif options[:type] == Date
92
+ value = Date.parse(value) rescue nil
93
+ end
94
+ ensure
95
+ self.instance_variable_set("@#{attribute}", value)
96
+ end
97
+ end
98
+
99
+ end
100
+
101
+ def initialize_with_polymorphism(arg)
102
+ case arg
103
+ when String
104
+ if arg =~ /http:\/\//
105
+ initialize_without_polymorphism(query_by_uri(arg))
106
+ else
107
+ initialize_without_polymorphism(query_by_string(arg))
108
+ end
109
+ when Hpricot
110
+ initialize_without_polymorphism(arg)
111
+ end
112
+ end
113
+
114
+ alias_method_chain :initialize, :polymorphism
115
+
116
+ protected
117
+
118
+ def query_by_string(string)
119
+ xml = self.class.fetch_and_parse(self.class.api_path(nil, string.gsub(/\s+/, '_')))
120
+ return xml.at(:entry)
121
+ end
122
+
123
+ def query_by_uri(uri)
124
+ xml = self.class.fetch_and_parse(uri)
125
+ return xml.at(:entry)
126
+ end
127
+
128
+ def query_association_by_id(association, id)
129
+ klass = "mtv/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 MTVNetworkServiceError < StandardError; end
136
+
137
+ class Artist < Base; end
138
+ class Video < Base; end
139
+ end
140
+ end
@@ -0,0 +1,11 @@
1
+ module MTV
2
+ module Music
3
+ module VERSION #:nodoc:
4
+ MAJOR = 0
5
+ MINOR = 1
6
+ TINY = 0
7
+
8
+ STRING = [MAJOR, MINOR, TINY].join('.')
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,68 @@
1
+ module MTV
2
+ module Music
3
+ class Video < Base
4
+ attribute :media_uri, String, :matcher => "media:content", :attribute => "url"
5
+ attribute :artist_uri, String, :matcher => "author/uri"
6
+ attribute :title, String, :matcher => "media:title"
7
+ attribute :description, String, :matcher => "media:description"
8
+ attribute :director, String, :matcher => "media:credit[@role^='director']"
9
+ attribute :released_on, Date, :matcher => "published"
10
+ attribute :updated_on, Date, :matcher => "updated"
11
+
12
+ # <embed src="http://media.mtvnservices.com/mgid:uma:video:api.mtvnservices.com:235854"
13
+ # width="448" height="366" type="application/x-shockwave-flash"
14
+ # allowFullScreen="true" allowScriptAccess="always" flashvars="autoPlay=false" />
15
+ def embed_code(options = {})
16
+ options.update({
17
+ :src => @media_uri,
18
+ :type => "application/x-shockwave-flash",
19
+ :width => "448",
20
+ :height => "366",
21
+ :allowFullScreen => "true",
22
+ :allowScriptAccess => "always",
23
+ :flashvars => "autoPlay=false"})
24
+ tag(:embed, options)
25
+ end
26
+
27
+ def artist
28
+ Artist.new(@artist_uri)
29
+ end
30
+
31
+ alias_method :to_html, :embed_code
32
+
33
+ class << self
34
+ # Workaround for case-sensitivity for Video aliases
35
+ def api_path(method, id = nil)
36
+ parameters = [self.name.downcase, id, method].compact
37
+ return parameters.collect!{|param| CGI::escape(param.to_s)}.join('/')
38
+ end
39
+ end
40
+
41
+ # Taken from ActionView#TagHelper
42
+ def tag(name, options = nil, open = false, escape = true)
43
+ "<#{name}#{tag_options(options, escape) if options}#{open ? ">" : " />"}"
44
+ end
45
+
46
+ def tag_options(options, escape = true)
47
+ unless options.blank?
48
+ attrs = []
49
+ if escape
50
+ options.each do |key, value|
51
+ next unless value
52
+ key = key.to_s
53
+ value = Set.new(%w(disabled readonly multiple)).include?(key) ? key : escape_once(value)
54
+ attrs << %(#{key}="#{value}")
55
+ end
56
+ else
57
+ attrs = options.map { |key, value| %(#{key}="#{value}") }
58
+ end
59
+ " #{attrs.sort * ' '}" unless attrs.empty?
60
+ end
61
+ end
62
+
63
+ def escape_once(html)
64
+ html.to_s.gsub(/[\"><]|&(?!([a-zA-Z]+|(#\d+));)/) { |special| ERB::Util::HTML_ESCAPE[special] }
65
+ end
66
+ end
67
+ end
68
+ end
@@ -0,0 +1,60 @@
1
+ # Ported from John Nunemaker's Scrobbler Gem (http://scrobbler.rubyforge.org/)
2
+
3
+ require 'net/https'
4
+ require 'activesupport'
5
+
6
+ module REST
7
+ class Connection
8
+ def initialize(base_url, options = {})
9
+ @base_url = base_url
10
+ @username = options[:username]
11
+ @password = options[:password]
12
+
13
+ @rate_limit = options[:rate_limit].to_f
14
+ @lock_expiry = Time.now.to_f
15
+ end
16
+
17
+ def get(resource, args = nil)
18
+ request(resource, "get", args)
19
+ end
20
+
21
+ def post(resource, args = nil)
22
+ request(resource, "post", args)
23
+ end
24
+
25
+ def request(resource, method = "get", args = {})
26
+ url = URI.join(@base_url, resource)
27
+
28
+ unless args.empty?
29
+ # TODO: What about keys without value?
30
+ url.query = args.map { |k,v| "%s=%s" % [URI.encode(k.to_s), URI.encode(v.to_s)] }.join("&")
31
+ end
32
+
33
+ case method
34
+ when "get"
35
+ req = Net::HTTP::Get.new(url.request_uri)
36
+ when "post"
37
+ req = Net::HTTP::Post.new(url.request_uri)
38
+ end
39
+
40
+ if @username and @password
41
+ req.basic_auth(@username, @password)
42
+ end
43
+
44
+ http = Net::HTTP.new(url.host, url.port)
45
+ http.use_ssl = (url.port == 443)
46
+
47
+ res = http.start() { |conn| conn.request(req) }
48
+ puts url
49
+ res.body
50
+ end
51
+
52
+ def request_with_rate_limiting(*args)
53
+ sleep @lock_expiry - Time.now.to_f rescue ArgumentError
54
+ @lock_expiry = Time.now.to_f + @rate_limit
55
+ request_without_rate_limiting(*args)
56
+ end
57
+
58
+ alias_method_chain :request, :rate_limiting
59
+ end
60
+ end
@@ -0,0 +1,34 @@
1
+ Gem::Specification.new do |s|
2
+ s.name = "mtv-music"
3
+ s.version = "0.1.1"
4
+ s.date = "2008-11-13"
5
+ s.summary = "A Ruby wrapper for the MTV Music API."
6
+ s.email = "kurt@karmalab.org"
7
+ s.homepage = "http://github.com/kschrader/mtv-music/"
8
+ s.description = "A Ruby wrapper for the MTV Music API.
9
+ See http://developer.mtvnservices.com/ for more information about the API."
10
+ s.authors = ["Mattt Thompson", "Kurt Schrader"]
11
+
12
+ s.files = [
13
+ "README",
14
+ "mtv-music.gemspec",
15
+ "lib/mtv-music.rb",
16
+ "lib/rest.rb",
17
+ "lib/mtv-music/base.rb",
18
+ "lib/mtv-music/artist.rb",
19
+ "lib/mtv-music/video.rb",
20
+ "lib/mtv-music/version.rb"
21
+ ]
22
+ s.test_files = [
23
+ "test/test_helper.rb",
24
+ "test/test_mtv_music_artist.rb",
25
+ "test/test_mtv_music_video.rb"
26
+ ]
27
+
28
+ s.add_dependency("hpricot", ["> 0.6"])
29
+ s.add_dependency("activesupport", ["> 2.1.0"])
30
+ s.add_dependency("flexmock", ["> 0.8.2"])
31
+
32
+ s.has_rdoc = false
33
+ s.rdoc_options = ["--main", "README"]
34
+ end
@@ -0,0 +1,12 @@
1
+ require 'rubygems'
2
+ gem 'flexmock'
3
+
4
+ require 'test/unit'
5
+ require 'flexmock/test_unit'
6
+ require File.dirname(__FILE__) + '/../lib/mtv-music'
7
+
8
+ include MTV::Music
9
+
10
+ def fixture(_filename)
11
+ File.open(File.dirname(__FILE__) + '/fixtures/%s.xml' % _filename ).read
12
+ end
@@ -0,0 +1,62 @@
1
+ require File.dirname(__FILE__) + '/test_helper.rb'
2
+
3
+ class TestMTVMusicArtist < Test::Unit::TestCase
4
+ def test_artist_initialization_from_alias
5
+ flexmock(Artist).should_receive(:fetch_and_parse).
6
+ once.with("artist/radiohead").
7
+ and_return(Hpricot::XML(fixture(:radiohead)))
8
+
9
+ assert_nothing_raised do
10
+ @artist = Artist.new("Radiohead")
11
+ end
12
+ end
13
+
14
+ def test_artist_initialization_from_alias_with_whitespace
15
+ flexmock(Artist).should_receive(:fetch_and_parse).
16
+ once.with("artist/nine_inch_nails").
17
+ and_return(Hpricot::XML(fixture(:nine_inch_nails)))
18
+
19
+ assert_nothing_raised do
20
+ @artist = Artist.new("Nine Inch Nails")
21
+ end
22
+ end
23
+
24
+ def test_artist_no_such_alias_error
25
+ flexmock(Artist).should_receive(:fetch_and_parse).
26
+ once.with("artist/sailing_three_tuesdays").
27
+ and_return(Hpricot::XML(fixture(:sailing_three_tuesdays)))
28
+
29
+ assert_raise MTVNetworkServiceError do
30
+ @artist = Artist.new("Sailing Three Tuesdays")
31
+ end
32
+ end
33
+
34
+ def test_artist_class_attributes_and_associations
35
+ flexmock(Artist).should_receive(:fetch_and_parse).
36
+ once.with("artist/radiohead").
37
+ and_return(Hpricot::XML(fixture(:radiohead)))
38
+
39
+ assert_nothing_raised do
40
+ @artist = Artist.new("Radiohead")
41
+ end
42
+
43
+ assert ! Artist.attributes.empty?
44
+ Artist.attributes.keys.each do |attribute|
45
+ assert_respond_to @artist, attribute
46
+ end
47
+ end
48
+
49
+ def test_artist_instance_variables
50
+ flexmock(Artist).should_receive(:fetch_and_parse).
51
+ once.with("artist/radiohead").
52
+ and_return(Hpricot::XML(fixture(:radiohead)))
53
+
54
+ assert_nothing_raised do
55
+ @artist = Artist.new("Radiohead")
56
+ end
57
+
58
+ assert_equal @artist.id, "radiohead"
59
+ assert_equal @artist.uri, "http://api.mtvnservices.com/1/artist/radiohead/"
60
+ assert_equal @artist.name, "Radiohead"
61
+ end
62
+ end
@@ -0,0 +1,52 @@
1
+ require File.dirname(__FILE__) + '/test_helper.rb'
2
+
3
+ class TestYahooMusicRelease < Test::Unit::TestCase
4
+ def test_video_initialization_from_alias
5
+ flexmock(Video).should_receive(:fetch_and_parse).
6
+ once.with("video/hznHivqrbHHZBZNXB").
7
+ and_return(Hpricot::XML(fixture(:there_there)))
8
+
9
+ assert_nothing_raised do
10
+ @video = Video.new("hznHivqrbHHZBZNXB")
11
+ end
12
+ end
13
+
14
+ def test_artist_nonexistent_id_error
15
+ flexmock(Video).should_receive(:fetch_and_parse).
16
+ once.with("video/nil").
17
+ and_return(Hpricot::XML(fixture(:nil_video)))
18
+
19
+ assert_raise MTVNetworkServiceError do
20
+ @artist = Video.new("nil")
21
+ end
22
+ end
23
+
24
+ def test_video_class_attributes_and_associations
25
+ flexmock(Video).should_receive(:fetch_and_parse).
26
+ once.with("video/hznHivqrbHHZBZNXB").
27
+ and_return(Hpricot::XML(fixture(:there_there)))
28
+
29
+ assert_nothing_raised do
30
+ @video = Video.new("hznHivqrbHHZBZNXB")
31
+ end
32
+
33
+ assert ! Video.attributes.empty?
34
+ Video.attributes.keys.each do |attribute|
35
+ assert_respond_to @video, attribute
36
+ end
37
+ end
38
+
39
+ def test_video_instance_variables
40
+ flexmock(Video).should_receive(:fetch_and_parse).
41
+ once.with("video/hznHivqrbHHZBZNXB").
42
+ and_return(Hpricot::XML(fixture(:there_there)))
43
+
44
+ assert_nothing_raised do
45
+ @video = Video.new("hznHivqrbHHZBZNXB")
46
+ end
47
+
48
+ assert_equal @video.id, "hznHivqrbHHZDPSP"
49
+ assert_equal @video.title, "There There"
50
+ assert_equal @video.director, "Chris Hopewell"
51
+ end
52
+ end
metadata ADDED
@@ -0,0 +1,90 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: kschrader-mtv-music
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - Mattt Thompson
8
+ - Kurt Schrader
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2008-11-13 00:00:00 -08:00
14
+ default_executable:
15
+ dependencies:
16
+ - !ruby/object:Gem::Dependency
17
+ name: hpricot
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">"
22
+ - !ruby/object:Gem::Version
23
+ version: "0.6"
24
+ version:
25
+ - !ruby/object:Gem::Dependency
26
+ name: activesupport
27
+ version_requirement:
28
+ version_requirements: !ruby/object:Gem::Requirement
29
+ requirements:
30
+ - - ">"
31
+ - !ruby/object:Gem::Version
32
+ version: 2.1.0
33
+ version:
34
+ - !ruby/object:Gem::Dependency
35
+ name: flexmock
36
+ version_requirement:
37
+ version_requirements: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - ">"
40
+ - !ruby/object:Gem::Version
41
+ version: 0.8.2
42
+ version:
43
+ description: A Ruby wrapper for the MTV Music API. See http://developer.mtvnservices.com/ for more information about the API.
44
+ email: kurt@karmalab.org
45
+ executables: []
46
+
47
+ extensions: []
48
+
49
+ extra_rdoc_files: []
50
+
51
+ files:
52
+ - README
53
+ - mtv-music.gemspec
54
+ - lib/mtv-music.rb
55
+ - lib/rest.rb
56
+ - lib/mtv-music/base.rb
57
+ - lib/mtv-music/artist.rb
58
+ - lib/mtv-music/video.rb
59
+ - lib/mtv-music/version.rb
60
+ has_rdoc: false
61
+ homepage: http://github.com/kschrader/mtv-music/
62
+ post_install_message:
63
+ rdoc_options:
64
+ - --main
65
+ - README
66
+ require_paths:
67
+ - lib
68
+ required_ruby_version: !ruby/object:Gem::Requirement
69
+ requirements:
70
+ - - ">="
71
+ - !ruby/object:Gem::Version
72
+ version: "0"
73
+ version:
74
+ required_rubygems_version: !ruby/object:Gem::Requirement
75
+ requirements:
76
+ - - ">="
77
+ - !ruby/object:Gem::Version
78
+ version: "0"
79
+ version:
80
+ requirements: []
81
+
82
+ rubyforge_project:
83
+ rubygems_version: 1.2.0
84
+ signing_key:
85
+ specification_version: 2
86
+ summary: A Ruby wrapper for the MTV Music API.
87
+ test_files:
88
+ - test/test_helper.rb
89
+ - test/test_mtv_music_artist.rb
90
+ - test/test_mtv_music_video.rb