acts_as_unvlogable_2 1.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,11 @@
1
+ require "acts_as_unvlogable/vg_youtube"
2
+
3
+ class VgYoutu < VgYoutube
4
+ def initialize(url=nil, options={})
5
+ url = URI(url)
6
+ url.host = 'www.youtube.com'
7
+ url.query = "#{url.query}&v=#{url.path[1..-1]}"
8
+ url.path = '/watch'
9
+ super(url.to_s, options)
10
+ end
11
+ end
@@ -0,0 +1,67 @@
1
+ # ----------------------------------------------
2
+ # Class for Youtube (youtube.com)
3
+ # http://www.youtube.com/watch?v=25AsfkriHQc
4
+ # ----------------------------------------------
5
+
6
+ class VgYoutube
7
+
8
+ def initialize(url=nil, options={})
9
+ object = YouTubeIt::Client.new({})
10
+ @url = url
11
+ @video_id = @url.query_param('v')
12
+ begin
13
+ @details = object.video_by(@video_id)
14
+ raise if @details.blank?
15
+ raise ArgumentError, "Embedding disabled by request" unless @details.embeddable?
16
+ @details.instance_variable_set(:@noembed, false)
17
+ rescue
18
+ raise ArgumentError, "Unsuported url or service"
19
+ end
20
+ end
21
+
22
+ def title
23
+ @details.title
24
+ end
25
+
26
+ def thumbnail
27
+ @details.thumbnails.first.url
28
+ end
29
+
30
+ def duration
31
+ @details.duration
32
+ end
33
+
34
+ def embed_url
35
+ @details.media_content.first.url if @details.noembed == false
36
+ end
37
+
38
+ # options
39
+ # You can read more about the youtube player options in
40
+ # http://code.google.com/intl/en/apis/youtube/player_parameters.html
41
+ # Use them in options (ex {:rel => 0, :color1 => '0x333333'})
42
+ #
43
+ def embed_html(width=425, height=344, options={}, params={})
44
+ "<object width='#{width}' height='#{height}'><param name='movie' value='#{embed_url}#{options.map {|k,v| "&#{k}=#{v}"}}'></param><param name='allowFullScreen' value='true'></param><param name='allowscriptaccess' value='always'></param>#{params.map{|k,v|"<param name='#{k}' value='#{v}'></param>"}.join}<embed src='#{embed_url}#{options.map {|k,v| "&#{k}=#{v}"}}' type='application/x-shockwave-flash' allowscriptaccess='always' allowfullscreen='true' width='#{width}' height='#{height}' #{params.map {|k,v| "#{k}=#{v}"}.join(" ")}></embed></object>" if @details.noembed == false
45
+ end
46
+
47
+
48
+ def flv
49
+ doc = URI::parse("http://www.youtube.com/get_video_info?&video_id=#{@video_id}").read
50
+ CGI::unescape(doc.split("&url_encoded_fmt_stream_map=")[1]).split("url=").each do |u|
51
+ u = CGI::unescape(u)
52
+ unless u.index("x-flv").nil?
53
+ return u.split("&quality").first
54
+ break
55
+ end
56
+ end
57
+ end
58
+
59
+ def download_url
60
+ flv
61
+ end
62
+
63
+ def service
64
+ "Youtube"
65
+ end
66
+
67
+ end
@@ -0,0 +1,392 @@
1
+ # encoding: utf-8
2
+ require 'test/unit'
3
+
4
+ $LOAD_PATH << File.dirname(__FILE__) + '/../lib'
5
+ # Main class
6
+ require 'acts_as_unvlogable'
7
+ # Gems & other herbs
8
+ require 'shoulda'
9
+ require 'ruby-debug'
10
+
11
+ class ActsAsUnvlogableTest < Test::Unit::TestCase
12
+
13
+ context "Instancing UnvlogIt" do
14
+
15
+ context "without any url" do
16
+ should "raise an ArgumentError exception" do
17
+ assert_raise(ArgumentError, "We need a video url") { UnvlogIt.new }
18
+ end
19
+ end
20
+
21
+ context "with an unsupported url" do
22
+ should "raise an ArgumentError exception" do
23
+ assert_raise(ArgumentError, "Unsuported url or service") { UnvlogIt.new("http://iwannagothere.net/") }
24
+ end
25
+ end
26
+
27
+ # ----------------------------------------------------------
28
+ # Testing youtube
29
+ # ----------------------------------------------------------
30
+ context "with an existent youtube url" do
31
+ setup do
32
+ @videotron = UnvlogIt.new("http://www.youtube.com/watch?v=MVa4q-YVjD8") # => Keith Moon´s drum kit explodes
33
+ end
34
+ should "initialize a VgYoutube instance" do
35
+ assert_equal VgYoutube, @videotron.instance_values['object'].class
36
+ assert_equal "http://www.youtube.com/watch?v=MVa4q-YVjD8", @videotron.instance_values['object'].instance_values['url']
37
+ assert_equal "MVa4q-YVjD8", @videotron.instance_values['object'].instance_values['video_id']
38
+ assert_not_nil @videotron.instance_values['object'].instance_values['details']
39
+ end
40
+
41
+ should "return the video properties" do
42
+ check_video_attributes({:title => "Keith Moon´s drum kit explodes", :service => "Youtube"})
43
+ end
44
+ end
45
+
46
+ context "with an existent youtube url that can not be embedded" do
47
+ should "raise an ArgumentError" do
48
+ assert_raise(ArgumentError, "Embedding disabled by request") { UnvlogIt.new("http://www.youtube.com/watch?v=6TT19cB0NTM") }# => Oh! Yeah! by Chickenfoot from the Tonight Show w Conan O'Brien
49
+ end
50
+ end
51
+
52
+ context "with an inexistent youtube url" do
53
+ should "raise an ArgumentError" do
54
+ assert_raise(ArgumentError, "Unsuported url or service") { UnvlogIt.new("http://www.youtube.com/watch?v=inexistente") }
55
+ end
56
+ end
57
+
58
+ # ----------------------------------------------------------
59
+ # Testing metacafe
60
+ # ----------------------------------------------------------
61
+ context "with an existent metacafe url" do
62
+ setup do
63
+ @videotron = UnvlogIt.new("http://www.metacafe.com/watch/1135061/close_call_a320_caught_in_crosswinds/") # => Close Call! A320 Caught in Crosswinds
64
+ end
65
+ should "initialize a VgMetacafe instance" do
66
+ assert_equal VgMetacafe, @videotron.instance_values['object'].class
67
+ assert_equal "http://www.metacafe.com/watch/1135061/close_call_a320_caught_in_crosswinds/", @videotron.instance_values['object'].instance_values['url']
68
+ assert_equal 3, @videotron.instance_values['object'].instance_values['args'].size
69
+ assert !@videotron.instance_values['object'].instance_values['youtubed']
70
+ assert_nil @videotron.instance_values['object'].instance_values['yt']
71
+ end
72
+
73
+ should "return the video properties" do
74
+ check_video_attributes({:title => "Close call a320 caught in crosswinds", :service => "Metacafe"})
75
+ end
76
+ end
77
+
78
+ context "with an existent 'youtubed' metacafe url" do
79
+ setup do
80
+ @videotron = UnvlogIt.new("http://www.metacafe.com/watch/yt-r07zdVLOWBA/pop_rocks_and_coke_myth/") # => Close Call! A320 Caught in Crosswinds
81
+ end
82
+ should "initialize a VgMetacafe instance" do
83
+ assert_equal VgMetacafe, @videotron.instance_values['object'].class
84
+ assert_equal "http://www.metacafe.com/watch/yt-r07zdVLOWBA/pop_rocks_and_coke_myth/", @videotron.instance_values['object'].instance_values['url']
85
+ assert_equal 3, @videotron.instance_values['object'].instance_values['args'].size
86
+ assert @videotron.instance_values['object'].instance_values['youtubed']
87
+ assert "VgYoutube", @videotron.instance_values['object'].instance_values['yt'].class.to_s
88
+ end
89
+
90
+ should "return the video properties" do
91
+ check_video_attributes({:title => "Pop Rocks and Coke Myth", :service => "Metacafe"})
92
+ end
93
+ end
94
+
95
+
96
+ end
97
+
98
+
99
+ # ----------------------------------------------------------
100
+ # Testing dailymotion
101
+ # ----------------------------------------------------------
102
+ context "with a dailymotion video url" do
103
+ setup do
104
+ @videotron = UnvlogIt.new("http://www.dailymotion.com/video/x7u5kn_parkour-dayyy_sport") # => parkour dayyy
105
+ end
106
+ should "initialize a VgDailymotion instance" do
107
+ assert_equal "VgDailymotion", @videotron.instance_values['object'].class.to_s
108
+ assert_equal "http://www.dailymotion.com/video/x7u5kn_parkour-dayyy_sport", @videotron.instance_values['object'].instance_values['url']
109
+ assert_equal "x7u5kn_parkour-dayyy_sport", @videotron.instance_values['object'].instance_values['video_id']
110
+ assert_not_nil @videotron.instance_values['object'].instance_values['feed']
111
+ end
112
+
113
+ should "return the video properties" do
114
+ check_video_attributes({:title => "parkour dayyy", :service => "Dailymotion"})
115
+ end
116
+ end
117
+
118
+
119
+
120
+ # ----------------------------------------------------------
121
+ # Testing collegehumor
122
+ # ----------------------------------------------------------
123
+ context "with a collegehumor video url" do
124
+ setup do
125
+ @videotron = UnvlogIt.new("http://www.collegehumor.com/video/3005349/brohemian-rhapsody") # => Brohemian Rhapsody
126
+ end
127
+ should "initialize a VgCollegehumor instance" do
128
+ assert_equal "VgCollegehumor", @videotron.instance_values['object'].class.to_s
129
+ assert_equal "http://www.collegehumor.com/video/3005349/brohemian-rhapsody", @videotron.instance_values['object'].instance_values['url']
130
+ assert_equal "3005349", @videotron.instance_values['object'].instance_values['video_id']
131
+ assert_not_nil @videotron.instance_values['object'].instance_values['feed']
132
+ end
133
+
134
+ should "return the video properties" do
135
+ check_video_attributes({:title => "Brohemian Rhapsody", :service => "CollegeHumor"})
136
+ end
137
+ end
138
+
139
+
140
+ # ----------------------------------------------------------
141
+ # Testing blip.tv
142
+ # ----------------------------------------------------------
143
+ context "with a blip.tv video url" do
144
+ setup do
145
+ @videotron = UnvlogIt.new("http://blip.tv/file/678407/") # => Toy Break 26 : Adult Toys
146
+ end
147
+ should "initialize a VgBlip instance" do
148
+ assert_equal "VgBlip", @videotron.instance_values['object'].class.to_s
149
+ assert_equal "http://blip.tv/file/678407/", @videotron.instance_values['object'].instance_values['url']
150
+ assert_not_nil @videotron.instance_values['object'].instance_values['feed']
151
+ end
152
+
153
+ should "return the video properties" do
154
+ check_video_attributes({:title => "Toy Break 26 : Adult Toys", :service => "Blip.tv"})
155
+ end
156
+ end
157
+
158
+
159
+ # ----------------------------------------------------------
160
+ # Testing vids.myspace.com
161
+ # ----------------------------------------------------------
162
+ context "with a vids.myspace.com video url" do
163
+ setup do
164
+ @videotron = UnvlogIt.new("http://vids.myspace.com/index.cfm?fuseaction=vids.individual&VideoID=27111431") # => rocabilis
165
+ end
166
+ should "initialize a VgMyspace instance" do
167
+ assert_equal "VgMyspace", @videotron.instance_values['object'].class.to_s
168
+ assert_equal "http://vids.myspace.com/index.cfm?fuseaction=vids.individual&VideoID=27111431", @videotron.instance_values['object'].instance_values['url']
169
+ assert_equal "27111431", @videotron.instance_values['object'].instance_values['video_id']
170
+ assert_not_nil @videotron.instance_values['object'].instance_values['feed']
171
+ end
172
+
173
+ should "return the video properties" do
174
+ check_video_attributes({:title => "rocabilis", :service => "Myspace"})
175
+ end
176
+ end
177
+
178
+
179
+ # ----------------------------------------------------------
180
+ # Testing 11870.com
181
+ # ----------------------------------------------------------
182
+ context "with an 11870.com video url" do
183
+ setup do
184
+ @videotron = UnvlogIt.new("http://11870.com/pro/chic-basic-born/media/b606abfe") # => Chic & Basic Born
185
+ end
186
+ should "initialize a Vg11870 instance" do
187
+ assert_equal "Vg11870", @videotron.instance_values['object'].class.to_s
188
+ assert_equal "http://11870.com/pro/chic-basic-born/media/b606abfe", @videotron.instance_values['object'].instance_values['url']
189
+ assert_not_nil @videotron.instance_values['object'].instance_values['page']
190
+ assert_not_nil @videotron.instance_values['object'].instance_values['flashvars']
191
+ end
192
+
193
+ should "return the video properties" do
194
+ check_video_attributes({:title => "Chic & Basic Born", :service => "11870.com"})
195
+ end
196
+ end
197
+
198
+
199
+ # ----------------------------------------------------------
200
+ # Testing dalealplay.com
201
+ # ----------------------------------------------------------
202
+ context "with a dalealplay.com video url" do
203
+ setup do
204
+ @videotron = UnvlogIt.new("http://www.dalealplay.com/informaciondecontenido.php?con=80280") # => Camelos Semos Jonathan Tú si que vales
205
+ end
206
+ should "initialize a VgDalealplay instance" do
207
+ assert_equal "VgDalealplay", @videotron.instance_values['object'].class.to_s
208
+ assert_equal "http://www.dalealplay.com/informaciondecontenido.php?con=80280", @videotron.instance_values['object'].instance_values['url']
209
+ assert_equal "80280", @videotron.instance_values['object'].instance_values['video_id']
210
+ assert_not_nil @videotron.instance_values['object'].instance_values['page']
211
+ end
212
+
213
+ should "return the video properties" do
214
+ check_video_attributes({:title => "Camelos.Semos. Jonathan. Tú si que vales.", :service => "dalealplay"})
215
+ end
216
+ end
217
+
218
+
219
+
220
+ # ----------------------------------------------------------
221
+ # Testing flickr.com
222
+ # ----------------------------------------------------------
223
+ context "with a flickr.com video url" do
224
+ setup do
225
+ @videotron = UnvlogIt.new("http://www.flickr.com/photos/jerovital/4152225414/", {:key => "065b2eff5e604e2a408c01af1f27a982" }) # => la primera vela
226
+ end
227
+ should "initialize a VgFlickr instance" do
228
+ assert_equal "VgFlickr", @videotron.instance_values['object'].class.to_s
229
+ assert_equal "http://www.flickr.com/photos/jerovital/4152225414/", @videotron.instance_values['object'].instance_values['url']
230
+ assert_equal "4152225414", @videotron.instance_values['object'].instance_values['video_id']
231
+ assert_not_nil @videotron.instance_values['object'].instance_values['details']
232
+ end
233
+
234
+ should "return the video properties" do
235
+ check_video_attributes({:title => "flipando en los columpios", :service => "Flickr"})
236
+ end
237
+ end
238
+
239
+
240
+ # ----------------------------------------------------------
241
+ # Testing qik.com
242
+ # ----------------------------------------------------------
243
+ context "with a qik.com video url" do
244
+ setup do
245
+ @videotron = UnvlogIt.new("http://qik.com/video/340982") # => Honolulu Day 8: USS Arizona at Pearl Harbor
246
+ end
247
+ should "initialize a VgQik instance" do
248
+ assert_equal "VgQik", @videotron.instance_values['object'].class.to_s
249
+ assert_equal "http://qik.com/video/340982", @videotron.instance_values['object'].instance_values['url']
250
+ assert_equal "340982", @videotron.instance_values['object'].instance_values['video_id']
251
+ assert_not_nil @videotron.instance_values['object'].instance_values['page']
252
+ end
253
+
254
+ should "return the video properties" do
255
+ check_video_attributes({:title => "Honolulu Day 8: USS Arizona at Pearl Harbor", :service => "Qik"})
256
+ end
257
+ end
258
+
259
+
260
+
261
+ # ----------------------------------------------------------
262
+ # Testing www.marca.tv
263
+ # ----------------------------------------------------------
264
+ context "with a www.marca.tv video url" do
265
+ setup do
266
+ @videotron = UnvlogIt.new("http://www.marca.com/tv/?v=DN23wG8c1Rj") # => Pau entra por la puerta grande en el club de los 10.000
267
+ end
268
+ should "initialize a VgMarca instance" do
269
+ assert_equal "VgMarca", @videotron.instance_values['object'].class.to_s
270
+ assert_equal "http://www.marca.com/tv/?v=DN23wG8c1Rj", @videotron.instance_values['object'].instance_values['url']
271
+ assert_equal "DN23wG8c1Rj", @videotron.instance_values['object'].instance_values['video_id']
272
+ assert_not_nil @videotron.instance_values['object'].instance_values['feed']
273
+ end
274
+
275
+ should "return the video properties" do
276
+ check_video_attributes({:title => "Pau entra por la puerta grande en el club de los 10.000", :service => "Marca.tv"})
277
+ end
278
+ end
279
+
280
+
281
+
282
+ # ----------------------------------------------------------
283
+ # Testing ted talks
284
+ # ----------------------------------------------------------
285
+ context "with a ted talks video url" do
286
+ setup do
287
+ @videotron = UnvlogIt.new("http://www.ted.com/talks/benjamin_wallace_on_the_price_of_happiness.html") # => Benjamin Wallace: Does happiness have a price tag?
288
+ end
289
+ should "initialize a VgTed instance" do
290
+ assert_equal "VgTed", @videotron.instance_values['object'].class.to_s
291
+ assert_equal "http://www.ted.com/talks/benjamin_wallace_on_the_price_of_happiness.html", @videotron.instance_values['object'].instance_values['url']
292
+ assert_not_nil @videotron.instance_values['object'].instance_values['page']
293
+ assert_not_nil @videotron.instance_values['object'].instance_values['flashvars']
294
+ assert_not_nil @videotron.instance_values['object'].instance_values['args']
295
+ end
296
+
297
+ should "return the video properties" do
298
+ check_video_attributes({:title => "Benjamin Wallace: The price of happiness", :service => "Ted Talks"})
299
+ end
300
+ end
301
+
302
+ context "with an invalid ted talks video url" do
303
+ should "raise an ArgumentError exception" do
304
+ assert_raise(ArgumentError, "Unsuported url or service") { UnvlogIt.new("http://www.ted.com/index.php/wadus.html") }
305
+ end
306
+ end
307
+
308
+
309
+ # ----------------------------------------------------------
310
+ # Testing vimeo
311
+ # ----------------------------------------------------------
312
+ context "with a vimeo video url" do
313
+ setup do
314
+ @videotron = UnvlogIt.new("http://vimeo.com/2354261") # => People are strange
315
+ end
316
+ should "initialize a VgVimeo instance" do
317
+ assert_equal "VgVimeo", @videotron.instance_values['object'].class.to_s
318
+ assert_equal "http://vimeo.com/2354261", @videotron.instance_values['object'].instance_values['url']
319
+ assert_equal "2354261", @videotron.instance_values['object'].instance_values['video_id']
320
+ assert_not_nil @videotron.instance_values['object'].instance_values['feed']
321
+ end
322
+
323
+ should "return the video properties" do
324
+ check_video_attributes({:title => "People are strange", :service => "Vimeo"})
325
+ end
326
+ end
327
+
328
+ # ----------------------------------------------------------
329
+ # Testing RuTube
330
+ # ----------------------------------------------------------
331
+ context "with a rutube video url" do
332
+ setup do
333
+ @videotron = UnvlogIt.new("http://rutube.ru/tracks/1958807.html?v=56cd2f1b50a4d2b69ff455e72f2fae29") # => chipmunks!!
334
+ end
335
+ should "initialize a VgRutube instance" do
336
+ assert_equal "VgRutube", @videotron.instance_values['object'].class.to_s
337
+ assert_equal "http://rutube.ru/tracks/1958807.html?v=56cd2f1b50a4d2b69ff455e72f2fae29", @videotron.instance_values['object'].instance_values['url']
338
+ assert_equal "1958807", @videotron.instance_values['object'].instance_values['movie_id']
339
+ assert_equal "56cd2f1b50a4d2b69ff455e72f2fae29", @videotron.instance_values['object'].send(:movie_hash)
340
+ end
341
+
342
+ should "return the video properties" do
343
+ check_video_attributes({:title => "Запасливые бурундуки"})
344
+ end
345
+ end
346
+
347
+ context "with an invalid rutube video url" do
348
+ should "raise an ArgumentError exception" do
349
+ assert_raise(ArgumentError, "Unsuported url or service") { UnvlogIt.new("http://rutube.ru/tracks/abdcd.html?v=523423") }
350
+ end
351
+ end
352
+
353
+ # ----------------------------------------------------------
354
+ # Testing Prostopleer
355
+ # ----------------------------------------------------------
356
+ context "with a prostopleer url" do
357
+ setup do
358
+ @videotron = UnvlogIt.new("http://prostopleer.com/tracks/401758bI6n")
359
+ end
360
+ should "initialize a VgProstopleer instance" do
361
+ assert_equal "VgProstopleer", @videotron.instance_values['object'].class.to_s
362
+ assert_equal "http://prostopleer.com/tracks/401758bI6n", @videotron.instance_values['object'].instance_values['url']
363
+ assert_equal "401758bI6n", @videotron.instance_values['object'].instance_values['track_id']
364
+ assert_equal "Combichrist - sent to destroy", @videotron.title
365
+ end
366
+ end
367
+
368
+ context "with an invalid prostopleer url" do
369
+ should "raise an ArgumentError exception" do
370
+ assert_raise(ArgumentError) { UnvlogIt.new("http://prostopleer.com/trackszz/401758bI6n") }
371
+ end
372
+ end
373
+
374
+ protected
375
+
376
+ def check_video_attributes(options={})
377
+ assert_equal "#{options[:title]}", @videotron.title unless (options.blank? || options[:title].blank?)
378
+ assert_equal "#{options[:service]}", @videotron.service unless (options.blank? || options[:service].blank?)
379
+ assert_not_nil @videotron.thumbnail
380
+ if options.blank? || options[:noembed].blank?
381
+ assert_not_nil @videotron.embed_url
382
+ assert_not_nil @videotron.embed_html
383
+ elsif options[:noembed]
384
+ assert_nil @videotron.embed_url
385
+ assert_nil @videotron.embed_html
386
+ assert_nil @videotron.video_details[:embed_url]
387
+ assert_nil @videotron.video_details[:embed_html]
388
+ end
389
+ assert_not_nil @videotron.flv
390
+ assert_equal Hash, @videotron.video_details.class
391
+ end
392
+ end