youtube_rails 0.0.1

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 (3) hide show
  1. checksums.yaml +7 -0
  2. data/lib/youtube_rails.rb +48 -0
  3. metadata +43 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: b6a9bb2682692d8f111207a232243d33252ef7dd
4
+ data.tar.gz: 4f9a440e84993013c30413c2ce7263a8ef0f168b
5
+ SHA512:
6
+ metadata.gz: 0538d62ea40ab9b8df16a4f5125dbd1b023088c2bd35805b02135bae72fc73ad1ee76f9fb9ef08a78819f04afbd3b04d8bfcd5fb474fb009e8fccf3f19d60051
7
+ data.tar.gz: 8de46b50520e658b3ec56704318b05b7016050e48cf695cd66ba91e37e1fc99b7d9e8527a5cde62adaadab6e64d196cf7f6d64df2a16b3692b5ecb7d1add0a80
@@ -0,0 +1,48 @@
1
+ class YouTubeRails
2
+ URL_FORMATS = {
3
+ regular: /^(https?:\/\/)?(www\.)?youtube.com\/watch\?(.*\&)?v=(?<id>[^&]+)/,
4
+ shortened: /^(https?:\/\/)?(www\.)?youtu.be\/(?<id>[^&]+)/,
5
+ embed: /^(https?:\/\/)?(www\.)?youtube.com\/embed\/(?<id>[^&]+)/,
6
+ embed_as3: /^(https?:\/\/)?(www\.)?youtube.com\/v\/(?<id>[^?]+)/,
7
+ chromeless_as3: /^(https?:\/\/)?(www\.)?youtube.com\/apiplayer\?video_id=(?<id>[^&]+)/
8
+ }
9
+
10
+ INVALID_CHARS = /[^a-zA-Z0-9\:\/\?\=\&\$\-\_\.\+\!\*\'\(\)\,]/
11
+
12
+ def self.has_invalid_chars?(youtube_url)
13
+ !INVALID_CHARS.match(youtube_url).nil?
14
+ end
15
+
16
+ def self.extract_video_id(youtube_url)
17
+ return nil if has_invalid_chars?(youtube_url)
18
+
19
+ URL_FORMATS.values.inject(nil) do |result, format_regex|
20
+ match = format_regex.match(youtube_url)
21
+ match ? match[:id] : result
22
+ end
23
+ end
24
+
25
+ def self.youtube_embed_url(youtube_url, width = 420, height = 315, options = {})
26
+ %(<iframe width="#{width}" height="#{height}" src="#{ youtube_embed_url_only(youtube_url, options) }" frameborder="0" allowfullscreen></iframe>)
27
+ end
28
+
29
+ def self.youtube_regular_url(youtube_url, options = {})
30
+ vid_id = extract_video_id(youtube_url)
31
+ "http#{'s' if options[:ssl]}://www.youtube.com/watch?v=#{ vid_id }"
32
+ end
33
+
34
+ def self.youtube_shortened_url(youtube_url, options = {})
35
+ vid_id = extract_video_id(youtube_url)
36
+ "http#{'s' if options[:ssl]}://youtu.be/#{ vid_id }"
37
+ end
38
+
39
+ def self.youtube_embed_url_only(youtube_url, options = {})
40
+ vid_id = extract_video_id(youtube_url)
41
+ "http#{'s' if options[:ssl]}://www.youtube.com/embed/#{ vid_id }"
42
+ end
43
+
44
+ def self.youtube_video_image(youtube_url)
45
+ vid_id = extract_video_id(youtube_url)
46
+ "https://i.ytimg.com/vi/#{ vid_id }/hqdefault.jpg"
47
+ end
48
+ end
metadata ADDED
@@ -0,0 +1,43 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: youtube_rails
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Luiz Picolo
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-02-08 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Parse youtube addresses and generate embed html code
14
+ email: luizpicolo@gmail.com
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - lib/youtube_rails.rb
20
+ homepage: https://github.com/luizpicolo/youtube_rails/
21
+ licenses: []
22
+ metadata: {}
23
+ post_install_message:
24
+ rdoc_options: []
25
+ require_paths:
26
+ - lib
27
+ required_ruby_version: !ruby/object:Gem::Requirement
28
+ requirements:
29
+ - - ">="
30
+ - !ruby/object:Gem::Version
31
+ version: '0'
32
+ required_rubygems_version: !ruby/object:Gem::Requirement
33
+ requirements:
34
+ - - ">="
35
+ - !ruby/object:Gem::Version
36
+ version: '0'
37
+ requirements: []
38
+ rubyforge_project:
39
+ rubygems_version: 2.4.8
40
+ signing_key:
41
+ specification_version: 4
42
+ summary: YouTube address helper
43
+ test_files: []