easy_youtube 0.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. data/lib/easy_youtube.rb +62 -0
  2. metadata +62 -0
@@ -0,0 +1,62 @@
1
+ require "net/http"
2
+ class EasyYouTube
3
+ URL_FORMATS = {
4
+ regular: /^(https?:\/\/)?(www\.)?youtube.com\/watch\?(.*\&)?v=(?<id>[^&]+)/,
5
+ shortened: /^(https?:\/\/)?(www\.)?youtu.be\/(?<id>[^&]+)/,
6
+ embed: /^(https?:\/\/)?(www\.)?youtube.com\/embed\/(?<id>[^&]+)/,
7
+ embed_as3: /^(https?:\/\/)?(www\.)?youtube.com\/v\/(?<id>[^?]+)/,
8
+ chromeless_as3: /^(https?:\/\/)?(www\.)?youtube.com\/apiplayer\?video_id=(?<id>[^&]+)/
9
+ }
10
+
11
+ INVALID_CHARS = /[^a-zA-Z0-9\:\/\?\=\&\$\-\_\.\+\!\*\'\(\)\,]/
12
+
13
+ def self.extract_video_id(youtube_url)
14
+ return nil if has_invalid_chars?(youtube_url)
15
+ URL_FORMATS.values.each do |format_regex|
16
+ match = format_regex.match(youtube_url)
17
+ return match[:id] if match
18
+ end
19
+ end
20
+
21
+ def self.has_invalid_chars?(youtube_url)
22
+ !INVALID_CHARS.match(youtube_url).nil?
23
+ end
24
+
25
+ def self.youtube_embed_url(youtube_url, width = 420, height = 315)
26
+ video_id = extract_video_id(youtube_url)
27
+ %(<iframe width="#{width}" height="#{height}" src="http://www.youtube.com/embed/#{video_id}" frameborder="0" allowfullscreen></iframe>)
28
+ end
29
+
30
+ def self.youtube_regular_url(youtube_url)
31
+ video_id = extract_video_id(youtube_url)
32
+ "http://www.youtube.com/watch?v=#{ video_id }"
33
+ end
34
+
35
+ def self.youtube_shortened_url(youtube_url)
36
+ video_id = extract_video_id(youtube_url)
37
+ "http://youtu.be/#{ video_id }"
38
+ end
39
+
40
+ def self.valid_id?(id)
41
+ if id
42
+ response = Net::HTTP.get("gdata.youtube.com", "/feeds/api/videos/#{id}")
43
+ if ["Invalid id", "Video not found"].include? response
44
+ false
45
+ else
46
+ true
47
+ end
48
+ else
49
+ false
50
+ end
51
+ end
52
+
53
+ def self.valid_youtube_link?(youtube_url)
54
+ if has_invalid_chars?(youtube_url)
55
+ false
56
+ else
57
+ video_id = extract_video_id(youtube_url)
58
+ valid_id?(id)
59
+ end
60
+ end
61
+
62
+ end
metadata ADDED
@@ -0,0 +1,62 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: easy_youtube
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Nate Holland
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2014-01-30 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rspec
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ description: This is a simple Gem for dealing with youtube videos
31
+ email: natesholland@gmail.com
32
+ executables: []
33
+ extensions: []
34
+ extra_rdoc_files: []
35
+ files:
36
+ - lib/easy_youtube.rb
37
+ homepage: https://github.com/natesholland/easy_youtube
38
+ licenses:
39
+ - MIT
40
+ post_install_message:
41
+ rdoc_options: []
42
+ require_paths:
43
+ - lib
44
+ required_ruby_version: !ruby/object:Gem::Requirement
45
+ none: false
46
+ requirements:
47
+ - - ! '>='
48
+ - !ruby/object:Gem::Version
49
+ version: '0'
50
+ required_rubygems_version: !ruby/object:Gem::Requirement
51
+ none: false
52
+ requirements:
53
+ - - ! '>='
54
+ - !ruby/object:Gem::Version
55
+ version: '0'
56
+ requirements: []
57
+ rubyforge_project:
58
+ rubygems_version: 1.8.25
59
+ signing_key:
60
+ specification_version: 3
61
+ summary: Youtube helper functions
62
+ test_files: []