youtube_addy 0.0.0 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (2) hide show
  1. data/lib/youtube_addy.rb +25 -2
  2. metadata +1 -1
@@ -1,5 +1,28 @@
1
1
  class YouTubeAddy
2
- def self.hi
3
- puts "Hello world!"
2
+ URL_Formats = {
3
+ regular: /^(https?:\/\/)?(www\.)?youtube.com\/watch\?v=([^&]+)/,
4
+ shortened: /^(https?:\/\/)?(www\.)?youtu.be\/([^&]+)/,
5
+ invalid_chars: /[^a-zA-Z0-9\:\/\?\=\&\$\-\_\.\+\!\*\'\(\)\,]/
6
+ }
7
+
8
+ def self.has_invalid_chars?(youtube_url)
9
+ !URL_Formats[:invalid_chars].match(youtube_url).nil?
10
+ end
11
+
12
+ def self.extract_video_id(youtube_url)
13
+ return nil if has_invalid_chars?(youtube_url)
14
+
15
+ if match = URL_Formats[:regular].match(youtube_url)
16
+ return match[3]
17
+ elsif match = URL_Formats[:shortened].match(youtube_url)
18
+ return match[3]
19
+ end
20
+
21
+ nil
22
+ end
23
+
24
+ def self.youtube_embed_url(youtube_url, width = 420, height = 315)
25
+ vid_id = extract_video_id(youtube_url)
26
+ %(<iframe width="#{width}" height="#{height}" src="http://www.youtube.com/embed/#{vid_id}" frameborder="0" allowfullscreen></iframe>)
4
27
  end
5
28
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: youtube_addy
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.0
4
+ version: 1.0.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors: