Last active
January 4, 2016 06:58
-
-
Save tanraya/8585004 to your computer and use it in GitHub Desktop.
Parse and compose urls to YouTube and Vimeo videos
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Url | |
module Parser | |
# Youtube | |
YOUTUBE_REGEX = [ | |
/watch\?v=(?<slug>[A-Za-z0-9_-]+)/, | |
/(?:www\.)?youtube\.com\/(?:embed|v)\/(?<slug>[A-Za-z0-9_-]+)/, | |
/youtu\.be\/(?<slug>[A-Za-z0-9_-]+)/, | |
] | |
# Vimeo | |
VIMEO_REGEX = [ | |
/vimeo\.com\/(?<slug>\d+)/, | |
/vimeo\.com\/video\/(?<slug>\d+)/ | |
] | |
# url or iframe tag | |
def self.parse(url) | |
if m = Regexp.union(YOUTUBE_REGEX).match(url) | |
{ slug: m[:slug], type: :youtube } | |
elsif m = Regexp.union(VIMEO_REGEX).match(url) | |
{ slug: m[:slug], type: :vimeo } | |
else | |
{ slug: nil, type: nil } | |
end | |
end | |
end | |
end | |
module Url | |
module Composer | |
def self.compose(parsed_hash) | |
case parsed_hash[:type] | |
when :youtube | |
"//www.youtube.com/embed/#{parsed_hash[:slug]}" | |
when :vimeo | |
"//player.vimeo.com/video/#{parsed_hash[:slug]}" | |
else | |
nil | |
end | |
end | |
end | |
end | |
describe Url::Parser do | |
describe "youtube" do | |
context "variant 1" do | |
subject { Url::Parser.parse("http://www.youtube.com/watch?v=7kE9W5aULTg") } | |
it { should include(slug: "7kE9W5aULTg", type: :youtube) } | |
end | |
context "variant 2" do | |
subject { Url::Parser.parse("watch?v=Eb24RfQ7y3A&feature=related") } | |
it { should include(slug: "Eb24RfQ7y3A", type: :youtube) } | |
end | |
context "variant 3" do | |
subject { Url::Parser.parse("http://youtu.be/ccmrXwpEFSI") } | |
it { should include(slug: "ccmrXwpEFSI", type: :youtube) } | |
end | |
context "variant 4" do | |
subject { Url::Parser.parse("//www.youtube.com/v/N7_r9wdr15M?hl=en_US&version=3") } | |
it { should include(slug: "N7_r9wdr15M", type: :youtube) } | |
end | |
context "variant 5" do | |
subject { Url::Parser.parse("//www.youtube.com/embed/7kE9W5aULTg") } | |
it { should include(slug: "7kE9W5aULTg", type: :youtube) } | |
end | |
context "variant 6" do | |
subject { Url::Parser.parse(%q{<iframe width="640" height="360" src="//www.youtube.com/embed/7kE9W5aULTg" frameborder="0" allowfullscreen></iframe>}) } | |
it { should include(slug: "7kE9W5aULTg", type: :youtube) } | |
end | |
context "variant 7" do | |
subject { Url::Parser.parse("\nyoutube.com/watch?v=7kE9W5aULTg\n") } | |
it { should include(slug: "7kE9W5aULTg", type: :youtube) } | |
end | |
end | |
describe "vimeo" do | |
context "variant 1" do | |
subject { Url::Parser.parse("http://vimeo.com/76129556") } | |
it { should include(slug: "76129556", type: :vimeo) } | |
end | |
context "variant 2" do | |
subject { Url::Parser.parse(%q{<iframe src="//player.vimeo.com/video/76129556" width="500" height="281" frameborder="0" webkitallowfullscreen mozallowfullscreen allowfullscreen></iframe> <p><a href="http://vimeo.com/76129556">Orgesticulanismus - Mathieu Labaye</a> from <a href="http://vimeo.com/user20885903">Camera-etc</a> on <a href="https://vimeo.com">Vimeo</a>.</p>}) } | |
it { should include(slug: "76129556", type: :vimeo) } | |
end | |
context "variant 3" do | |
subject { Url::Parser.parse(%Q{<iframe\nsrc="vimeo.com/video/76129556\n" width="500" height="281" frameborder="0" webkitallowfullscreen mozallowfullscreen allowfullscreen></iframe> <p><a href="http://vimeo.com/76129556">Orgesticulanismus - Mathieu Labaye</a> from <a href="http://vimeo.com/user20885903">Camera-etc</a> on <a href="https://vimeo.com">Vimeo</a>.</p>}) } | |
it { should include(slug: "76129556", type: :vimeo) } | |
end | |
end | |
describe "unrecognized url" do | |
subject { Url::Parser.parse("http://example.com/76129556") } | |
it { should include(slug: nil, type: nil) } | |
end | |
end | |
describe Url::Composer do | |
describe "youtube" do | |
subject { Url::Composer.compose(slug: "7kE9W5aULTg", type: :youtube) } | |
it { should eq "//www.youtube.com/embed/7kE9W5aULTg" } | |
end | |
describe "vimeo" do | |
subject { Url::Composer.compose(slug: "76129556", type: :vimeo) } | |
it { should eq "//player.vimeo.com/video/76129556" } | |
end | |
describe "unrecognized composing" do | |
subject { Url::Composer.compose(type: nil, slug: nil) } | |
it { should be_nil } | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment