youtube_rails 1.2.2 → 1.3.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +5 -5
  2. data/lib/youtube_rails.rb +64 -16
  3. metadata +5 -6
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: b83992d7cc2f6bf50b33cd4853a57bc247c296ad
4
- data.tar.gz: fc0287f67856ee1812139734da4ddbe0018e2efe
2
+ SHA256:
3
+ metadata.gz: 8d1b81a5c7528335893d18d5e6ffad8da532b612851a47e6873a04e6d51206fe
4
+ data.tar.gz: 36767e4ac2e364e1ed8b1a7fd8250ebbfcf5f3b783a12cc5f852b6501de358e9
5
5
  SHA512:
6
- metadata.gz: ce85fc384feb1a2042960d8ab9cba3fc257b3a49b46d5f56da3dace29186bdf1a0a5fb9a739857aeab8ab31fa898e3903a05b894b21c4cd95020ce3835ec0cfd
7
- data.tar.gz: aa3db3b725e2ac7468626315d3ab0498bff42fa1d21705cd5ba147a75b0da16eac21e1e3cb065244ede955a8e32cbdc906eaed42516d599d96dfc50f901ca6d9
6
+ metadata.gz: b09579bc900465574939882a12abb9c3ba7b87b5c8c40e999f08be48fb2f50cd7d69498ce607523a6c228b463a072b8ffbc98b2ce32b6b1f83c2dca66ee5d969
7
+ data.tar.gz: 499bf7175c62027e7cfafe750b17e890e5225061f456ddad273b5218e861d348fe5281697fbd3fc3592e6c0192a0ce5cde2445b0f6599d6e4576adc07a511bc8
data/lib/youtube_rails.rb CHANGED
@@ -1,30 +1,78 @@
1
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>[^&]+)/
2
+ # Converts a list of domains into a regex matching them at the start of a string
3
+ def self._domains_to_regex(domains)
4
+ pattern = [
5
+ '^(',
6
+ domains.map {|d| Regexp.quote(d) }.join('|'),
7
+ ')/'
8
+ ].join('')
9
+
10
+ Regexp.new(pattern, Regexp::IGNORECASE | Regexp::MULTILINE)
11
+ end
12
+
13
+ SCHEME_FORMAT = %r{(https?:|)//}i
14
+ SHORTURL_DOMAIN = 'youtu.be'
15
+ # Some URLs are interchangeable with others. The keys here are just an ID.
16
+ DOMAIN_ALIASES = {
17
+ 'youtube.com' => %w{www.youtube.com
18
+ youtube.com
19
+ m.youtube.com
20
+ www.youtube-nocookie.com},
21
+ SHORTURL_DOMAIN => [SHORTURL_DOMAIN],
8
22
  }
23
+ DOMAIN_REGEX = DOMAIN_ALIASES.map do |placeholder, domains|
24
+ [placeholder, _domains_to_regex(domains)]
25
+ end.to_h
9
26
 
10
- INVALID_CHARS = /[^a-zA-Z0-9\:\/\?\=\&\$\-\_\.\+\!\*\'\(\)\,]/
27
+ ID = %r{(?<id>[0-9a-zA-Z_-]+)} # URL-safe Base64 ID
28
+ ANYPARAMS = %r{([^;&]*[&;])*} # Zero or more URL parameters
29
+ URL_FORMATS = [
30
+ %r{^(watch|ytscreeningroom)\?#{ANYPARAMS}v=#{ID}}mi,
31
+ %r{^(v|e|embed|shorts)/#{ID}}mi,
32
+ %r{^oembed\?#{ANYPARAMS}url=[^&;]+watch(%3f|\?)v(=|%3d)#{ID}}mi, # accepts encoded delims
33
+ %r{^attribution_link\?#{ANYPARAMS}u=(/|%2f)watch(%3f|\?)v(=|%3d)#{ID}}mi, # ditto
34
+ %r{^apiplayer\?#{ANYPARAMS}video_id=#{ID}}mi,
35
+ ]
36
+ SHORTURL_FORMATS = [
37
+ %r{^#{ID}}i,
38
+ ]
39
+
40
+ # See reserved and unreserved characters here:
41
+ # https://www.rfc-editor.org/rfc/rfc3986#appendix-A
42
+ # Note, % character must also be included, as this is used in pct-encoded escapes.
43
+ INVALID_CHARS = %r{[^a-zA-Z0-9:/?=&$\-_.+!*'(),~#\[\]@;%]}
11
44
 
12
45
  def self.has_invalid_chars?(youtube_url)
13
46
  !INVALID_CHARS.match(youtube_url).nil?
14
47
  end
15
48
 
16
49
  def self.extract_video_id(youtube_url)
17
- return nil if has_invalid_chars?(youtube_url)
50
+ return nil if has_invalid_chars?(youtube_url)
51
+ youtube_url = youtube_url
52
+ .strip # remove whitespace before and after
53
+ .sub(%r{^#{SCHEME_FORMAT}}, '') # remove valid schemes
18
54
 
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
55
+ # Deal with shortened URLs as a special case
56
+ if youtube_url.sub!(DOMAIN_REGEX['youtu.be'], '')
57
+ SHORTURL_FORMATS.each do |regex|
58
+ match = youtube_url.match(regex)
59
+ return match[:id] if match
60
+ end
61
+ return nil # No matches
62
+ end
24
63
 
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
64
+ # Ensure one of the regular allows domains matches
65
+ return nil unless youtube_url.sub!(DOMAIN_REGEX['youtube.com'], '')
66
+
67
+ URL_FORMATS.inject(nil) do |result, format_regex|
68
+ match = format_regex.match(youtube_url)
69
+ match ? match[:id] : result
70
+ end
71
+ end
72
+
73
+ def self.youtube_embed_url(youtube_url, width = 420, height = 315, **options)
74
+ %(<iframe width="#{width}" height="#{height}" src="#{ youtube_embed_url_only(youtube_url, **options) }" frameborder="0" allowfullscreen></iframe>)
75
+ end
28
76
 
29
77
  def self.youtube_regular_url(youtube_url, **options)
30
78
  vid_id = extract_video_id(youtube_url)
metadata CHANGED
@@ -1,11 +1,11 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: youtube_rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.2
4
+ version: 1.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Luiz Picolo
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
  date: 2016-04-11 00:00:00.000000000 Z
@@ -20,7 +20,7 @@ files:
20
20
  homepage: https://github.com/luizpicolo/youtube_rails/
21
21
  licenses: []
22
22
  metadata: {}
23
- post_install_message:
23
+ post_install_message:
24
24
  rdoc_options: []
25
25
  require_paths:
26
26
  - lib
@@ -35,9 +35,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
35
35
  - !ruby/object:Gem::Version
36
36
  version: '0'
37
37
  requirements: []
38
- rubyforge_project:
39
- rubygems_version: 2.5.1
40
- signing_key:
38
+ rubygems_version: 3.4.1
39
+ signing_key:
41
40
  specification_version: 4
42
41
  summary: YouTube address helper
43
42
  test_files: []