wgif 0.2.0 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.travis.yml +1 -0
- data/README.md +27 -10
- data/Rakefile +8 -3
- data/lib/wgif.rb +10 -10
- data/lib/wgif/argument_parser.rb +94 -0
- data/lib/wgif/cli.rb +31 -87
- data/lib/wgif/download_bar.rb +0 -1
- data/lib/wgif/downloader.rb +23 -27
- data/lib/wgif/exceptions.rb +8 -25
- data/lib/wgif/gif_maker.rb +3 -1
- data/lib/wgif/installer.rb +2 -3
- data/lib/wgif/uploader.rb +7 -4
- data/lib/wgif/version.rb +1 -1
- data/lib/wgif/video.rb +17 -16
- data/lib/wgif/video_cache.rb +1 -4
- data/spec/integration/empty_image_list_spec.rb +18 -0
- data/spec/spec_helper.rb +3 -0
- data/spec/unit/wgif/argument_parser_spec.rb +153 -0
- data/spec/unit/wgif/cli_spec.rb +98 -0
- data/spec/{wgif → unit/wgif}/download_bar_spec.rb +0 -0
- data/spec/{wgif → unit/wgif}/downloader_spec.rb +14 -9
- data/spec/{wgif → unit/wgif}/gif_maker_spec.rb +0 -0
- data/spec/{wgif → unit/wgif}/installer_spec.rb +45 -35
- data/spec/unit/wgif/uploader_spec.rb +48 -0
- data/spec/{wgif → unit/wgif}/video_cache_spec.rb +0 -0
- data/spec/{wgif → unit/wgif}/video_spec.rb +27 -22
- data/wgif.gemspec +22 -22
- metadata +37 -18
- data/spec/wgif/cli_spec.rb +0 -207
- data/spec/wgif/uploader_spec.rb +0 -44
data/spec/wgif/cli_spec.rb
DELETED
@@ -1,207 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
require 'wgif/cli'
|
3
|
-
|
4
|
-
describe WGif::CLI do
|
5
|
-
let(:cli) { described_class.new }
|
6
|
-
|
7
|
-
it 'parses a URL from command line args' do
|
8
|
-
args = cli.parse_args ["http://example.com"]
|
9
|
-
args[:url].should eq("http://example.com")
|
10
|
-
end
|
11
|
-
|
12
|
-
it 'starts at 0s by default' do
|
13
|
-
args = cli.parse_args ["http://example.com"]
|
14
|
-
args[:trim_from].should eq("00:00:00")
|
15
|
-
end
|
16
|
-
|
17
|
-
it 'trims clips to 1s by default' do
|
18
|
-
args = cli.parse_args ["http://example.com"]
|
19
|
-
args[:duration].should eq(1)
|
20
|
-
end
|
21
|
-
|
22
|
-
it 'parses the short frame count option' do
|
23
|
-
options = cli.parse_options ["-f", "40"]
|
24
|
-
options[:frames].should eq(40)
|
25
|
-
end
|
26
|
-
|
27
|
-
it 'parses the long frame count option' do
|
28
|
-
options = cli.parse_options ["--frames", "40"]
|
29
|
-
options[:frames].should eq(40)
|
30
|
-
end
|
31
|
-
|
32
|
-
it 'parses the short start time option' do
|
33
|
-
options = cli.parse_options ["-s", "00:00:05"]
|
34
|
-
options[:trim_from].should eq("00:00:05")
|
35
|
-
end
|
36
|
-
|
37
|
-
it 'parses the long start time option' do
|
38
|
-
options = cli.parse_options ["--start", "00:00:05"]
|
39
|
-
options[:trim_from].should eq("00:00:05")
|
40
|
-
end
|
41
|
-
|
42
|
-
it 'parses the short duration option' do
|
43
|
-
options = cli.parse_options ["-d", "1.43"]
|
44
|
-
options[:duration].should eq(1.43)
|
45
|
-
end
|
46
|
-
|
47
|
-
it 'parses the long duration option' do
|
48
|
-
options = cli.parse_options ["--duration", "5.3"]
|
49
|
-
options[:duration].should eq(5.3)
|
50
|
-
end
|
51
|
-
|
52
|
-
it 'parses the short dimensions option' do
|
53
|
-
options = cli.parse_options ["-w", "400"]
|
54
|
-
expect(options[:dimensions]).to eq("400")
|
55
|
-
end
|
56
|
-
|
57
|
-
it 'parses the long dimensions option' do
|
58
|
-
options = cli.parse_options ["--width", "300"]
|
59
|
-
expect(options[:dimensions]).to eq("300")
|
60
|
-
end
|
61
|
-
|
62
|
-
it 'parses the short upload option' do
|
63
|
-
options = cli.parse_options ["-u"]
|
64
|
-
expect(options[:upload]).to eq(true)
|
65
|
-
end
|
66
|
-
|
67
|
-
it 'parses the long upload option' do
|
68
|
-
options = cli.parse_options ["--upload"]
|
69
|
-
expect(options[:upload]).to eq(true)
|
70
|
-
end
|
71
|
-
|
72
|
-
it 'handles args in wacky order' do
|
73
|
-
args = cli.parse_args([
|
74
|
-
"-d",
|
75
|
-
"1.5",
|
76
|
-
"http://example.com",
|
77
|
-
"--frames",
|
78
|
-
"60",
|
79
|
-
"my-great-gif.gif",
|
80
|
-
"-s",
|
81
|
-
"00:00:05"])
|
82
|
-
|
83
|
-
expect(args).to eq(url: "http://example.com",
|
84
|
-
trim_from: "00:00:05",
|
85
|
-
duration: 1.5,
|
86
|
-
frames: 60,
|
87
|
-
output: "my-great-gif.gif",
|
88
|
-
dimensions: "480")
|
89
|
-
end
|
90
|
-
|
91
|
-
context 'validating args' do
|
92
|
-
|
93
|
-
it 'checks for a missing output file' do
|
94
|
-
args = cli.parse_args([
|
95
|
-
"http://example.com",
|
96
|
-
])
|
97
|
-
expect{ cli.validate_args args }.to raise_error(WGif::MissingOutputFileException)
|
98
|
-
end
|
99
|
-
|
100
|
-
it 'checks for an invalid URL' do
|
101
|
-
args = cli.parse_args([
|
102
|
-
"crazy nonsense",
|
103
|
-
"output.gif"
|
104
|
-
])
|
105
|
-
expect{ cli.validate_args args }.to raise_error(WGif::InvalidUrlException)
|
106
|
-
end
|
107
|
-
|
108
|
-
it 'checks for an invalid timestamp' do
|
109
|
-
args = cli.parse_args([
|
110
|
-
"http://lol.wut",
|
111
|
-
"output.gif",
|
112
|
-
"-s",
|
113
|
-
"rofl"
|
114
|
-
])
|
115
|
-
expect{ cli.validate_args args }.to raise_error(WGif::InvalidTimestampException)
|
116
|
-
end
|
117
|
-
|
118
|
-
it 'returns true when args are OK' do
|
119
|
-
args = cli.parse_args([
|
120
|
-
"https://crazynonsense.info",
|
121
|
-
"output.gif"
|
122
|
-
])
|
123
|
-
expect{ cli.validate_args args }.not_to raise_error
|
124
|
-
end
|
125
|
-
end
|
126
|
-
|
127
|
-
context 'error handling' do
|
128
|
-
|
129
|
-
before do
|
130
|
-
@mock_stdout = StringIO.new
|
131
|
-
@real_stdout, $stdout = $stdout, @mock_stdout
|
132
|
-
end
|
133
|
-
|
134
|
-
after do
|
135
|
-
$stdout = @real_stdout
|
136
|
-
end
|
137
|
-
|
138
|
-
def expect_help_with_message(out, message)
|
139
|
-
expect(out).to include(message)
|
140
|
-
expect(out).to include('Usage: wgif [YouTube URL] [output file] [options]')
|
141
|
-
cli.parser.summarize.each do |help_info|
|
142
|
-
expect(out).to include(help_info)
|
143
|
-
end
|
144
|
-
expect(out).to include('Example:')
|
145
|
-
end
|
146
|
-
|
147
|
-
it 'catches invalid URLs' do
|
148
|
-
OptionParser.any_instance.stub(:parse!).and_raise(WGif::InvalidUrlException)
|
149
|
-
expect{ cli.make_gif([]) }.to raise_error(SystemExit)
|
150
|
-
expect_help_with_message(@mock_stdout.string, 'That looks like an invalid URL. Check the syntax.')
|
151
|
-
end
|
152
|
-
|
153
|
-
it 'catches invalid timestamps' do
|
154
|
-
OptionParser.any_instance.stub(:parse!).and_raise(WGif::InvalidTimestampException)
|
155
|
-
expect{ cli.make_gif([]) }.to raise_error(SystemExit)
|
156
|
-
expect_help_with_message(@mock_stdout.string, 'That looks like an invalid timestamp. Check the syntax.')
|
157
|
-
end
|
158
|
-
|
159
|
-
it 'catches missing output args' do
|
160
|
-
OptionParser.any_instance.stub(:parse!).and_raise(WGif::MissingOutputFileException)
|
161
|
-
expect{ cli.make_gif([]) }.to raise_error(SystemExit)
|
162
|
-
expect_help_with_message(@mock_stdout.string, 'Please specify an output file.')
|
163
|
-
end
|
164
|
-
|
165
|
-
it 'catches missing videos' do
|
166
|
-
OptionParser.any_instance.stub(:parse!).and_raise(WGif::VideoNotFoundException)
|
167
|
-
expect{ cli.make_gif([]) }.to raise_error(SystemExit)
|
168
|
-
expect_help_with_message(@mock_stdout.string, "WGif can't find a valid YouTube video at that URL.")
|
169
|
-
end
|
170
|
-
|
171
|
-
it 'catches encoding exceptions' do
|
172
|
-
OptionParser.any_instance.stub(:parse!).and_raise(WGif::ClipEncodingException)
|
173
|
-
expect{ cli.make_gif([]) }.to raise_error(SystemExit)
|
174
|
-
expect_help_with_message(@mock_stdout.string, "WGif encountered an error transcoding the video.")
|
175
|
-
end
|
176
|
-
|
177
|
-
it 'catches upload errors' do
|
178
|
-
OptionParser.any_instance.stub(:parse!).and_raise(WGif::ImgurException, "Imgur error")
|
179
|
-
expect{ cli.make_gif([]) }.to raise_error(SystemExit)
|
180
|
-
expect_help_with_message(@mock_stdout.string, "Imgur error")
|
181
|
-
end
|
182
|
-
|
183
|
-
it 'raises SystemExit when thrown' do
|
184
|
-
OptionParser.any_instance.stub(:parse!).and_raise(SystemExit)
|
185
|
-
expect{ cli.make_gif([]) }.to raise_error(SystemExit)
|
186
|
-
end
|
187
|
-
|
188
|
-
it 'Prints the backtrace for all other exceptions' do
|
189
|
-
exception = Exception.new 'crazy error'
|
190
|
-
OptionParser.any_instance.stub(:parse!).and_raise(exception)
|
191
|
-
expect{ cli.make_gif([]) }.to raise_error(SystemExit)
|
192
|
-
expect_help_with_message(@mock_stdout.string, 'Something went wrong creating your GIF. The details:')
|
193
|
-
expect(@mock_stdout.string).to include('Please open an issue')
|
194
|
-
expect(@mock_stdout.string).to include("#{exception}")
|
195
|
-
expect(@mock_stdout.string).to include(exception.backtrace.join("\n"))
|
196
|
-
end
|
197
|
-
|
198
|
-
it 'prints help information' do
|
199
|
-
expect{ cli.make_gif(['-h']) }.to raise_error(SystemExit)
|
200
|
-
cli.parser.summarize.each do |help_info|
|
201
|
-
expect(@mock_stdout.string).to include(help_info)
|
202
|
-
end
|
203
|
-
end
|
204
|
-
|
205
|
-
end
|
206
|
-
|
207
|
-
end
|
data/spec/wgif/uploader_spec.rb
DELETED
@@ -1,44 +0,0 @@
|
|
1
|
-
require 'json'
|
2
|
-
require 'spec_helper'
|
3
|
-
require 'typhoeus'
|
4
|
-
require 'wgif/uploader'
|
5
|
-
|
6
|
-
describe WGif::Uploader do
|
7
|
-
let(:api_key) { 'api-key' }
|
8
|
-
let(:uploader) { WGif::Uploader.new(api_key) }
|
9
|
-
let(:success) { Typhoeus::Response.new(
|
10
|
-
response_code: 200,
|
11
|
-
return_code: :ok,
|
12
|
-
body: {data: {link: 'foo'}}.to_json)
|
13
|
-
}
|
14
|
-
|
15
|
-
let(:failure) { Typhoeus::Response.new(
|
16
|
-
response_code: 400,
|
17
|
-
return_code: :error,
|
18
|
-
body: {data: {error: 'You should be ashamed of yourself.'}}.to_json )
|
19
|
-
}
|
20
|
-
let(:tempfile) { Tempfile.new('whatever') }
|
21
|
-
let(:request_params) { {body: {image: tempfile},
|
22
|
-
headers: {Authorization: "Client-ID #{api_key}"}} }
|
23
|
-
|
24
|
-
it 'sends an authorized POST request to Imgur with image file data' do
|
25
|
-
File.stub(:open).and_yield(tempfile)
|
26
|
-
expect(Typhoeus).to receive(:post).
|
27
|
-
with('https://api.imgur.com/3/image', request_params).and_return(success)
|
28
|
-
uploader.upload(tempfile.path)
|
29
|
-
end
|
30
|
-
|
31
|
-
it 'raises an exception if request is not successful' do
|
32
|
-
Typhoeus.stub(/http/).and_return(failure)
|
33
|
-
expect{ uploader.upload(tempfile.path) }.
|
34
|
-
to raise_error(WGif::ImgurException, 'You should be ashamed of yourself.')
|
35
|
-
end
|
36
|
-
|
37
|
-
it 'returns the url when successful' do
|
38
|
-
File.stub(:open).and_yield(tempfile)
|
39
|
-
expect(Typhoeus).to receive(:post).
|
40
|
-
with('https://api.imgur.com/3/image', request_params).and_return(success)
|
41
|
-
expect(uploader.upload(tempfile.path)).to eq("foo")
|
42
|
-
end
|
43
|
-
|
44
|
-
end
|