link_thumbnailer 2.5.0 → 2.5.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +4 -0
- data/README.md +12 -2
- data/lib/generators/templates/initializer.rb +1 -1
- data/lib/link_thumbnailer/configuration.rb +7 -3
- data/lib/link_thumbnailer/model.rb +1 -1
- data/lib/link_thumbnailer/processor.rb +22 -9
- data/lib/link_thumbnailer/version.rb +1 -1
- data/spec/configuration_spec.rb +7 -1
- data/spec/processor_spec.rb +34 -9
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 8ebb211bdc1846b8bd87073ba32ad911ce291294
|
4
|
+
data.tar.gz: 0681efbd2d6315670e5b9888c66d27b898aa9401
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7b15cff3407a6e288a97f9923279b4f926b5f8d08bd00db52881dec032501198bd8dda0c635b14fd89054c2b83f87ce4a3557a9fe57d5490af8ed6f5b3905f74
|
7
|
+
data.tar.gz: 576533b10d2f694ce8bbd974bc76af780a566227d37fc05d0ff82aca54c9807b521cdf56c457af06b8c9d54496aef04d0a51c21787af4479e7930f603847e48c
|
data/CHANGELOG.md
CHANGED
data/README.md
CHANGED
@@ -83,7 +83,7 @@ object.to_json
|
|
83
83
|
|
84
84
|
## Configuration
|
85
85
|
|
86
|
-
|
86
|
+
LinkThumbnailer comes with default configuration values. You can change default value by overriding them in a rails initializer:
|
87
87
|
|
88
88
|
In `config/initializers/link_thumbnailer.rb`
|
89
89
|
|
@@ -107,7 +107,7 @@ LinkThumbnailer.configure do |config|
|
|
107
107
|
#
|
108
108
|
# See http://www.ruby-doc.org/stdlib-2.1.1/libdoc/net/http/rdoc/Net/HTTP.html#open_timeout
|
109
109
|
#
|
110
|
-
# config.
|
110
|
+
# config.http_open_timeout = 5
|
111
111
|
|
112
112
|
# List of blacklisted urls you want to skip when searching for images.
|
113
113
|
#
|
@@ -177,6 +177,16 @@ LinkThumbnailer defines a list of custom exceptions you may want to rescue in yo
|
|
177
177
|
* `RedirectLimit` -- raised when redirection threshold defined in config is reached
|
178
178
|
* `BadUriFormat` -- raised when url given is not a valid HTTP url
|
179
179
|
|
180
|
+
You can rescue from any LinkThumbnailer exceptions using the following code:
|
181
|
+
|
182
|
+
```ruby
|
183
|
+
begin
|
184
|
+
LinkThumbnailer.generate('http://foo.com')
|
185
|
+
rescue LinkThumbnailer::Exceptions => e
|
186
|
+
# do something
|
187
|
+
end
|
188
|
+
```
|
189
|
+
|
180
190
|
## Contributing
|
181
191
|
|
182
192
|
1. Fork it
|
@@ -18,7 +18,7 @@ LinkThumbnailer.configure do |config|
|
|
18
18
|
#
|
19
19
|
# See http://www.ruby-doc.org/stdlib-2.1.1/libdoc/net/http/rdoc/Net/HTTP.html#open_timeout
|
20
20
|
#
|
21
|
-
# config.
|
21
|
+
# config.http_open_timeout = 5
|
22
22
|
|
23
23
|
# List of blacklisted urls you want to skip when searching for images.
|
24
24
|
#
|
@@ -23,10 +23,13 @@ module LinkThumbnailer
|
|
23
23
|
class Configuration
|
24
24
|
|
25
25
|
attr_accessor :redirect_limit, :blacklist_urls, :user_agent,
|
26
|
-
:verify_ssl, :
|
27
|
-
:description_min_length, :positive_regex, :negative_regex,
|
26
|
+
:verify_ssl, :http_open_timeout, :http_read_timeout, :attributes,
|
27
|
+
:graders, :description_min_length, :positive_regex, :negative_regex,
|
28
28
|
:image_limit, :image_stats
|
29
29
|
|
30
|
+
alias_method :http_timeout, :http_open_timeout
|
31
|
+
alias_method :http_timeout=, :http_open_timeout=
|
32
|
+
|
30
33
|
# Create a new instance.
|
31
34
|
#
|
32
35
|
# @return [LinkThumbnailer::Configuration]
|
@@ -34,7 +37,8 @@ module LinkThumbnailer
|
|
34
37
|
@redirect_limit = 3
|
35
38
|
@user_agent = 'link_thumbnailer'
|
36
39
|
@verify_ssl = true
|
37
|
-
@
|
40
|
+
@http_open_timeout = 5
|
41
|
+
@http_read_timeout = 5
|
38
42
|
@blacklist_urls = [
|
39
43
|
%r{^http://ad\.doubleclick\.net/},
|
40
44
|
%r{^http://b\.scorecardresearch\.com/},
|
@@ -15,14 +15,14 @@ module LinkThumbnailer
|
|
15
15
|
super(config)
|
16
16
|
end
|
17
17
|
|
18
|
-
def call(url = '', redirect_count = 0)
|
18
|
+
def call(url = '', redirect_count = 0, headers = {})
|
19
19
|
self.url = url
|
20
20
|
@redirect_count = redirect_count
|
21
21
|
|
22
22
|
raise ::LinkThumbnailer::RedirectLimit if too_many_redirections?
|
23
23
|
|
24
24
|
with_valid_url do
|
25
|
-
set_http_headers
|
25
|
+
set_http_headers(headers)
|
26
26
|
set_http_options
|
27
27
|
perform_request
|
28
28
|
end
|
@@ -35,23 +35,32 @@ module LinkThumbnailer
|
|
35
35
|
yield if block_given?
|
36
36
|
end
|
37
37
|
|
38
|
-
def set_http_headers
|
38
|
+
def set_http_headers(headers = {})
|
39
|
+
headers.each { |k, v| http.headers[k] = v }
|
39
40
|
http.headers['User-Agent'] = user_agent
|
40
41
|
http.override_headers['Accept-Encoding'] = 'none'
|
41
42
|
end
|
42
43
|
|
43
44
|
def set_http_options
|
44
45
|
http.verify_mode = ::OpenSSL::SSL::VERIFY_NONE unless ssl_required?
|
45
|
-
http.open_timeout =
|
46
|
+
http.open_timeout = http_open_timeout
|
47
|
+
http.read_timeout = http_read_timeout
|
46
48
|
http.proxy = :ENV
|
47
49
|
end
|
48
50
|
|
49
51
|
def perform_request
|
50
|
-
response
|
52
|
+
response = http.request(url)
|
53
|
+
headers = {}
|
54
|
+
headers['Cookie'] = response['Set-Cookie'] if response['Set-Cookie'].present?
|
55
|
+
|
51
56
|
case response
|
52
57
|
when ::Net::HTTPSuccess then response.body
|
53
58
|
when ::Net::HTTPRedirection
|
54
|
-
call
|
59
|
+
call(
|
60
|
+
resolve_relative_url(response['location']),
|
61
|
+
redirect_count + 1,
|
62
|
+
headers
|
63
|
+
)
|
55
64
|
else
|
56
65
|
response.error!
|
57
66
|
end
|
@@ -62,7 +71,7 @@ module LinkThumbnailer
|
|
62
71
|
end
|
63
72
|
|
64
73
|
def build_absolute_url_for(relative_url)
|
65
|
-
URI("#{url.scheme}://#{url.host}#{relative_url}")
|
74
|
+
::URI.parse("#{url.scheme}://#{url.host}#{relative_url}")
|
66
75
|
end
|
67
76
|
|
68
77
|
def redirect_limit
|
@@ -73,8 +82,12 @@ module LinkThumbnailer
|
|
73
82
|
config.user_agent
|
74
83
|
end
|
75
84
|
|
76
|
-
def
|
77
|
-
config.
|
85
|
+
def http_open_timeout
|
86
|
+
config.http_open_timeout
|
87
|
+
end
|
88
|
+
|
89
|
+
def http_read_timeout
|
90
|
+
config.http_read_timeout
|
78
91
|
end
|
79
92
|
|
80
93
|
def ssl_required?
|
data/spec/configuration_spec.rb
CHANGED
@@ -7,7 +7,8 @@ describe LinkThumbnailer::Configuration do
|
|
7
7
|
it { expect(instance.redirect_limit).to eq(3) }
|
8
8
|
it { expect(instance.user_agent).to eq('link_thumbnailer') }
|
9
9
|
it { expect(instance.verify_ssl).to eq(true) }
|
10
|
-
it { expect(instance.
|
10
|
+
it { expect(instance.http_open_timeout).to eq(5) }
|
11
|
+
it { expect(instance.http_read_timeout).to eq(5) }
|
11
12
|
it { expect(instance.blacklist_urls).to_not be_empty }
|
12
13
|
it { expect(instance.attributes).to eq([:title, :images, :description, :videos, :favicon]) }
|
13
14
|
it { expect(instance.graders).to_not be_empty }
|
@@ -17,6 +18,11 @@ describe LinkThumbnailer::Configuration do
|
|
17
18
|
it { expect(instance.image_limit).to eq(5) }
|
18
19
|
it { expect(instance.image_stats).to eq(true) }
|
19
20
|
|
21
|
+
describe "#http_timeout" do
|
22
|
+
it { expect(instance.method(:http_timeout)).to eq(instance.method(:http_open_timeout)) }
|
23
|
+
it { expect(instance.method(:http_timeout=)).to eq(instance.method(:http_open_timeout=)) }
|
24
|
+
end
|
25
|
+
|
20
26
|
describe '.config' do
|
21
27
|
|
22
28
|
it { expect(LinkThumbnailer.config).to be_a(described_class) }
|
data/spec/processor_spec.rb
CHANGED
@@ -151,17 +151,29 @@ describe LinkThumbnailer::Processor do
|
|
151
151
|
|
152
152
|
describe 'open_timeout' do
|
153
153
|
|
154
|
-
let(:
|
154
|
+
let(:http_open_timeout) { 1 }
|
155
155
|
|
156
156
|
before do
|
157
|
-
instance.stub(:
|
157
|
+
instance.stub(:http_open_timeout).and_return(http_open_timeout)
|
158
158
|
action
|
159
159
|
end
|
160
160
|
|
161
|
-
it { expect(http.open_timeout).to eq(
|
161
|
+
it { expect(http.open_timeout).to eq(http_open_timeout) }
|
162
162
|
|
163
163
|
end
|
164
164
|
|
165
|
+
describe 'read_timeout' do
|
166
|
+
|
167
|
+
let(:http_read_timeout) { 1 }
|
168
|
+
|
169
|
+
before do
|
170
|
+
instance.stub(:http_read_timeout).and_return(http_read_timeout)
|
171
|
+
action
|
172
|
+
end
|
173
|
+
|
174
|
+
it { expect(http.read_timeout).to eq(http_read_timeout) }
|
175
|
+
|
176
|
+
end
|
165
177
|
end
|
166
178
|
|
167
179
|
describe '#perform_request' do
|
@@ -200,7 +212,7 @@ describe LinkThumbnailer::Processor do
|
|
200
212
|
|
201
213
|
it 'calls call method' do
|
202
214
|
expect(instance).to receive(:resolve_relative_url)
|
203
|
-
expect(instance).to receive(:call).with(new_url, instance.redirect_count + 1)
|
215
|
+
expect(instance).to receive(:call).with(new_url, instance.redirect_count + 1, {})
|
204
216
|
action
|
205
217
|
end
|
206
218
|
|
@@ -250,16 +262,29 @@ describe LinkThumbnailer::Processor do
|
|
250
262
|
|
251
263
|
end
|
252
264
|
|
253
|
-
describe '#
|
265
|
+
describe '#http_open_timeout' do
|
266
|
+
|
267
|
+
let(:http_open_timeout) { 1 }
|
268
|
+
let(:action) { instance.send(:http_open_timeout) }
|
269
|
+
|
270
|
+
before do
|
271
|
+
instance.config.http_open_timeout = http_open_timeout
|
272
|
+
end
|
273
|
+
|
274
|
+
it { expect(action).to eq(http_open_timeout) }
|
275
|
+
|
276
|
+
end
|
277
|
+
|
278
|
+
describe '#http_read_timeout' do
|
254
279
|
|
255
|
-
let(:
|
256
|
-
let(:action) { instance.send(:
|
280
|
+
let(:http_read_timeout) { 1 }
|
281
|
+
let(:action) { instance.send(:http_read_timeout) }
|
257
282
|
|
258
283
|
before do
|
259
|
-
instance.config.
|
284
|
+
instance.config.http_read_timeout = http_read_timeout
|
260
285
|
end
|
261
286
|
|
262
|
-
it { expect(action).to eq(
|
287
|
+
it { expect(action).to eq(http_read_timeout) }
|
263
288
|
|
264
289
|
end
|
265
290
|
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: link_thumbnailer
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.5.
|
4
|
+
version: 2.5.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Pierre-Louis Gottfrois
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-
|
11
|
+
date: 2015-05-01 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -230,7 +230,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
230
230
|
version: '0'
|
231
231
|
requirements: []
|
232
232
|
rubyforge_project:
|
233
|
-
rubygems_version: 2.
|
233
|
+
rubygems_version: 2.2.2
|
234
234
|
signing_key:
|
235
235
|
specification_version: 4
|
236
236
|
summary: Ruby gem ranking images from a given URL returning an object containing images
|