youtube_identifier 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +40 -0
- data/Rakefile +1 -0
- data/lib/youtube_identifier/identifier.rb +31 -0
- data/lib/youtube_identifier/invalid_url_error.rb +7 -0
- data/lib/youtube_identifier/version.rb +3 -0
- data/lib/youtube_identifier.rb +6 -0
- data/spec/spec_helper.rb +3 -0
- data/spec/support/invalid_url.rb +7 -0
- data/spec/youtube_identifier/identifier_spec.rb +72 -0
- data/youtube_identifier.gemspec +25 -0
- metadata +110 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 Sharethrough, Inc.
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,40 @@
|
|
1
|
+
# YouTubeIdentifier
|
2
|
+
|
3
|
+
YouTubeIdentifier provides information about any valid YouTube URL.
|
4
|
+
|
5
|
+
- It can figure out the video ID of a YouTube URL.
|
6
|
+
- It can build a canonical YouTube URL based on any other YouTube URL or just the video ID.
|
7
|
+
|
8
|
+
## Installation
|
9
|
+
|
10
|
+
Add this line to your application's Gemfile:
|
11
|
+
|
12
|
+
gem 'youtube_identifier'
|
13
|
+
|
14
|
+
And then execute:
|
15
|
+
|
16
|
+
$ bundle
|
17
|
+
|
18
|
+
Or install it yourself as:
|
19
|
+
|
20
|
+
$ gem install youtube_identifier
|
21
|
+
|
22
|
+
## Usage
|
23
|
+
|
24
|
+
url = 'www.youtube.com/watch?v=7OLQnKr_sh8'
|
25
|
+
YouTubeIdentifier::Identifier.new(url).valid? # => true
|
26
|
+
YouTubeIdentifier::Identifier.new(url).id # => '7OLQnKr_sh8'
|
27
|
+
YouTubeIdentifier::Identifier.new(url).canonical_url # => 'http://www.youtube.com/watch?v=7OLQnKr_sh8'
|
28
|
+
|
29
|
+
url = 'http://google.com'
|
30
|
+
YouTubeIdentifier::Identifier.new(url).valid? # => false
|
31
|
+
YouTubeIdentifier::Identifier.new(url).id # => YouTubeIdentifier::InvalidURLError
|
32
|
+
YouTubeIdentifier::Identifier.new(url).canonical_url # => YouTubeIdentifier::InvalidURLError
|
33
|
+
|
34
|
+
## Contributing
|
35
|
+
|
36
|
+
1. Fork it
|
37
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
38
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
39
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
40
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require 'bundler/gem_tasks'
|
@@ -0,0 +1,31 @@
|
|
1
|
+
module YouTubeIdentifier
|
2
|
+
class Identifier
|
3
|
+
def initialize(url)
|
4
|
+
@url = url
|
5
|
+
end
|
6
|
+
|
7
|
+
def valid?
|
8
|
+
!url_regexp.match(url).nil?
|
9
|
+
end
|
10
|
+
|
11
|
+
def id
|
12
|
+
raise InvalidURLError unless self.valid?
|
13
|
+
|
14
|
+
url_regexp.match(url) do |match|
|
15
|
+
match[1]
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
def canonical_url
|
20
|
+
"http://www.youtube.com/watch?v=#{id}"
|
21
|
+
end
|
22
|
+
|
23
|
+
private
|
24
|
+
|
25
|
+
attr_reader :url
|
26
|
+
|
27
|
+
def url_regexp
|
28
|
+
/(?:youtube\.com\/(?:[^\/]+\/.+\/|(?:v|e(?:mbed)?)\/|.*[?&]v=)|youtu\.be\/)([^"&?\/ ]{11})/
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
shared_examples_for 'it raises an error with an invalid URL' do |method|
|
2
|
+
it 'raises an error' do
|
3
|
+
expect {
|
4
|
+
YouTubeIdentifier::Identifier.new('http://google.com').public_send(method)
|
5
|
+
}.to raise_error(YouTubeIdentifier::InvalidURLError, 'Please make sure the URL is a valid YouTube URL')
|
6
|
+
end
|
7
|
+
end
|
@@ -0,0 +1,72 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'youtube_identifier'
|
3
|
+
|
4
|
+
describe YouTubeIdentifier::Identifier do
|
5
|
+
def urls
|
6
|
+
[
|
7
|
+
'youtube.com/watch?v=7OLQnKr_sh8',
|
8
|
+
'www.youtube.com/watch?v=7OLQnKr_sh8',
|
9
|
+
'http://www.youtube.com/watch?v=7OLQnKr_sh8',
|
10
|
+
'https://www.youtube.com/watch?v=7OLQnKr_sh8',
|
11
|
+
'http://youtube.com/watch?v=7OLQnKr_sh8',
|
12
|
+
'http://youtu.be/7OLQnKr_sh8',
|
13
|
+
'http://www.youtube.com/embed/7OLQnKr_sh8?showinfo=0',
|
14
|
+
'http://www.youtube.com/watch?v=7OLQnKr_sh8&feature=youtu.be',
|
15
|
+
'http://www.youtube.com/watch?feature=player_embedded&v=7OLQnKr_sh8',
|
16
|
+
'http://www.youtube.com/watch?v=7OLQnKr_sh8&feature=c4-overview&list=UUXHuWmYc0-eXz1dSXTLrO1Q',
|
17
|
+
'http://www.youtube.com/watch?v=7OLQnKr_sh8&list=TLb6Fk7xTJEaw'
|
18
|
+
]
|
19
|
+
end
|
20
|
+
|
21
|
+
describe '#valid?' do
|
22
|
+
context 'with a valid YouTube URL' do
|
23
|
+
specify do
|
24
|
+
expect(YouTubeIdentifier::Identifier.new('http://youtube.com/watch?v=7OLQnKr_sh8')).to be_valid
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
context 'with a YouTube URL without a video ID' do
|
29
|
+
specify do
|
30
|
+
expect(YouTubeIdentifier::Identifier.new('http://youtube.com/watch')).to_not be_valid
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
context 'with a non-YouTube URL' do
|
35
|
+
specify do
|
36
|
+
expect(YouTubeIdentifier::Identifier.new('http://google.com')).to_not be_valid
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
describe '#id' do
|
42
|
+
context 'for a valid URL' do
|
43
|
+
it 'fetches the ID' do
|
44
|
+
video_id = '7OLQnKr_sh8'
|
45
|
+
|
46
|
+
urls.each do |url|
|
47
|
+
expect(YouTubeIdentifier::Identifier.new(url).id).to eq(video_id)
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
context 'for an invalid URL' do
|
53
|
+
it_behaves_like 'it raises an error with an invalid URL', :id
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
describe '#canonical_url' do
|
58
|
+
context 'for a valid URL' do
|
59
|
+
it 'builds the full YouTube URL without extra parameters' do
|
60
|
+
canonical_url = 'http://www.youtube.com/watch?v=7OLQnKr_sh8'
|
61
|
+
|
62
|
+
urls.each do |url|
|
63
|
+
expect(YouTubeIdentifier::Identifier.new(url).canonical_url).to eq(canonical_url)
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
context 'for an invalid URL' do
|
69
|
+
it_behaves_like 'it raises an error with an invalid URL', :canonical_url
|
70
|
+
end
|
71
|
+
end
|
72
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'youtube_identifier/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = 'youtube_identifier'
|
8
|
+
spec.version = YouTubeIdentifier::VERSION
|
9
|
+
spec.authors = ['Danny Olson']
|
10
|
+
spec.email = ['danny@sharethrough.com']
|
11
|
+
spec.description = %q{YouTubeIdentifier provides information about any valid YouTube URL.}
|
12
|
+
spec.summary = %q{YouTubeIdentifier provides information about any valid YouTube URL.}
|
13
|
+
|
14
|
+
spec.homepage = 'https://github.com/sharethrough/youtube_identifier'
|
15
|
+
spec.license = 'MIT'
|
16
|
+
|
17
|
+
spec.files = `git ls-files`.split($/)
|
18
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
19
|
+
spec.test_files = spec.files.grep(%r{^spec/})
|
20
|
+
spec.require_paths = ['lib']
|
21
|
+
|
22
|
+
spec.add_development_dependency 'bundler', '~> 1.3'
|
23
|
+
spec.add_development_dependency 'rake'
|
24
|
+
spec.add_development_dependency 'rspec', '~> 2.14'
|
25
|
+
end
|
metadata
ADDED
@@ -0,0 +1,110 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: youtube_identifier
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Danny Olson
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-08-14 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: bundler
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ~>
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '1.3'
|
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: '1.3'
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: rake
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
type: :development
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: rspec
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ~>
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '2.14'
|
54
|
+
type: :development
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ~>
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '2.14'
|
62
|
+
description: YouTubeIdentifier provides information about any valid YouTube URL.
|
63
|
+
email:
|
64
|
+
- danny@sharethrough.com
|
65
|
+
executables: []
|
66
|
+
extensions: []
|
67
|
+
extra_rdoc_files: []
|
68
|
+
files:
|
69
|
+
- .gitignore
|
70
|
+
- Gemfile
|
71
|
+
- LICENSE.txt
|
72
|
+
- README.md
|
73
|
+
- Rakefile
|
74
|
+
- lib/youtube_identifier.rb
|
75
|
+
- lib/youtube_identifier/identifier.rb
|
76
|
+
- lib/youtube_identifier/invalid_url_error.rb
|
77
|
+
- lib/youtube_identifier/version.rb
|
78
|
+
- spec/spec_helper.rb
|
79
|
+
- spec/support/invalid_url.rb
|
80
|
+
- spec/youtube_identifier/identifier_spec.rb
|
81
|
+
- youtube_identifier.gemspec
|
82
|
+
homepage: https://github.com/sharethrough/youtube_identifier
|
83
|
+
licenses:
|
84
|
+
- MIT
|
85
|
+
post_install_message:
|
86
|
+
rdoc_options: []
|
87
|
+
require_paths:
|
88
|
+
- lib
|
89
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
90
|
+
none: false
|
91
|
+
requirements:
|
92
|
+
- - ! '>='
|
93
|
+
- !ruby/object:Gem::Version
|
94
|
+
version: '0'
|
95
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
96
|
+
none: false
|
97
|
+
requirements:
|
98
|
+
- - ! '>='
|
99
|
+
- !ruby/object:Gem::Version
|
100
|
+
version: '0'
|
101
|
+
requirements: []
|
102
|
+
rubyforge_project:
|
103
|
+
rubygems_version: 1.8.24
|
104
|
+
signing_key:
|
105
|
+
specification_version: 3
|
106
|
+
summary: YouTubeIdentifier provides information about any valid YouTube URL.
|
107
|
+
test_files:
|
108
|
+
- spec/spec_helper.rb
|
109
|
+
- spec/support/invalid_url.rb
|
110
|
+
- spec/youtube_identifier/identifier_spec.rb
|