Skip to content

Instantly share code, notes, and snippets.

@JoelQ
Last active December 11, 2015 03:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JoelQ/4541317 to your computer and use it in GitHub Desktop.
Save JoelQ/4541317 to your computer and use it in GitHub Desktop.
Example refactoring using Introduce Explaining Variable
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
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