Created
January 15, 2013 19:45
-
-
Save JoelQ/4541398 to your computer and use it in GitHub Desktop.
Example refactoring for RenameMethod
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
require 'uri' | |
class MediaParser | |
def initialize(text) | |
@text = text | |
@links = URI.extract @text | |
end | |
def parse_links | |
@links.each do |link| | |
media = convert_to_media(link) | |
embed link, media | |
end | |
@parsed_text | |
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 | |
def embed(link, media) | |
source = @parsed_text || @text | |
@parsed_text = source.gsub(link, media) | |
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
require 'uri' | |
class MediaParser | |
def initialize(text) | |
@text = text | |
@links = URI.extract @text | |
end | |
def parse_links | |
@links.each do |link| | |
embedable_media = convert_to_media(link) | |
replace_url_with_html_embed link, embedable_media | |
end | |
@parsed_text | |
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 | |
def replace_url_with_html_embed(link, html_snippet) | |
source = @parsed_text || @text | |
@parsed_text = source.gsub(link, html_snippet) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment