Last active
December 11, 2015 03:59
-
-
Save JoelQ/4541831 to your computer and use it in GitHub Desktop.
Refactoring example for Extract Class
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) | |
path = url_components[5] | |
query = url_components[7] | |
if url_is_an_image? link | |
"<img src=\"#{link}\" />" | |
elsif url_is_a_youtube_regular? link | |
video_id = query[2..-1] | |
"<iframe width=\"560\" height=\"315\" src=\"http://www.youtube.com/embed/#{video_id}\" frameborder=\"0\" allowfullscreen></iframe>" | |
elsif url_is_a_youtube_short? link | |
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 | |
private | |
def url_is_an_image?(url) | |
path = URI.split(url)[5] | |
path.end_with? 'png', 'gif', 'jpeg' | |
end | |
def url_is_a_youtube_regular?(url) | |
host = URI.split(url)[2] | |
query = URI.split(url)[7] | |
host.include?("youtube") && query && query.include?('v=') | |
end | |
def url_is_a_youtube_short?(url) | |
host = URI.split(url)[2] | |
host.include? "youtu.be" | |
end | |
def replace_url_with_html_embed(link, html_snippet) | |
source = @parsed_text || @text | |
@parsed_text = source.gsub(link, html_snippet) | |
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) | |
image_embedder = ImageEmbedder.new link | |
youtube_embedder = YouTubeEmbedder.new link | |
hyperlink_embedder = HyperlinkEmbedder.new link | |
if image_embedder.can_process_url? | |
image_embedder.convert_to_media_markup | |
elsif youtube_embedder.can_process_url? | |
youtube_embedder.convert_to_media_markup | |
else | |
hyperlink_embedder.convert_to_media_markup | |
end | |
end | |
private | |
def replace_url_with_html_embed(link, html_snippet) | |
source = @parsed_text || @text | |
@parsed_text = source.gsub(link, html_snippet) | |
end | |
end | |
class ImageEmbedder | |
def initialize(url) | |
@url = url | |
end | |
def can_process_url? | |
path = URI.split(@url)[5] | |
path.end_with? 'png', 'gif', 'jpeg' | |
end | |
def convert_to_media_markup | |
"<img src=\"#{@url}\" />" | |
end | |
end | |
class YouTubeEmbedder | |
def initialize(url) | |
@url = url | |
url_components = URI.split(url) | |
@host = url_components[2] | |
@path = url_components[5] | |
@query = url_components[7] | |
end | |
def can_process_url? | |
url_is_a_youtube_short? || url_is_a_youtube_regular? | |
end | |
def convert_to_media_markup | |
"<iframe width=\"560\" height=\"315\" src=\"http://www.youtube.com/embed/#{video_id}\" frameborder=\"0\" allowfullscreen></iframe>" | |
end | |
private | |
def video_id | |
url_is_a_youtube_regular? ? @query[2..-1] : @path.gsub('/', '') | |
end | |
def url_is_a_youtube_regular? | |
@host.include?("youtube") && @query && @query.include?('v=') | |
end | |
def url_is_a_youtube_short? | |
@host.include? "youtu.be" | |
end | |
end | |
class HyperlinkEmbedder | |
def initialize(url) | |
@url = url | |
end | |
def can_process_url? | |
true | |
end | |
def convert_to_media_markup | |
"<a href=\"#{@url}\">#{@url}</a>" | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment