Class: Pinport::Parser
- Inherits:
-
Object
- Object
- Pinport::Parser
- Defined in:
- lib/pinport/parser.rb
Class Method Summary collapse
-
.dos2unix(file, output_path = nil) ⇒ String
Fixes linebreaks on files and returns result.
-
.sort_file_contents(input, output_path = nil) ⇒ Object
Runs ‘sort` command to sort contents of a text file.
-
.strip_chars(file, chars, output_path = nil) ⇒ String
Strips characters to be filtered and returns result.
Class Method Details
.dos2unix(file, output_path = nil) ⇒ String
Fixes linebreaks on files and returns result
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/pinport/parser.rb', line 11 def dos2unix(file, output_path = nil) puts "Fixing newlines for #{file} ..." if File.exists?(file) input = File.open(file) output = Tempfile.new("/tmp") regex = /\r\n/ newstring = "\n" while (line = input.gets) do output_line = line.gsub(regex, newstring) output << output_line end input.close output.close output_path = file unless output_path != nil system("mv #{Shellwords.shellescape(output.path)} #{Shellwords.shellescape(output_path)}") puts "Parser: Corrected newlines." return output_path # return "Newlines were successfully fixed for #{file}" else puts "Specified file does not exist." return nil end end |
.sort_file_contents(input, output_path = nil) ⇒ Object
Runs ‘sort` command to sort contents of a text file. Returns true if successfully outputted to a file; otherwise prints the contents of the result
64 65 66 67 68 69 70 71 |
# File 'lib/pinport/parser.rb', line 64 def sort_file_contents(input, output_path = nil) puts "Sorting: #{input} ..." # Shellescape arguments to prevent errors system("sort #{Shellwords.shellescape(input)} -o #{Shellwords.shellescape(output_path)}") unless output_path == nil return true # otherwise: print the result `sort #{input}` end |
.strip_chars(file, chars, output_path = nil) ⇒ String
Strips characters to be filtered and returns result
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/pinport/parser.rb', line 40 def strip_chars(file, chars, output_path = nil) puts "Filtering: #{file} ..." if File.exists?(file) input = File.open(file) output = Tempfile.new("/tmp") while (line = input.gets) do output_line = line.gsub!(chars, '') output << output_line end input.close output.close output_path = file unless output_path != nil system("mv #{Shellwords.shellescape(output.path)} #{Shellwords.shellescape(output_path)}") puts "Parser: Filtered #{chars}." return output_path else puts "Specified file does not exist." return nil end end |