youtube_rails 1.2.3 → 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 +4 -4
  2. data/lib/youtube_rails.rb +56 -8
  3. metadata +5 -5
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 86f7224eee425af232a74d07e4acb689f9e3ec3907d96ba479e561a884e8a0ac
4
- data.tar.gz: e1c60b63ef630a7d77b0f7d1d4c5e172984e20c83f46c29be563831e011a98c9
3
+ metadata.gz: 8d1b81a5c7528335893d18d5e6ffad8da532b612851a47e6873a04e6d51206fe
4
+ data.tar.gz: 36767e4ac2e364e1ed8b1a7fd8250ebbfcf5f3b783a12cc5f852b6501de358e9
5
5
  SHA512:
6
- metadata.gz: a0ba62d3d5160f52d90e0eccd28204f9587cbe3e7b69fa9e11e923fe62d92e35c214ea10816c8c5af6cea7747a3daa9aa6e196b5de2bd478eb021ab67e379ca6
7
- data.tar.gz: '039ad878a6d2f32ce06207b842043a1f23a674a68a2c3c18e9946abe42f9ac3877eb82a8ac1b43c144fae8c236df05166d5979e9e61f756cbf883e93b6b5919a'
6
+ metadata.gz: b09579bc900465574939882a12abb9c3ba7b87b5c8c40e999f08be48fb2f50cd7d69498ce607523a6c228b463a072b8ffbc98b2ce32b6b1f83c2dca66ee5d969
7
+ data.tar.gz: 499bf7175c62027e7cfafe750b17e890e5225061f456ddad273b5218e861d348fe5281697fbd3fc3592e6c0192a0ce5cde2445b0f6599d6e4576adc07a511bc8
data/lib/youtube_rails.rb CHANGED
@@ -1,13 +1,46 @@
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?
@@ -15,8 +48,23 @@ class YouTubeRails
15
48
 
16
49
  def self.extract_video_id(youtube_url)
17
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
54
+
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
18
63
 
19
- URL_FORMATS.values.inject(nil) do |result, format_regex|
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|
20
68
  match = format_regex.match(youtube_url)
21
69
  match ? match[:id] : result
22
70
  end
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.3
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,8 +35,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
35
35
  - !ruby/object:Gem::Version
36
36
  version: '0'
37
37
  requirements: []
38
- rubygems_version: 3.3.7
39
- signing_key:
38
+ rubygems_version: 3.4.1
39
+ signing_key:
40
40
  specification_version: 4
41
41
  summary: YouTube address helper
42
42
  test_files: []