link_thumbnailer 1.0.5 → 1.0.6
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.
- data/CHANGELOG.md +4 -0
- data/app/controllers/link_thumbnailer/previews_controller.rb +1 -1
- data/lib/link_thumbnailer.rb +18 -13
- data/lib/link_thumbnailer/{config.rb → configuration.rb} +0 -0
- data/lib/link_thumbnailer/doc.rb +4 -4
- data/lib/link_thumbnailer/fetcher.rb +9 -10
- data/lib/link_thumbnailer/img_parser.rb +4 -4
- data/lib/link_thumbnailer/opengraph.rb +1 -1
- data/lib/link_thumbnailer/rails/routes.rb +4 -4
- data/lib/link_thumbnailer/rails/routes/mapping.rb +5 -5
- data/lib/link_thumbnailer/version.rb +1 -1
- data/lib/link_thumbnailer/web_image.rb +3 -3
- data/spec/doc_parser_spec.rb +19 -1
- data/spec/fetcher_spec.rb +6 -6
- data/spec/img_url_filter_spec.rb +2 -2
- data/spec/opengraph_spec.rb +1 -1
- metadata +3 -3
data/CHANGELOG.md
CHANGED
data/lib/link_thumbnailer.rb
CHANGED
@@ -1,5 +1,5 @@
|
|
1
1
|
require 'link_thumbnailer/engine' if defined? Rails
|
2
|
-
require 'link_thumbnailer/
|
2
|
+
require 'link_thumbnailer/configuration'
|
3
3
|
require 'link_thumbnailer/object'
|
4
4
|
require 'link_thumbnailer/fetcher'
|
5
5
|
require 'link_thumbnailer/doc_parser'
|
@@ -24,21 +24,21 @@ module LinkThumbnailer
|
|
24
24
|
|
25
25
|
def config
|
26
26
|
self.configuration ||= Configuration.new(
|
27
|
-
:
|
28
|
-
:
|
29
|
-
:
|
30
|
-
:
|
27
|
+
mandatory_attributes: %w(url title images),
|
28
|
+
strict: true,
|
29
|
+
redirect_limit: 3,
|
30
|
+
blacklist_urls: [
|
31
31
|
%r{^http://ad\.doubleclick\.net/},
|
32
32
|
%r{^http://b\.scorecardresearch\.com/},
|
33
33
|
%r{^http://pixel\.quantserve\.com/},
|
34
34
|
%r{^http://s7\.addthis\.com/}
|
35
35
|
],
|
36
|
-
:
|
37
|
-
:
|
38
|
-
:
|
39
|
-
:
|
40
|
-
:
|
41
|
-
:
|
36
|
+
rmagick_attributes: %w(source_url mime_type colums rows filesize number_colors),
|
37
|
+
limit: 10,
|
38
|
+
top: 5,
|
39
|
+
user_agent: 'linkthumbnailer',
|
40
|
+
verify_ssl: true,
|
41
|
+
http_timeout: 5
|
42
42
|
)
|
43
43
|
end
|
44
44
|
|
@@ -72,19 +72,24 @@ module LinkThumbnailer
|
|
72
72
|
end
|
73
73
|
|
74
74
|
def opengraph(doc)
|
75
|
+
return nil unless opengraph?(doc)
|
75
76
|
self.object = LinkThumbnailer::Opengraph.parse(self.object, doc)
|
76
77
|
return self.object if self.object.valid?
|
77
78
|
nil
|
78
79
|
end
|
79
80
|
|
80
81
|
def custom(doc)
|
81
|
-
self.object[:title]
|
82
|
+
self.object[:title] = doc.title
|
82
83
|
self.object[:description] = doc.description
|
83
|
-
self.object[:images]
|
84
|
+
self.object[:images] = self.img_parser.parse(doc.img_abs_urls.dup)
|
84
85
|
return self.object if self.object.valid?
|
85
86
|
nil
|
86
87
|
end
|
87
88
|
|
89
|
+
def opengraph?(doc)
|
90
|
+
!doc.xpath('//meta[starts-with(@property, "og:") and @content]').empty?
|
91
|
+
end
|
92
|
+
|
88
93
|
end
|
89
94
|
|
90
95
|
end
|
File without changes
|
data/lib/link_thumbnailer/doc.rb
CHANGED
@@ -8,22 +8,21 @@ module LinkThumbnailer
|
|
8
8
|
|
9
9
|
def fetch(url, redirect_count = 0)
|
10
10
|
if redirect_count > LinkThumbnailer.configuration.redirect_limit
|
11
|
-
raise ArgumentError,
|
12
|
-
"too many redirects (#{redirect_count})"
|
11
|
+
raise ArgumentError, "too many redirects (#{redirect_count})"
|
13
12
|
end
|
14
13
|
|
15
14
|
self.url = url.is_a?(URI) ? url : URI(url)
|
16
15
|
|
17
16
|
if self.url.is_a?(URI::HTTP)
|
18
|
-
http
|
19
|
-
http.headers['User-Agent']
|
20
|
-
http.verify_mode
|
21
|
-
http.open_timeout
|
22
|
-
resp
|
17
|
+
http = Net::HTTP::Persistent.new('linkthumbnailer')
|
18
|
+
http.headers['User-Agent'] = LinkThumbnailer.configuration.user_agent
|
19
|
+
http.verify_mode = OpenSSL::SSL::VERIFY_NONE unless LinkThumbnailer.configuration.verify_ssl
|
20
|
+
http.open_timeout = LinkThumbnailer.configuration.http_timeout
|
21
|
+
resp = http.request(self.url)
|
23
22
|
case resp
|
24
|
-
|
25
|
-
|
26
|
-
|
23
|
+
when Net::HTTPSuccess then resp.body
|
24
|
+
when Net::HTTPRedirection then fetch(resp['location'], redirect_count + 1)
|
25
|
+
else resp.error!
|
27
26
|
end
|
28
27
|
end
|
29
28
|
end
|
@@ -5,8 +5,8 @@ module LinkThumbnailer
|
|
5
5
|
class ImgParser
|
6
6
|
|
7
7
|
def initialize(fetcher, img_url_filter)
|
8
|
-
@fetcher
|
9
|
-
@img_url_filters
|
8
|
+
@fetcher = fetcher
|
9
|
+
@img_url_filters = [*img_url_filter]
|
10
10
|
end
|
11
11
|
|
12
12
|
def parse(img_urls)
|
@@ -14,7 +14,7 @@ module LinkThumbnailer
|
|
14
14
|
img_urls.delete_if { |i| filter.reject?(i) }
|
15
15
|
end
|
16
16
|
|
17
|
-
imgs
|
17
|
+
imgs = []
|
18
18
|
count = 0
|
19
19
|
img_urls.each { |i|
|
20
20
|
break if count >= LinkThumbnailer.configuration.limit
|
@@ -37,7 +37,7 @@ module LinkThumbnailer
|
|
37
37
|
)
|
38
38
|
img.source_url = img_url
|
39
39
|
img
|
40
|
-
rescue
|
40
|
+
rescue StandardError
|
41
41
|
nil
|
42
42
|
end
|
43
43
|
|
@@ -12,7 +12,7 @@ module LinkThumbnailer
|
|
12
12
|
end
|
13
13
|
|
14
14
|
def self.install!
|
15
|
-
ActionDispatch::Routing::Mapper.send
|
15
|
+
ActionDispatch::Routing::Mapper.send(:include, LinkThumbnailer::Rails::Routes::Helper)
|
16
16
|
end
|
17
17
|
|
18
18
|
attr_accessor :routes
|
@@ -23,7 +23,7 @@ module LinkThumbnailer
|
|
23
23
|
|
24
24
|
def generate_routes!(options)
|
25
25
|
@mapping = Mapper.new.map(&@options)
|
26
|
-
routes.scope 'link', :
|
26
|
+
routes.scope 'link', as: 'link' do
|
27
27
|
map_route(:previews, :preview_routes)
|
28
28
|
end
|
29
29
|
end
|
@@ -37,8 +37,8 @@ module LinkThumbnailer
|
|
37
37
|
end
|
38
38
|
|
39
39
|
def preview_routes(mapping)
|
40
|
-
routes.scope :
|
41
|
-
routes.match 'preview', :
|
40
|
+
routes.scope controller: mapping[:controllers] do
|
41
|
+
routes.match 'preview', via: :post, action: :create, as: mapping[:as], defaults: { format: 'json' }
|
42
42
|
end
|
43
43
|
end
|
44
44
|
|
@@ -7,11 +7,11 @@ module LinkThumbnailer
|
|
7
7
|
|
8
8
|
def initialize
|
9
9
|
@controllers = {
|
10
|
-
:
|
10
|
+
previews: 'link_thumbnailer/previews'
|
11
11
|
}
|
12
12
|
|
13
13
|
@as = {
|
14
|
-
:
|
14
|
+
previews: :preview
|
15
15
|
}
|
16
16
|
|
17
17
|
@skips = []
|
@@ -19,13 +19,13 @@ module LinkThumbnailer
|
|
19
19
|
|
20
20
|
def [](routes)
|
21
21
|
{
|
22
|
-
:
|
23
|
-
:
|
22
|
+
controllers: @controllers[routes],
|
23
|
+
as: @as[routes]
|
24
24
|
}
|
25
25
|
end
|
26
26
|
|
27
27
|
def skipped?(controller)
|
28
|
-
|
28
|
+
@skips.include?(controller)
|
29
29
|
end
|
30
30
|
end
|
31
31
|
end
|
@@ -6,9 +6,9 @@ module LinkThumbnailer
|
|
6
6
|
def to_hash
|
7
7
|
result = {}
|
8
8
|
LinkThumbnailer.configuration.rmagick_attributes.each {|m|
|
9
|
-
k
|
10
|
-
result[k] = self.send(m)
|
11
|
-
result[k] = result[k].to_s
|
9
|
+
k = m.to_sym
|
10
|
+
result[k] = self.send(m) if self.respond_to?(m)
|
11
|
+
result[k] = result[k].to_s if result[k].is_a?(URI)
|
12
12
|
}
|
13
13
|
|
14
14
|
result
|
data/spec/doc_parser_spec.rb
CHANGED
@@ -2,6 +2,24 @@ require 'spec_helper'
|
|
2
2
|
|
3
3
|
describe LinkThumbnailer::DocParser do
|
4
4
|
|
5
|
-
it { should respond_to
|
5
|
+
it { should respond_to(:parse).with(1).arguments }
|
6
|
+
|
7
|
+
let(:instance) { LinkThumbnailer::DocParser.new }
|
8
|
+
|
9
|
+
describe "#parse" do
|
10
|
+
|
11
|
+
let(:source_url) { 'http://foo.com' }
|
12
|
+
|
13
|
+
subject { instance.parse('', source_url) }
|
14
|
+
|
15
|
+
it { expect(subject.source_url).to eq(source_url) }
|
16
|
+
it { expect(subject).to respond_to(:doc_base_href) }
|
17
|
+
it { expect(subject).to respond_to(:img_srcs) }
|
18
|
+
it { expect(subject).to respond_to(:img_abs_urls) }
|
19
|
+
it { expect(subject).to respond_to(:title) }
|
20
|
+
it { expect(subject).to respond_to(:description) }
|
21
|
+
it { expect(subject).to respond_to(:source_url) }
|
22
|
+
|
23
|
+
end
|
6
24
|
|
7
25
|
end
|
data/spec/fetcher_spec.rb
CHANGED
@@ -17,14 +17,14 @@ describe LinkThumbnailer::Fetcher do
|
|
17
17
|
|
18
18
|
context "when redirect_count is more than config" do
|
19
19
|
|
20
|
-
it {
|
20
|
+
it { expect { fetcher.fetch(url, 10) }.to raise_exception(ArgumentError) }
|
21
21
|
|
22
22
|
end
|
23
23
|
|
24
24
|
context "when no http error" do
|
25
25
|
|
26
26
|
before do
|
27
|
-
stub_request(:get, url).to_return(:
|
27
|
+
stub_request(:get, url).to_return(status: 200, body: 'foo', headers: {})
|
28
28
|
end
|
29
29
|
|
30
30
|
it "returns body response" do
|
@@ -43,8 +43,8 @@ describe LinkThumbnailer::Fetcher do
|
|
43
43
|
let(:another_url) { 'http://bar.com' }
|
44
44
|
|
45
45
|
before do
|
46
|
-
stub_request(:get, url).to_return(:
|
47
|
-
stub_request(:get, another_url).to_return(:
|
46
|
+
stub_request(:get, url).to_return(status: 300, body: 'foo', headers: { 'Location' => another_url })
|
47
|
+
stub_request(:get, another_url).to_return(status: 200, body: 'bar', headers: {})
|
48
48
|
end
|
49
49
|
|
50
50
|
it "returns body response" do
|
@@ -61,10 +61,10 @@ describe LinkThumbnailer::Fetcher do
|
|
61
61
|
context "when http error" do
|
62
62
|
|
63
63
|
before do
|
64
|
-
stub_request(:get, url).to_return(:
|
64
|
+
stub_request(:get, url).to_return(status: 500, body: 'foo', headers: {})
|
65
65
|
end
|
66
66
|
|
67
|
-
it {
|
67
|
+
it { expect { fetcher.fetch(url) }.to raise_exception(Net::HTTPFatalError) }
|
68
68
|
|
69
69
|
end
|
70
70
|
|
data/spec/img_url_filter_spec.rb
CHANGED
@@ -16,13 +16,13 @@ describe LinkThumbnailer::ImgUrlFilter do
|
|
16
16
|
|
17
17
|
context "when img_url does not contain any blacklisted urls" do
|
18
18
|
|
19
|
-
it { img_url_filter.reject?('http://valid.com/foo/bar.png').
|
19
|
+
it { expect(img_url_filter.reject?('http://valid.com/foo/bar.png')).to be_false }
|
20
20
|
|
21
21
|
end
|
22
22
|
|
23
23
|
context "when img_url does contain any blacklisted urls" do
|
24
24
|
|
25
|
-
it { img_url_filter.reject?('http://not_valid.net/foo/bar.png').
|
25
|
+
it { expect(img_url_filter.reject?('http://not_valid.net/foo/bar.png')).to be_true }
|
26
26
|
|
27
27
|
end
|
28
28
|
|
data/spec/opengraph_spec.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: link_thumbnailer
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.6
|
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: 2013-05-
|
12
|
+
date: 2013-05-04 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: nokogiri
|
@@ -142,7 +142,7 @@ files:
|
|
142
142
|
- lib/generators/link_thumbnailer/install_generator.rb
|
143
143
|
- lib/generators/templates/initializer.rb
|
144
144
|
- lib/link_thumbnailer.rb
|
145
|
-
- lib/link_thumbnailer/
|
145
|
+
- lib/link_thumbnailer/configuration.rb
|
146
146
|
- lib/link_thumbnailer/doc.rb
|
147
147
|
- lib/link_thumbnailer/doc_parser.rb
|
148
148
|
- lib/link_thumbnailer/engine.rb
|