Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Ruby version of UnicodeFileToHtmlTextConverter both original problem and tested and refactored solution #14

Merged
merged 3 commits into from

2 participants

@Sam-Serpoosh

And here is the last one! Thanks again for publishing these exercises they were interesting!

Regards,

@lucaminudel
Owner

A question on the solution. Isn't possible to use IO (does it define a general protocol for input or output for files bat also others in memory sources?) as generic source to pass to the initialize method ?

That should work with files but also with strings (in tests) or in future other sources. In this way the OCP is also respected.

@lucaminudel lucaminudel merged commit 428078b into lucaminudel:master
@Sam-Serpoosh
@lucaminudel
Owner

If I understood correctly (I'm new to Ruby) File::Open return a File object that inherit from IO like all the input/output classes in Ruby. Let's say that IO define the abstract concept of an i/o stream.

I can write UnicodeFileToHtmTextConverter.initialize that accept an object that implement the IO protocol (for the input part since the class read only).
If the parameter instead is a string, the initialize can open the file and use that as input.

Something like this:

def initialize(source)
   if source.respond_to?("read")
       @source = source
   else 
       @source = File.open(source)
   end 
end

Does this make sense?

@Sam-Serpoosh
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
View
30 ...-Serpoosh/code/unicode_file_to_html_text_converter/unicode_file_to_html_text_converter.rb
@@ -0,0 +1,30 @@
+class UnicodeFileToHtmTextConverter
+ def initialize(full_file_name_with_path)
+ @full_file_name_with_path = full_file_name_with_path
+ @html = ""
+ end
+
+ def convert_to_html
+ text = read_content
+ text.each_line { |line| @html += to_html(line) }
+
+ @html
+ end
+
+ private
+ def read_content
+ FileReader.read_content(@full_file_name_with_path)
+ end
+
+ def to_html(text)
+ converted = HttpUtility.html_encode(text)
+ converted += "<br />"
+ end
+end
+
+class FileReader
+ def self.read_content(file_path)
+ text = File.open(file_path).read # Not exactly the same thing as the # CShapr code but same functionality!
+ text.gsub!(/\r\n?/, "\n")
+ end
+end
View
33 ...spec/unicode_file_to_html_text_converter_spec/unicode_file_to_html_text_converter_spec.rb
@@ -0,0 +1,33 @@
+require_relative "../../code/unicode_file_to_html_text_converter/unicode_file_to_html_text_converter"
+
+class HttpUtility; end
+
+describe UnicodeFileToHtmTextConverter do
+ let(:file_path) { stub }
+ let(:foobar) { "foobar" }
+
+ before do
+ @converter = UnicodeFileToHtmTextConverter.new(file_path)
+ end
+
+ it "reads the file content" do
+ FileReader.should_receive(:read_content).with(file_path).
+ and_return(foobar)
+ HttpUtility.stub(:html_encode => foobar)
+
+ @converter.convert_to_html
+ end
+
+ it "converts to html on a line basis" do
+ FileReader.stub(:read_content => foobar)
+ HttpUtility.should_receive(:html_encode).and_return("<b>foobar</b>")
+
+ html = @converter.convert_to_html
+ html.should == "<b>foobar</b><br />"
+ end
+end
+
+# Another way of doing this which specially more suitable for
+# statically typed languages is inject the FileReader &
+# HttpUtility through the constructor according to
+# Dependency Inversion Principle
View
18 ...Exercises/Ruby/unicode_file_to_html_text_converter/unicode_file_to_html_text_converter.rb
@@ -0,0 +1,18 @@
+class UnicodeFileToHtmTextConverter
+ def initialize(full_file_name_with_path)
+ @full_file_name_with_path = full_file_name_with_path
+ end
+
+ def convert_to_html
+ html = ""
+
+ text = File.open(@full_file_name_with_path).read # Not exactly the same code in CSharp but same functionality!
+ text.gsub!(/\r\n?/, "\n")
+ text.each_line do |line|
+ html += HttpUtility.html_encode(line)
+ html += "<br />"
+ end
+
+ return html
+ end
+end
Something went wrong with that request. Please try again.