srtshifter 1.0.2 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -3,75 +3,63 @@ require 'optparse'
3
3
  module SrtShifter
4
4
  class Options
5
5
 
6
- operations = ["ADD", "SUB"]
6
+ attr_accessor :options
7
7
 
8
- options = {}
9
-
10
- ARGV.options do |opts|
8
+ def initialize(args)
9
+ @arguments = args
11
10
 
12
- opts.banner = "Usage: #{File.basename($PROGRAM_NAME)} [OPTIONS] input_file output_file"
13
-
14
- opts.separator ""
15
- opts.separator "Common Options:"
11
+ @operations = ["ADD", "SUB"]
16
12
 
17
- opts.on("-o","--operation (ADD|SUB)", String, "Type ADD to increase time or SUB to decrease time.") do |o|
18
- options[:operation] = o.upcase if operations.include? o.upcase
19
- end
13
+ @options = {}
14
+ end
20
15
 
21
- opts.on("-t","--time (VALUE)", Float, "Time in milliseconds.") do |n|
22
- options[:time] = n
23
- end
16
+ def parse_options
17
+ @arguments.options do |opts|
18
+
19
+ opts.banner = "Usage: #{File.basename($PROGRAM_NAME)} [OPTIONS] input_file output_file"
20
+
21
+ opts.separator ""
22
+ opts.separator "Common Options:"
23
+
24
+ opts.on("-o","--operation (ADD|SUB)", String, "Type ADD to increase time or SUB to decrease time.") do |o|
25
+ @options[:operation] = o
26
+ end
27
+
28
+ opts.on("-t","--time (VALUE)", Float, "Time in milliseconds.") do |n|
29
+ @options[:time] = n
30
+ end
24
31
 
25
- opts.on_tail('-h', '--help', 'Show this help message.') do
26
- puts(opts)
27
- exit(0)
32
+ opts.on_tail('-h', '--help', 'Show this help message.') do
33
+ puts(opts)
34
+ exit(0)
35
+ end
36
+
37
+ opts.parse!
28
38
  end
29
-
30
- opts.parse!
39
+ end
31
40
 
41
+ def validate_args
32
42
  begin
33
- raise "Operation option is missing" if options[:operation].nil?
34
- raise "Time option is missing" if options[:time].nil?
35
-
36
- raise "Input file is missing" if ARGV[0].nil?
37
- options[:input] = ARGV[0]
38
-
39
- raise "Output file is missing" if ARGV[1].nil?
40
- options[:output] = ARGV[1]
41
43
 
42
- subtitle = SrtShifter::Subtitle.new(options)
43
- subtitle.shift
44
44
 
45
- rescue Exception => ex
46
- puts "#{ex.message}. Please use -h or --help for usage."
47
- exit(1)
48
- end
45
+ raise "Operation option is missing" if @options[:operation].nil?
46
+ raise "Unknown operation: #{@options[:operation]}" unless @operations.include? @options[:operation].upcase
47
+
48
+ raise "Time option is missing" if @options[:time].nil?
49
+
50
+ raise "Input file is missing" if @arguments[0].nil?
51
+ @options[:input] = @arguments[0]
52
+
53
+ raise "Output file is missing" if @arguments[1].nil?
54
+ @options[:output] = @arguments[1]
55
+
56
+ # subtitle = SrtShifter::Subtitle.new(@options)
57
+ # subtitle.shift
49
58
 
50
- # begin
51
- # raise "Unknown option(s) #{options.join(', ')}" if options.empty?
52
- # rescue Exception => e
53
- # puts "#{ex.message()}. Please use -h or --help for usage."
54
- # exit(1)
55
- # end
56
-
57
- # begin
58
-
59
- # opts.parse! ARGV
60
-
61
- # # raise OptionParser::MissingArgument if options[:operation].nil? || options[:time].nil?
62
-
63
- # # options[:input] = ARGV[0] unless ARGV[0] == nil
64
- # # options[:output] = ARGV[1] unless ARGV[1] == nil
65
-
66
- # # subtitle = SrtShifter::Subtitle.new(options)
67
- # # subtitle.shift
68
- # rescue
69
- # # $stderr.print $!
70
- # # puts options
71
- # # puts "\n"
72
- # # puts opts
73
- # # #exit
74
- # end
59
+ rescue Exception => ex
60
+ puts "#{ex.message}. Please use -h or --help for usage."
61
+ exit(1)
62
+ end
75
63
  end
76
64
  end
77
65
  end
@@ -0,0 +1,24 @@
1
+ module SrtShifter
2
+ class Shifter
3
+
4
+ def initialize(args)
5
+ @arguments = args
6
+ @opts = SrtShifter::Options.new(@arguments)
7
+ @subtitle = subtitle = SrtShifter::Subtitle.new(@opts.options)
8
+ end
9
+
10
+ def execute
11
+ @subtitle.shift if parsed_options? and valid_args?
12
+ end
13
+
14
+ protected
15
+ def parsed_options?
16
+ @opts.parse_options
17
+ end
18
+
19
+ def valid_args?
20
+ @opts.validate_args
21
+ end
22
+
23
+ end
24
+ end
@@ -1,3 +1,3 @@
1
1
  module SrtShifter
2
- VERSION = "1.0.2"
2
+ VERSION = "1.1.0"
3
3
  end
data/lib/srtshifter.rb CHANGED
@@ -1,3 +1,7 @@
1
1
  require "srtshifter/subtitle"
2
2
  require "srtshifter/options"
3
- require "srtshifter/version"
3
+ require "srtshifter/version"
4
+ require "srtshifter/shifter"
5
+
6
+ shifter = SrtShifter::Shifter.new(ARGV)
7
+ shifter.execute
@@ -0,0 +1,2 @@
1
+ require "test_helper"
2
+
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: srtshifter
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.2
4
+ version: 1.1.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-12-25 00:00:00.000000000 Z
12
+ date: 2012-12-26 00:00:00.000000000 Z
13
13
  dependencies: []
14
14
  description: Shifts STR Subtitles
15
15
  email:
@@ -27,10 +27,12 @@ files:
27
27
  - bin/srtshifter
28
28
  - lib/srtshifter.rb
29
29
  - lib/srtshifter/options.rb
30
+ - lib/srtshifter/shifter.rb
30
31
  - lib/srtshifter/subtitle.rb
31
32
  - lib/srtshifter/version.rb
32
33
  - srtshifter.gemspec
33
34
  - test/lib/srtshifter/options_test.rb
35
+ - test/lib/srtshifter/shifter_test.rb
34
36
  - test/lib/srtshifter/subtitle_test.rb
35
37
  - test/lib/srtshifter/version_test.rb
36
38
  - test/samples/test_srt.srt
@@ -61,6 +63,7 @@ specification_version: 3
61
63
  summary: It fixes the timing of a given SubRip file
62
64
  test_files:
63
65
  - test/lib/srtshifter/options_test.rb
66
+ - test/lib/srtshifter/shifter_test.rb
64
67
  - test/lib/srtshifter/subtitle_test.rb
65
68
  - test/lib/srtshifter/version_test.rb
66
69
  - test/samples/test_srt.srt