tiegz-ruby-mtv 1.2 → 1.3

Sign up to get free protection for your applications and to get access to all the features.
data/TODO CHANGED
@@ -1,3 +1,4 @@
1
1
  - Fix edge cases where no string is passed to a #find, #browse, #search, etc. method
2
2
  - Add request cacheing
3
- - Allow fake or real requests during tests
3
+ - Allow fake or real requests during tests
4
+ - Try Nokogiri instead of Hpricot and benchmark results
data/lib/mtv.rb CHANGED
@@ -1,4 +1,5 @@
1
1
  require 'activesupport'
2
+ require 'hpricot'
2
3
  require 'open-uri'
3
4
  require 'benchmark'
4
5
 
data/lib/mtv/artist.rb CHANGED
@@ -89,9 +89,9 @@ module MTV
89
89
  protected
90
90
  # Parses a response xml string for artists.
91
91
  def parse_many(body) # :nodoc:
92
- entries = XmlSimple.xml_in(body)['entry']
93
- raise Error, "That artist not found!" if entries.nil?
92
+ entries = (Hpricot.XML(body)/:entry)
94
93
  entries = [entries] unless entries.is_a? Array
94
+ raise Error, "That artist not found!" if entries.nil?
95
95
  entries.map { |entry|
96
96
  instantiate entry
97
97
  }.reject { |artist| MTV::Artist.name.nil? || MTV::Artist.name.empty? }
@@ -99,17 +99,17 @@ module MTV
99
99
 
100
100
  # Parses a response xml string for an artist.
101
101
  def parse_one(body) # :nodoc:
102
- entry = XmlSimple.xml_in(body)
103
- name = entry['author'].first['name'].first
102
+ entry = Hpricot.XML(body)
103
+ name = (entry/'author/name').inner_html
104
104
  raise Error, "That artist not found!" if name.nil? || name.empty?
105
105
  instantiate entry
106
106
  end
107
107
 
108
108
  def instantiate(entry={})
109
- Artist.new(:name => entry['author'].first['name'].to_s,
110
- :uri => entry['author'].first['uri'].to_s,
111
- :links => entry['link'].map { |link| link['href'].to_s },
112
- :updated => entry['updated'].to_s.to_datetime)
109
+ Artist.new(:name => (entry/'author/name').inner_html,
110
+ :uri => (entry/'author/uri').inner_html,
111
+ :links => (entry/'link').map { |l| l.attributes['href'] },
112
+ :updated => (entry/'updated').inner_html)
113
113
  end
114
114
  end
115
115
 
data/lib/mtv/video.rb CHANGED
@@ -4,7 +4,7 @@ module MTV
4
4
  attr_accessor :artist, :artist_name, :artist_uri, :category, :content, :credit,
5
5
  :media_player_url, :media_thumbnails, :media_title, :media_description,
6
6
  :media_keywords, :media_duration, :media_expression, :media_url, :media_type,
7
- :media_credits, :media_category, :media_medium,
7
+ :media_credits, :media_category, :media_medium, :published, :thumbnails,
8
8
  :title, :published, :uid, :updated, :uri, :vid
9
9
 
10
10
  #-----------------
@@ -73,21 +73,19 @@ module MTV
73
73
  protected
74
74
  # Parses a response xml string for videos.
75
75
  def parse_many(body) # :nodoc:
76
- body = hack_media_enclosures(body)
77
- entries = XmlSimple.xml_in(body)['entry']
78
- raise Error, "That video not found!" if entries.nil?
76
+ entries = (Hpricot.XML(body)/:entry)
79
77
  entries = [entries] unless entries.is_a? Array
78
+ raise Error, "That video not found!" if entries.nil?
80
79
  entries.reject! { |entry| entry.nil? }
81
80
  entries.map { |entry|
82
81
  instantiate entry
83
- }
84
- end
82
+ }.reject { |artist| MTV::Artist.name.nil? || MTV::Artist.name.empty? }
83
+ end
85
84
 
86
85
  # Parses a response xml string for a video.
87
86
  def parse_one(body) # :nodoc:
88
- body = hack_media_enclosures(body)
89
- entry = XmlSimple.xml_in(body)
90
- artist_name = entry['author'].first['name'].first
87
+ entry = Hpricot.XML(body)
88
+ artist_name = (entry/'author/name').inner_html
91
89
  raise Error, "That video not found!" if artist_name.nil? || artist_name.empty?
92
90
  instantiate entry
93
91
  end
@@ -99,32 +97,28 @@ module MTV
99
97
  response.strip.gsub('<?xml version="1.0" encoding="iso-8859-1"?>', '').empty? ? "" : response
100
98
  end
101
99
 
102
- # HACK because XmlSimple doesn't seem to handle enclosures, we'll replace colons with hyphens
103
- # TODO write a better regexp for that hack :\
104
- def hack_media_enclosures(body) # :nodoc:
105
- body.gsub("<media:", "<media_").gsub("</media:", "</media_")
106
- end
107
-
108
100
  def instantiate(entry={})
109
- Video.new(:title => entry['title'].to_s,
110
- :artist_uri => entry['author'].first['uri'].to_s,
111
- :artist_name => entry['author'].first['name'].to_s,
112
- :published => entry['published'].first.to_s,
113
- :content => entry['content'], # Artist Name, Video Title, and Record Label
114
- :uri => entry['link'].select { |link| link['rel'].to_s == 'self' }.first['href'].to_s,
115
- :updated => (entry['updated'].to_s.to_datetime rescue nil),
116
- :media_category => entry['media_category'].to_s, # governing agreement
117
- :media_credits => entry['media_credit'].map { |credit| "#{credit['content'] || 'N/A'} (#{credit['role']})}" }.join(', '),
118
- :media_description => entry['media_description'].to_s,
119
- :media_duration => entry['media_content'].first['duration'].to_s,
120
- :media_expression => entry['media_content'].first['expression'].to_s,
121
- :media_keywords => entry['media_keywords'].to_s,
122
- :media_medium => entry['media_content'].first['medium'].to_s,
123
- :media_player_url => entry['media_player'].first['url'].to_s,
124
- :thumbnails => entry['media_thumbnail'],
125
- :media_title => entry['media_title'].to_s,
126
- :media_type => entry['media_content'].first['type'].to_s,
127
- :media_url => entry['media_content'].first['url'].to_s)
101
+ # TODO I did encounter a bug with a search for 'creep' on the first result... it
102
+ # has a media:credit node that is self-closing, and it appears to break hpricot.
103
+ Video.new(:title => (entry/'title').inner_html, # entry['title'].to_s,
104
+ :artist_uri => (entry/'author/uri').inner_html, # entry['author'].first['uri'].to_s,
105
+ :artist_name => (entry/'author/name').inner_html, # entry['author'].first['name'].to_s,
106
+ :published => (entry/'published').inner_html, # entry['published'].first.to_s,
107
+ :content => (entry/'content').inner_html, # Artist Name, Video Title, and Record Label
108
+ :uri => (entry/'link').select { |link| link.attributes['rel'] == 'self' }.first.attributes['href'],
109
+ :updated => ((entry/'updated').inner_html.to_datetime rescue nil), # (entry['updated'].to_s.to_datetime rescue nil),
110
+ :media_category => (entry/'media:category').inner_html, # entry['media_category'].to_s, # governing agreement
111
+ :media_credits => (entry/'media:credit').map { |credit| "#{credit.inner_html || 'N/A'} (#{credit.attributes['role']})" }.join(', '), # entry['media_credit'],
112
+ :media_description => (entry/'media:description').inner_html, # entry['media_description'].to_s,
113
+ :media_duration => ((entry/'media:content').first.attributes['duration'] rescue nil), # entry['media_content'].first['duration'].to_s,
114
+ :media_expression => ((entry/'media:content').first.attributes['expression'] rescue nil), #entry['media_content'].first['expression'].to_s,
115
+ :media_keywords => (entry/'media:keywords').inner_html, #entry['media_keywords'].to_s,
116
+ :media_medium => ((entry/'media:content').first.attributes['medium'] rescue nil), #entry['media_content'].first['medium'].to_s,
117
+ :media_player_url => ((entry/'media:player').first.attributes['url'] rescue nil), #entry['media_player'].first['url'].to_s,
118
+ :thumbnails => (entry/'media:thumbnail').map { |t| Thumbnail.new(t.attributes['url'], t.attributes['width'], t.attributes['height']) }, #entry['media_thumbnail'],
119
+ :media_title => (entry/'media:title').inner_html, #entry['media_title'].to_s,
120
+ :media_type => ((entry/'media:content').first.attributes['type'] rescue nil), # entry['media_content'].first['type'].to_s,
121
+ :media_url => ((entry/'media:content').first.attributes['url'] rescue nil)) #entry['media_content'].first['url'].to_s)
128
122
  end
129
123
  end
130
124
 
@@ -144,7 +138,7 @@ module MTV
144
138
  def initialize(values={})
145
139
  super(values)
146
140
  self.uid = uri.gsub(self.class.base_url + "/video", '').delete '/' unless @uid
147
- self.vid = media_url.split(':').last
141
+ self.vid = media_url.split(':').last if media_url
148
142
  end
149
143
 
150
144
  # Returns the artist object for the video.
@@ -183,20 +177,5 @@ module MTV
183
177
  attrs = [:title, :artist_uri].map { |name| "#{name}: #{attribute_for_inspect(name)}" }.compact.join(", ")
184
178
  "#<#{self.class} #{attrs}>"
185
179
  end
186
-
187
- # Returns an array of thumbnails for this video.
188
- #
189
- # ==== Examples
190
- #
191
- # MTV::Video.search('mellow gold').first.thumbnails
192
- # > [#<MTV::Thumbnail:0x184e274 @width="70", @height="53", @url="http://www.mtv.com/shared/promoimages/bands/b/beck/video_stills/loser/loser_70x53.jpg"> ... ]
193
- def thumbnails
194
- @thumbnails
195
- end
196
-
197
- # Parses the video thumbnails and sets them into an array.
198
- def thumbnails=(val) # :nodoc:
199
- @thumbnails = (val.is_a?(Array) ? val : [val]).map { |t| Thumbnail.new(t['url'], t['width'], t['height']) unless t.nil? }
200
- end
201
180
  end
202
181
  end
data/ruby-mtv.gemspec CHANGED
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = "ruby-mtv"
3
- s.version = "1.2"
3
+ s.version = "1.3"
4
4
  s.date = "2008-10-27"
5
5
  s.summary = "Ruby wrapper library for MTV api"
6
6
  s.email = "tieg.zaharia+github@gmail.com"
@@ -36,4 +36,5 @@ Gem::Specification.new do |s|
36
36
  s.rdoc_options = ["--main", "README"]
37
37
  s.extra_rdoc_files = ["README"]
38
38
  s.add_dependency("active-support", ["> 2.0.2"])
39
+ s.add_dependency("hpricot", ["> 0.6"])
39
40
  end
@@ -40,7 +40,7 @@ class TestArtist < Test::Unit::TestCase
40
40
  end
41
41
 
42
42
  def test_search_fails
43
- assert_raises(MTV::Error) { MTV::Artist.search('alexanderthegreat') }
43
+ assert MTV::Artist.search('alexanderthegreat').empty?
44
44
  end
45
45
 
46
46
  def test_videos
@@ -12,7 +12,7 @@ class TestVideo < Test::Unit::TestCase
12
12
  end
13
13
 
14
14
  def test_search_fails
15
- assert_raises(MTV::Error) { MTV::Video.search('somelongcrazynameforavideo') }
15
+ assert MTV::Video.search('somelongcrazynameforavideo').empty?
16
16
  end
17
17
 
18
18
  def test_find_succeeds
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tiegz-ruby-mtv
3
3
  version: !ruby/object:Gem::Version
4
- version: "1.2"
4
+ version: "1.3"
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tieg Zaharia
@@ -21,6 +21,15 @@ dependencies:
21
21
  - !ruby/object:Gem::Version
22
22
  version: 2.0.2
23
23
  version:
24
+ - !ruby/object:Gem::Dependency
25
+ name: hpricot
26
+ version_requirement:
27
+ version_requirements: !ruby/object:Gem::Requirement
28
+ requirements:
29
+ - - ">"
30
+ - !ruby/object:Gem::Version
31
+ version: "0.6"
32
+ version:
24
33
  description: ruby-mtv is a Ruby library for accessing Artist and Video objects through the MTVN api.
25
34
  email: tieg.zaharia+github@gmail.com
26
35
  executables: []