Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

started to transfor the script into a class

  • Loading branch information...
commit 9c09a643e38edc6ecee569583898322e188e29c7 1 parent 9fe4a79
Martin Carel authored
Showing with 47 additions and 22 deletions.
  1. +47 −22 validate.rb
View
69 validate.rb
@@ -1,35 +1,60 @@
#!/usr/bin/env ruby -w
-if ARGV.length != 1
- $stderr.puts "Usage: validator.sh subtitles_file.srt"
- exit 1
-end
+class SubtitlesValidator
+
+ def self.call args
+ puts "Starting..."
+ sv = SubtitlesValidator.new args
+ sv.read_file
+ sv.group_by_4
+ sv.does
+ puts "Done."
+ end
-if !File.exists?(ARGV[0])
- $stderr.puts "Could not find file \"#{ARGV[0]}\"."
- exit 2
-end
+ def initialize args
+ if args.length != 1
+ $stderr.puts "Usage: validator.sh subtitles_file.srt"
+ exit 1
+ end
-puts "Starting..."
+ if !File.exists?(args[0])
+ $stderr.puts "Could not find file \"#{args[0]}\"."
+ exit 2
+ end
-previous_stop = "00:00:00,000" # smallest possible value
+ @filename = args[0]
+ end
-File.open(ARGV[0]) do |file|
- while true do
- id, time, _, _ = file.gets, file.gets, file.gets, file.gets
- break if id.nil? # reached EOF
+ def read_file
+ @file_data = File.open(@filename).read
+ end
- start, stop = time.split(' --> ').map(&:strip)
- if start > stop
- puts "subtitle ##{id.strip} is invalid: #{time}"
- end
+ def group_by_4
+ # @file_data.split("\n").
+ end
+
+ def does
+ previous_stop = "00:00:00,000" # smallest possible value
- if previous_stop > start
- puts "subtitle ##{id.strip} is overlapping with the previous one."
+ File.open(@filename) do |file|
+ while true do
+ id, time, _, _ = file.gets, file.gets, file.gets, file.gets
+ break if id.nil? # reached EOF
+
+ start, stop = time.split(' --> ').map(&:strip)
+ if start > stop
+ puts "subtitle ##{id.strip} is invalid: #{time}"
+ end
+
+ if previous_stop > start
+ puts "subtitle ##{id.strip} is overlapping with the previous one."
+ end
+ previous_stop = stop
+ end
end
- previous_stop = stop
end
+
end
-puts "Done."
+SubtitlesValidator.call ARGV
Please sign in to comment.
Something went wrong with that request. Please try again.