directlink 0.0.4.1 → 0.0.4.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +4 -0
- data/api_tokens_for_travis.sh +1 -0
- data/bin/directlink +3 -2
- data/directlink.gemspec +3 -2
- data/lib/directlink.rb +63 -22
- data/reddit_token_for_travis.yaml +4 -0
- data/test.rb +71 -30
- metadata +24 -9
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 95b60e38f85cce3fe9c3cf915a4ae95cf76a1a33
|
4
|
+
data.tar.gz: c2a6dfdd49aa4b9ab22bf9dbaad5d594f1307454
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: dbca7e2c709fc76d04deb83c77b2742cb9ccd8b842d8f366a36c938f4baa709cd4748d09a8c503d78811364cacbd014c724dd8f78caa641225a89ff3dee9af89
|
7
|
+
data.tar.gz: e7ed956ca117b8e2d4a281cab5f314e553d375181381a80fc3a882bf9f6cec19fe360d1bbbe5ec3b4f6aa38a9516fcb140ed57c91329e8062af43d548714bfe7
|
data/README.md
CHANGED
@@ -159,3 +159,7 @@ SocketError: Failed to open TCP connection to minus.com:80 (getaddrinfo: nodenam
|
|
159
159
|
* style: `@@` and lambdas are used to keep things private
|
160
160
|
* this gem is a historically 2 or 3 libraries merged -- this is why tests may look awkward
|
161
161
|
* 500px.com has discontinued API in June 2018 -- the tool now uses undocumented methods
|
162
|
+
* `DirectLink()` can return an Array of Structs for 1) Imgur 2) Reddit unless `giveup = true` is set
|
163
|
+
|
164
|
+
TODO: maybe make all these web service specific methods private and discourage to use them since they all return very different things and sometimes don't raise exceptions while the `DirectLink()` does
|
165
|
+
TODO: what should `--json` print if exception was thrown?
|
data/api_tokens_for_travis.sh
CHANGED
data/bin/directlink
CHANGED
@@ -68,8 +68,9 @@ begin
|
|
68
68
|
(t.is_a?(Array) ? t : [t]).each{ |s| puts "=> #{s.url}\n #{s.type} #{s.width}x#{s.height}" }
|
69
69
|
end
|
70
70
|
end
|
71
|
-
rescue
|
72
|
-
|
71
|
+
rescue SocketError,
|
72
|
+
Net::OpenTimeout,
|
73
|
+
NetHTTPUtils::Error,
|
73
74
|
FastImage::UnknownImageType,
|
74
75
|
FastImage::ImageFetchFailure,
|
75
76
|
# DirectLink::ErrorMissingEnvVar,
|
data/directlink.gemspec
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
Gem::Specification.new do |spec|
|
2
2
|
spec.name = "directlink"
|
3
|
-
spec.version = "0.0.4.
|
3
|
+
spec.version = "0.0.4.2"
|
4
4
|
spec.summary = "converts any kind of image hyperlink to direct link, type of image and its resolution"
|
5
5
|
|
6
6
|
spec.author = "Victor Maslov aka Nakilon"
|
@@ -9,9 +9,10 @@ Gem::Specification.new do |spec|
|
|
9
9
|
spec.homepage = "https://github.com/nakilon/directlink"
|
10
10
|
spec.metadata = {"source_code_uri" => "https://github.com/nakilon/directlink"}
|
11
11
|
|
12
|
-
spec.add_dependency "nethttputils", "~>0.2.4.0"
|
13
12
|
spec.add_dependency "fastimage", "~>2.1.3"
|
14
13
|
spec.add_dependency "nokogiri"
|
14
|
+
spec.add_dependency "reddit_bot", "~>1.6.7"
|
15
|
+
spec.add_dependency "kramdown"
|
15
16
|
spec.add_development_dependency "minitest"
|
16
17
|
|
17
18
|
spec.require_path = "lib"
|
data/lib/directlink.rb
CHANGED
@@ -31,7 +31,7 @@ module DirectLink
|
|
31
31
|
end
|
32
32
|
class ErrorMissingEnvVar < logging_error
|
33
33
|
def initialize msg
|
34
|
-
super "(warning
|
34
|
+
super "(warning/recommendation) #{msg}"
|
35
35
|
end
|
36
36
|
end
|
37
37
|
|
@@ -199,6 +199,35 @@ module DirectLink
|
|
199
199
|
imageinfo.first["url"]
|
200
200
|
end
|
201
201
|
|
202
|
+
class << self
|
203
|
+
attr_accessor :reddit_bot
|
204
|
+
end
|
205
|
+
def self.reddit link
|
206
|
+
unless id = URI(link).path[/\A(?:\/r\/[0-9a-zA-Z_]+)?(?:\/comments)?\/([0-9a-z]{5,6})(?:\/|\z)/, 1]
|
207
|
+
raise DirectLink::ErrorBadLink.new link unless URI(link).host &&
|
208
|
+
URI(link).host.split(?.) == %w{ i redd it } &&
|
209
|
+
URI(link).path[/\A\/[a-z0-9]{12,13}\.(gif|jpg)\z/]
|
210
|
+
return [true, link]
|
211
|
+
end
|
212
|
+
if ENV["REDDIT_SECRETS"]
|
213
|
+
require "reddit_bot"
|
214
|
+
RedditBot.logger.level = Logger::FATAL
|
215
|
+
require "yaml"
|
216
|
+
reddit_bot ||= RedditBot::Bot.new YAML.load_file ENV["REDDIT_SECRETS"]
|
217
|
+
json = reddit_bot.json(:get, "/by_id/t3_#{id}")
|
218
|
+
else
|
219
|
+
raise ErrorMissingEnvVar.new "defining REDDIT_SECRETS env var is highly recommended" rescue nil
|
220
|
+
json = JSON.load NetHTTPUtils.request_data "#{link}.json", header: {"User-Agent" => "Mozilla"}
|
221
|
+
raise ErrorAssert.new "our knowledge about Reddit API seems to be outdated" unless json.size == 2
|
222
|
+
json = json.find{ |_| _["data"]["children"].first["kind"] == "t3" }
|
223
|
+
end
|
224
|
+
data = json["data"]["children"].first["data"]
|
225
|
+
url = data["url"]
|
226
|
+
return [true, url] unless data["is_self"]
|
227
|
+
raise ErrorAssert.new "our knowledge about Reddit API seems to be outdated" if url != "https://www.reddit.com" + data["permalink"]
|
228
|
+
return [false, data["selftext"]]
|
229
|
+
end
|
230
|
+
|
202
231
|
class_variable_set :@@directlink, Struct.new(:url, :width, :height, :type)
|
203
232
|
end
|
204
233
|
|
@@ -215,13 +244,16 @@ def DirectLink link, max_redirect_resolving_retry_delay = nil, giveup = false
|
|
215
244
|
|
216
245
|
struct = Module.const_get(__callee__).class_variable_get :@@directlink
|
217
246
|
|
218
|
-
|
219
|
-
|
220
|
-
|
221
|
-
|
222
|
-
|
223
|
-
|
247
|
+
google_without_schema_crutch = lambda do
|
248
|
+
if %w{ lh3 googleusercontent com } == URI(link).host.split(?.).last(3) ||
|
249
|
+
%w{ bp blogspot com } == URI(link).host.split(?.).last(3)
|
250
|
+
u = DirectLink.google link
|
251
|
+
f = FastImage.new(u, raise_on_failure: true, http_header: {"User-Agent" => "Mozilla"})
|
252
|
+
w, h = f.size
|
253
|
+
struct.new u, w, h, f.type
|
254
|
+
end
|
224
255
|
end
|
256
|
+
t = google_without_schema_crutch[] and return t
|
225
257
|
|
226
258
|
# to test that we won't hang for too long if someone like aeronautica.difesa.it will be silent for some reason:
|
227
259
|
# $ bundle console
|
@@ -241,14 +273,7 @@ def DirectLink link, max_redirect_resolving_retry_delay = nil, giveup = false
|
|
241
273
|
# because they can be hidden behind URL shorteners
|
242
274
|
# also it can resolve NetHTTPUtils::Error(404) before trying the adapter
|
243
275
|
|
244
|
-
|
245
|
-
if %w{ lh3 googleusercontent com } == URI(link).host.split(?.).last(3) ||
|
246
|
-
%w{ bp blogspot com } == URI(link).host.split(?.).last(3)
|
247
|
-
u = DirectLink.google link
|
248
|
-
f = FastImage.new(u, raise_on_failure: true, http_header: {"User-Agent" => "Mozilla"})
|
249
|
-
w, h = f.size
|
250
|
-
return struct.new u, w, h, f.type
|
251
|
-
end
|
276
|
+
t = google_without_schema_crutch[] and return t
|
252
277
|
|
253
278
|
begin
|
254
279
|
imgur = DirectLink.imgur(link).sort_by{ |u, w, h, t| - w * h }.map do |u, w, h, t|
|
@@ -257,9 +282,9 @@ def DirectLink link, max_redirect_resolving_retry_delay = nil, giveup = false
|
|
257
282
|
# `DirectLink.imgur` return value is always an Array
|
258
283
|
return imgur.size == 1 ? imgur.first : imgur
|
259
284
|
rescue DirectLink::ErrorMissingEnvVar
|
260
|
-
end if %w{
|
261
|
-
%w{
|
262
|
-
%w{
|
285
|
+
end if %w{ imgur com } == URI(link).host.split(?.).last(2) ||
|
286
|
+
%w{ i imgur com } == URI(link).host.split(?.).last(3) ||
|
287
|
+
%w{ m imgur com } == URI(link).host.split(?.).last(3) ||
|
263
288
|
%w{ www imgur com } == URI(link).host.split(?.).last(3)
|
264
289
|
|
265
290
|
if %w{ 500px com } == URI(link).host.split(?.).last(2)
|
@@ -269,25 +294,41 @@ def DirectLink link, max_redirect_resolving_retry_delay = nil, giveup = false
|
|
269
294
|
|
270
295
|
begin
|
271
296
|
w, h, u = DirectLink.flickr(link)
|
272
|
-
f = FastImage.new(u, raise_on_failure: true, http_header: {"User-Agent" => "Mozilla"}
|
297
|
+
f = FastImage.new(u, raise_on_failure: true) # , http_header: {"User-Agent" => "Mozilla"}
|
273
298
|
return struct.new u, w, h, f.type
|
274
299
|
rescue DirectLink::ErrorMissingEnvVar
|
275
300
|
end if %w{ www flickr com } == URI(link).host.split(?.).last(3)
|
276
301
|
|
277
|
-
if %w{
|
302
|
+
if %w{ wikipedia org } == URI(link).host.split(?.).last(2) ||
|
278
303
|
%w{ commons wikimedia org } == URI(link).host.split(?.).last(3)
|
279
304
|
u = DirectLink.wiki link
|
280
|
-
f = FastImage.new(u, raise_on_failure: true, http_header: {"User-Agent" => "Mozilla"}
|
305
|
+
f = FastImage.new(u, raise_on_failure: true) # , http_header: {"User-Agent" => "Mozilla"}
|
281
306
|
w, h = f.size
|
282
307
|
return struct.new u, w, h, f.type
|
283
308
|
end
|
284
309
|
|
310
|
+
# TODO protect in two places from eternal recursion
|
311
|
+
|
312
|
+
begin
|
313
|
+
s, u = DirectLink.reddit(link)
|
314
|
+
if s
|
315
|
+
return DirectLink u, max_redirect_resolving_retry_delay, giveup
|
316
|
+
end
|
317
|
+
raise DirectLink::ErrorBadLink.new link if giveup # TODO: print original url in such cases if there was a recursion
|
318
|
+
f = ->_{ _.type == :a ? _.attr["href"] : _.children.flat_map(&f) }
|
319
|
+
require "kramdown"
|
320
|
+
return f[Kramdown::Document.new(u).root].map{ |_| DirectLink _, max_redirect_resolving_retry_delay, giveup }
|
321
|
+
rescue DirectLink::ErrorMissingEnvVar
|
322
|
+
end if %w{ reddit com } == URI(link).host.split(?.).last(2) ||
|
323
|
+
%w{ redd it } == URI(link).host.split(?.).last(2)
|
324
|
+
|
325
|
+
|
285
326
|
begin
|
286
327
|
f = FastImage.new(link, raise_on_failure: true, http_header: {"User-Agent" => "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.139 Safari/537.36"})
|
287
328
|
rescue FastImage::UnknownImageType
|
288
329
|
raise if giveup
|
289
330
|
require "nokogiri"
|
290
|
-
html = Nokogiri::HTML NetHTTPUtils::request_data link
|
331
|
+
html = Nokogiri::HTML NetHTTPUtils::request_data link, header: {"User-Agent" => "Mozilla"}
|
291
332
|
h = {}
|
292
333
|
l = lambda do |node, s = []|
|
293
334
|
node.element_children.flat_map do |child|
|
data/test.rb
CHANGED
@@ -9,12 +9,13 @@ require "minitest/mock"
|
|
9
9
|
|
10
10
|
fail unless ENV.include? "IMGUR_CLIENT_ID"
|
11
11
|
fail unless ENV.include? "FLICKR_API_KEY"
|
12
|
+
fail unless ENV.include? "REDDIT_SECRETS"
|
12
13
|
|
13
14
|
require_relative "lib/directlink"
|
14
15
|
DirectLink.silent = true
|
15
16
|
describe DirectLink do
|
16
17
|
|
17
|
-
describe "
|
18
|
+
describe "./lib" do
|
18
19
|
|
19
20
|
describe "google" do
|
20
21
|
"
|
@@ -284,6 +285,8 @@ describe DirectLink do
|
|
284
285
|
end
|
285
286
|
end
|
286
287
|
|
288
|
+
# TODO we need tests that check we really get dimensions from `DirectLink()` method called on wiki and reddit links
|
289
|
+
# and maaaaybe move some tests from here to the context about giveup
|
287
290
|
[
|
288
291
|
[ :_500px, [
|
289
292
|
["https://500px.com/photo/264092015/morning-rider-by-tiger-seo", [1200, 800, "https://drscdn.500px.org/photo/264092015/m%3D1200/v2?webp=true&sig=49c6f8346ba8453ccb17208d4653b9e11bc3e1bb8c21c161047e2842716f3649", "jpeg"]],
|
@@ -306,12 +309,21 @@ describe DirectLink do
|
|
306
309
|
["http://commons.wikimedia.org/wiki/File:Eduard_Bohlen_anagoria.jpg", "https://upload.wikimedia.org/wikipedia/commons/0/0d/Eduard_Bohlen_anagoria.jpg"],
|
307
310
|
["https://en.wikipedia.org/wiki/Spanish_Civil_War#/media/File:Alfonso_XIIIdeEspa%C3%B1a.jpg", "https://upload.wikimedia.org/wikipedia/commons/f/fb/Alfonso_XIIIdeEspa%C3%B1a.jpg"], # escaped input URI
|
308
311
|
] ],
|
312
|
+
[ :reddit, [
|
313
|
+
["https://www.reddit.com/r/cacography/comments/32tq0i/c/", [true, "http://i.imgur.com/vy6Ms4Z.jpg"]],
|
314
|
+
["http://redd.it/32tq0i", [true, "http://i.imgur.com/vy6Ms4Z.jpg"]], # TODO maybe check that it calls #imgur recursively
|
315
|
+
["https://i.redd.it/c8rk0kjywhy01.jpg", [true, "https://i.redd.it/c8rk0kjywhy01.jpg"]],
|
316
|
+
["https://i.redd.it/si758zk7r5xz.jpg", [true, "https://i.redd.it/si758zk7r5xz.jpg"]], # it is 404 but `.reddit` does not care -- it just returns the url
|
317
|
+
["https://reddit.com/123456", [true, "http://www.youtube.com/watch?v=b9upM4RbIeU&feature=g-vrec"]],
|
318
|
+
["https://www.reddit.com/r/travel/988889", [true, "https://i.redd.it/3h5xls6ehrg11.jpg"]],
|
319
|
+
["http://redd.it/988889", [true, "https://i.redd.it/3h5xls6ehrg11.jpg"]],
|
320
|
+
] ],
|
309
321
|
].each do |method, tests|
|
310
322
|
describe method do
|
311
323
|
tests.each_with_index do |(input, expectation), i|
|
312
|
-
it "##{i + 1}" do
|
324
|
+
it "#{method} ##{i + 1}" do
|
313
325
|
if expectation.is_a? Class
|
314
|
-
assert_raises expectation do
|
326
|
+
assert_raises expectation, input do
|
315
327
|
DirectLink.method(method).call input
|
316
328
|
end
|
317
329
|
else
|
@@ -347,6 +359,13 @@ describe DirectLink do
|
|
347
359
|
https://en.wikipedia.org/wiki/Third_Party_System#/media/File:United_States_presidential_election_results,_1876-1892.svg
|
348
360
|
http://commons.wikimedia.org/wiki/File:Eduard_Bohlen_anagoria.jpg
|
349
361
|
},
|
362
|
+
reddit: %w{
|
363
|
+
https://www.reddit.com/r/cacography/comments/32tq0i/c/
|
364
|
+
https://i.redd.it/si758zk7r5xz.jpg
|
365
|
+
http://redd.it/32tq0i
|
366
|
+
https://reddit.com/123456
|
367
|
+
https://www.reddit.com/r/travel/988889
|
368
|
+
},
|
350
369
|
}.each do |method, tests|
|
351
370
|
describe "DirectLink() calls #{method}" do
|
352
371
|
tests.each_with_index do |input, i|
|
@@ -363,10 +382,12 @@ describe DirectLink do
|
|
363
382
|
end
|
364
383
|
|
365
384
|
describe "throws ErrorBadLink if method does not match the link" do
|
366
|
-
%i{ google imgur flickr _500px wiki }.each do |method|
|
367
|
-
|
368
|
-
|
369
|
-
DirectLink
|
385
|
+
%i{ google imgur flickr _500px wiki reddit }.each do |method|
|
386
|
+
["", "test", "http://example.com/"].each_with_index do |url, i|
|
387
|
+
it "#{method} ##{i + 1}" do
|
388
|
+
assert_raises DirectLink::ErrorBadLink do
|
389
|
+
DirectLink.method(method).call url
|
390
|
+
end
|
370
391
|
end
|
371
392
|
end
|
372
393
|
end
|
@@ -377,22 +398,38 @@ describe DirectLink do
|
|
377
398
|
describe "DirectLink()" do
|
378
399
|
|
379
400
|
it "throws ErrorBadLink if link is invalid" do
|
380
|
-
|
381
|
-
DirectLink
|
382
|
-
|
383
|
-
|
401
|
+
assert_equal "test".inspect, (
|
402
|
+
assert_raises DirectLink::ErrorBadLink do
|
403
|
+
DirectLink "test"
|
404
|
+
end
|
405
|
+
).message
|
384
406
|
end
|
385
407
|
|
386
408
|
describe "does not shadow the internal exception" do
|
387
|
-
|
409
|
+
it "raises SocketError from the redirect resolving stage" do
|
410
|
+
assert_raises SocketError do
|
411
|
+
NetHTTPUtils.stub :get_response, ->*{ raise SocketError.new } do
|
412
|
+
DirectLink "http://example.com/404"
|
413
|
+
end
|
414
|
+
end
|
415
|
+
end
|
416
|
+
it "raises Net::OpenTimeout -- server side issues can happen (not related to User Agent)" do
|
417
|
+
assert_raises Net::OpenTimeout do
|
418
|
+
NetHTTPUtils.stub :get_response, ->*{ raise Net::OpenTimeout.new } do
|
419
|
+
DirectLink "http://example.com/404"
|
420
|
+
end
|
421
|
+
end
|
422
|
+
end
|
423
|
+
[ # TODO this URLs may be reused from tests that check that this method calls internal method
|
388
424
|
[:google, "//lh3.googleusercontent.com/proxy/DZtTi5KL7PqiBwJc8weNGLk_Wi2UTaQH0AC_h2kuURiu0AbwyI2ywOk2XgdAjL7ceg=w530-h354-n"],
|
389
425
|
[:imgur, "http://imgur.com/HQHBBBD"],
|
390
426
|
[:flickr, "https://www.flickr.com/photos/44133687@N00/17380073505/"],
|
391
427
|
[:_500px, "https://500px.com/photo/112134597/milky-way-by-tom-hall"],
|
392
428
|
[:wiki, "http://commons.wikimedia.org/wiki/File:Eduard_Bohlen_anagoria.jpg"],
|
429
|
+
[:reddit, "https://www.reddit.com/123456"],
|
393
430
|
].each do |method, link|
|
394
|
-
it method do
|
395
|
-
e = assert_raises
|
431
|
+
it "can otherwise raise DirectLink::ErrorBadLink #{method}" do
|
432
|
+
e = assert_raises(DirectLink::ErrorBadLink, link) do
|
396
433
|
DirectLink.stub method, ->*{ raise DirectLink::ErrorBadLink.new "test" } do
|
397
434
|
DirectLink link
|
398
435
|
end
|
@@ -400,20 +437,12 @@ describe DirectLink do
|
|
400
437
|
assert_equal "\"test\" -- if you think this link is valid, please report the issue", e.message
|
401
438
|
end
|
402
439
|
end
|
403
|
-
it "raises SocketError from the redirect resolving stage" do
|
404
|
-
assert_raises SocketError do
|
405
|
-
NetHTTPUtils.stub :get_response, ->*{ raise SocketError.new } do
|
406
|
-
DirectLink "http://example.com/404"
|
407
|
-
end
|
408
|
-
end
|
409
|
-
end
|
410
440
|
end
|
411
441
|
|
412
|
-
describe "
|
442
|
+
describe "other domains tests" do
|
413
443
|
[
|
414
444
|
["http://www.aeronautica.difesa.it/organizzazione/REPARTI/divolo/PublishingImages/6%C2%B0%20Stormo/2013-decollo%20al%20tramonto%20REX%201280.jpg", ["http://www.aeronautica.difesa.it/organizzazione/REPARTI/divolo/PublishingImages/6%C2%B0%20Stormo/2013-decollo%20al%20tramonto%20REX%201280.jpg", 1280, 853, :jpeg]],
|
415
445
|
["http://minus.com/lkP3hgRJd9npi", SocketError, /nodename nor servname provided, or not known|No address associated with hostname/, 0],
|
416
|
-
["https://i.redd.it/si758zk7r5xz.jpg", NetHTTPUtils::Error, "HTTP error #404 "],
|
417
446
|
["http://www.cutehalloweencostumeideas.org/wp-content/uploads/2017/10/Niagara-Falls_04.jpg", SocketError, /nodename nor servname provided, or not known|Name or service not known/, 0],
|
418
447
|
].each_with_index do |(input, expectation, message_string_or_regex, max_redirect_resolving_retry_delay), i|
|
419
448
|
it "##{i + 1}" do
|
@@ -441,28 +470,35 @@ describe DirectLink do
|
|
441
470
|
["https://github.com/Nakilon/dhash-vips", FastImage::UnknownImageType, true],
|
442
471
|
["https://github.com/Nakilon/dhash-vips", 3],
|
443
472
|
["http://imgur.com/HQHBBBD", FastImage::UnknownImageType, true],
|
444
|
-
["http://imgur.com/HQHBBBD", "https://i.imgur.com/HQHBBBD.jpg?fb"],
|
473
|
+
["http://imgur.com/HQHBBBD", "https://i.imgur.com/HQHBBBD.jpg?fb"], # .at_css("meta[@property='og:image']")
|
474
|
+
["http://redd.it/123456", FastImage::UnknownImageType, true],
|
475
|
+
["http://redd.it/123456", 1],
|
476
|
+
["http://redd.it/997he7", DirectLink::ErrorBadLink, true],
|
477
|
+
["http://redd.it/997he7", 1], # currently only links are parsed
|
445
478
|
].each_with_index do |(input, expectation, giveup), i|
|
446
|
-
it "##{i + 1}" do
|
447
|
-
|
479
|
+
it "##{i + 1} (#{URI(input).host})" do # to match with minitest `-n` run flag
|
480
|
+
ti = ENV.delete "IMGUR_CLIENT_ID"
|
481
|
+
tr = ENV.delete "REDDIT_SECRETS"
|
448
482
|
begin
|
449
483
|
case expectation
|
450
484
|
when Class
|
451
485
|
e = assert_raises expectation, "for #{input} (giveup = #{giveup})" do
|
452
486
|
DirectLink input, nil, giveup
|
453
487
|
end
|
454
|
-
assert_equal expectation.to_s, e.
|
488
|
+
assert_equal expectation.to_s, e.class.to_s, "for #{input} (giveup = #{giveup})"
|
455
489
|
when String
|
456
490
|
result = DirectLink input, nil, giveup
|
457
491
|
assert_equal expectation, result.url, "for #{input} (giveup = #{giveup})"
|
458
492
|
else
|
459
493
|
result = DirectLink input, nil, giveup
|
494
|
+
result = [result] unless result.is_a? Array # we can't do `Array(<Struct>)` because it splats by elements
|
460
495
|
assert_equal expectation, result.size, ->{
|
461
496
|
"for #{input} (giveup = #{giveup}): #{result.map &:url}"
|
462
497
|
}
|
463
498
|
end
|
464
499
|
ensure
|
465
|
-
ENV["IMGUR_CLIENT_ID"] =
|
500
|
+
ENV["IMGUR_CLIENT_ID"] = ti
|
501
|
+
ENV["REDDIT_SECRETS"] = tr
|
466
502
|
end
|
467
503
|
end
|
468
504
|
end
|
@@ -513,7 +549,7 @@ describe DirectLink do
|
|
513
549
|
|
514
550
|
[1, "https://imgur.com/a/badlinkpattern", "NetHTTPUtils::Error: HTTP error #404 "],
|
515
551
|
# TODO: a test that it appends the `exception.cause`
|
516
|
-
].each_with_index do |(expected_exit_code, link, expected_output, unset), i|
|
552
|
+
].each_with_index do |(expected_exit_code, link, expected_output, unset), i| # TODO: unset is not used anymore or I have to go sleep?
|
517
553
|
it "##{i + 1}" do
|
518
554
|
string, status = Open3.capture2e "export #{File.read("api_tokens_for_travis.sh").gsub(/\n?export/, ?\s).strip}#{unset} && bundle exec ruby bin/directlink #{link}"
|
519
555
|
assert_equal [expected_exit_code, "#{expected_output}\n"], [status.exitstatus, string], "for #{link}"
|
@@ -557,12 +593,17 @@ describe DirectLink do
|
|
557
593
|
]
|
558
594
|
'.gsub(/^ {8}/, ""), "json"],
|
559
595
|
].each do |expected_output, param|
|
560
|
-
it "#{param || "default"}
|
596
|
+
it "#{param || "default"} output format" do
|
561
597
|
string, status = Open3.capture2e "export #{File.read("api_tokens_for_travis.sh").gsub(/\n?export/, ?\s).strip} && bundle exec ruby bin/directlink#{" --#{param}" if param} #{valid_imgur_image_url1} #{valid_imgur_image_url2}"
|
562
598
|
assert_equal [0, expected_output], [status.exitstatus, string]
|
563
599
|
end
|
564
600
|
end
|
565
601
|
|
602
|
+
it "reddit_bot gem logger does not flood STDOUT" do
|
603
|
+
string, status = Open3.capture2e "bundle exec ruby bin/directlink http://redd.it/997he7"
|
604
|
+
assert_equal "<= http://redd.it/997he7\n=> https://i.imgur.com/QpOBvRY.png\n image/png 460x460\n", string
|
605
|
+
end
|
606
|
+
|
566
607
|
end
|
567
608
|
|
568
609
|
end
|
metadata
CHANGED
@@ -1,45 +1,59 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: directlink
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.4.
|
4
|
+
version: 0.0.4.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Victor Maslov aka Nakilon
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-08-
|
11
|
+
date: 2018-08-22 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
|
-
name:
|
14
|
+
name: fastimage
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
17
|
- - "~>"
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version:
|
19
|
+
version: 2.1.3
|
20
20
|
type: :runtime
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
24
|
- - "~>"
|
25
25
|
- !ruby/object:Gem::Version
|
26
|
-
version:
|
26
|
+
version: 2.1.3
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
|
-
name:
|
28
|
+
name: nokogiri
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: reddit_bot
|
29
43
|
requirement: !ruby/object:Gem::Requirement
|
30
44
|
requirements:
|
31
45
|
- - "~>"
|
32
46
|
- !ruby/object:Gem::Version
|
33
|
-
version:
|
47
|
+
version: 1.6.7
|
34
48
|
type: :runtime
|
35
49
|
prerelease: false
|
36
50
|
version_requirements: !ruby/object:Gem::Requirement
|
37
51
|
requirements:
|
38
52
|
- - "~>"
|
39
53
|
- !ruby/object:Gem::Version
|
40
|
-
version:
|
54
|
+
version: 1.6.7
|
41
55
|
- !ruby/object:Gem::Dependency
|
42
|
-
name:
|
56
|
+
name: kramdown
|
43
57
|
requirement: !ruby/object:Gem::Requirement
|
44
58
|
requirements:
|
45
59
|
- - ">="
|
@@ -84,6 +98,7 @@ files:
|
|
84
98
|
- directlink.gemspec
|
85
99
|
- gplus.txt
|
86
100
|
- lib/directlink.rb
|
101
|
+
- reddit_token_for_travis.yaml
|
87
102
|
- test.rb
|
88
103
|
homepage: https://github.com/nakilon/directlink
|
89
104
|
licenses:
|