Last active
December 11, 2015 03:58
-
-
Save JoelQ/4541317 to your computer and use it in GitHub Desktop.
Example refactoring using Introduce Explaining Variable
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 convert_to_media(link) | |
if URI.split(link)[5].end_with? 'png', 'gif', 'jpeg' | |
"<img src=\"#{link}\" />" | |
elsif URI.split(link)[2].include?("youtube") && URI.split(link)[7] && URI.split(link)[7].include?('v=') | |
video_id = URI.split(link)[7][2..-1] | |
"<iframe width=\"560\" height=\"315\" src=\"http://www.youtube.com/embed/#{video_id}\" frameborder=\"0\" allowfullscreen></iframe>" | |
elsif URI.split(link)[2].include? "youtu.be" | |
video_id = URI.split(link)[5].gsub('/', '') | |
"<iframe width=\"560\" height=\"315\" src=\"http://www.youtube.com/embed/#{video_id}\" frameborder=\"0\" allowfullscreen></iframe>" | |
else | |
"<a href=\"#{link}\">#{link}</a>" | |
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 convert_to_media(link) | |
url_components = URI.split(link) | |
host = url_components[2] | |
path = url_components[5] | |
query = url_components[7] | |
if path.end_with? 'png', 'gif', 'jpeg' | |
"<img src=\"#{link}\" />" | |
elsif host.include?("youtube") && query && query.include?('v=') | |
video_id = query[2..-1] | |
"<iframe width=\"560\" height=\"315\" src=\"http://www.youtube.com/embed/#{video_id}\" frameborder=\"0\" allowfullscreen></iframe>" | |
elsif host.include? "youtu.be" | |
video_id = path.gsub('/', '') | |
"<iframe width=\"560\" height=\"315\" src=\"http://www.youtube.com/embed/#{video_id}\" frameborder=\"0\" allowfullscreen></iframe>" | |
else | |
"<a href=\"#{link}\">#{link}</a>" | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment