Created
March 11, 2013 23:33
-
-
Save TheNotary/5138918 to your computer and use it in GitHub Desktop.
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 RubyBBCode | |
def self.parse_youtube_id(url) | |
#url = "http://www.youtube.com/watch?v=E4Fbk52Mk1w" | |
url =~ /[vV]=([^&]*)/ | |
id = $1 | |
if id.nil? | |
# when there is no match for v=blah, then maybe they just | |
# provided us with the ID the way the system used to work... | |
# e.g. "E4Fbk52Mk1w" | |
return url | |
else | |
# else we got a match for an id and we can return that ID as a string... | |
return id | |
end | |
end | |
end |
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
def test_youtube_parser | |
url1 = "http://www.youtube.com/watch?v=E4Fbk52Mk1w" | |
just_an_id = 'E4Fbk52Mk1w' | |
url_without_http = "www.youtube.com/watch?v=E4Fbk52Mk1w" | |
url_without_www = "youtube.com/watch?v=E4Fbk52Mk1w" | |
url_with_feature = "http://www.youtube.com/watch?feature=player_embedded&v=E4Fbk52Mk1w" | |
expected_output = 'E4Fbk52Mk1w' | |
assert_equal expected_output, | |
RubyBBCode.parse_youtube_id(url1) | |
assert_equal expected_output, | |
RubyBBCode.parse_youtube_id(just_an_id) | |
assert_equal expected_output, | |
RubyBBCode.parse_youtube_id(url_without_http) | |
assert_equal expected_output, | |
RubyBBCode.parse_youtube_id(url_without_www) | |
assert_equal expected_output, | |
RubyBBCode.parse_youtube_id(url_with_feature) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment