link_thumbnailer 1.0.2 → 1.0.3
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/link_thumbnailer/fetcher.rb +5 -3
- data/lib/link_thumbnailer/version.rb +1 -1
- data/lib/link_thumbnailer.rb +3 -10
- data/spec/fetcher_spec.rb +34 -6
- data/spec/link_thumbnailer_spec.rb +53 -32
- metadata +2 -2
@@ -4,18 +4,20 @@ module LinkThumbnailer
|
|
4
4
|
|
5
5
|
class Fetcher
|
6
6
|
|
7
|
+
attr_accessor :url
|
8
|
+
|
7
9
|
def fetch(url, redirect_count = 0)
|
8
10
|
if redirect_count > LinkThumbnailer.configuration.redirect_limit
|
9
11
|
raise ArgumentError,
|
10
12
|
"too many redirects (#{redirect_count})"
|
11
13
|
end
|
12
14
|
|
13
|
-
|
15
|
+
self.url = url.is_a?(URI) ? url : URI(url)
|
14
16
|
|
15
|
-
if
|
17
|
+
if self.url.is_a?(URI::HTTP)
|
16
18
|
http = Net::HTTP::Persistent.new('linkthumbnailer')
|
17
19
|
http.headers['User-Agent'] = 'linkthumbnailer'
|
18
|
-
resp = http.request(
|
20
|
+
resp = http.request(self.url)
|
19
21
|
case resp
|
20
22
|
when Net::HTTPSuccess; resp.body
|
21
23
|
when Net::HTTPRedirection; fetch(resp['location'], redirect_count + 1)
|
data/lib/link_thumbnailer.rb
CHANGED
@@ -49,22 +49,15 @@ module LinkThumbnailer
|
|
49
49
|
|
50
50
|
doc = self.doc_parser.parse(self.fetcher.fetch(url), url)
|
51
51
|
|
52
|
-
self.object[:url] =
|
52
|
+
self.object[:url] = self.fetcher.url.to_s
|
53
53
|
opengraph(doc) || custom(doc)
|
54
54
|
end
|
55
55
|
|
56
56
|
private
|
57
57
|
|
58
58
|
def set_options(options)
|
59
|
-
|
60
|
-
|
61
|
-
config.strict = options[:strict] if options[:strict]
|
62
|
-
config.redirect_limit = options[:redirect_limit].to_i if options[:redirect_limit]
|
63
|
-
config.blacklist_urls = options[:blacklist_urls] if options[:blacklist_urls]
|
64
|
-
config.rmagick_attributes = options[:rmagick_attributes] if options[:rmagick_attributes]
|
65
|
-
config.limit = options[:limit].to_i if options[:limit]
|
66
|
-
config.top = options[:top].to_i if options[:top]
|
67
|
-
}
|
59
|
+
config
|
60
|
+
options.each {|k, v| config[k] = v }
|
68
61
|
end
|
69
62
|
|
70
63
|
def instantiate_classes
|
data/spec/fetcher_spec.rb
CHANGED
@@ -3,8 +3,11 @@ require 'spec_helper'
|
|
3
3
|
describe LinkThumbnailer::Fetcher do
|
4
4
|
|
5
5
|
it { should respond_to :fetch }
|
6
|
+
it { should respond_to :url }
|
7
|
+
it { should respond_to :url= }
|
6
8
|
|
7
9
|
let(:fetcher) { LinkThumbnailer::Fetcher.new }
|
10
|
+
let(:url) { 'http://foo.com' }
|
8
11
|
|
9
12
|
describe ".fetch" do
|
10
13
|
|
@@ -14,29 +17,54 @@ describe LinkThumbnailer::Fetcher do
|
|
14
17
|
|
15
18
|
context "when redirect_count is more than config" do
|
16
19
|
|
17
|
-
it { lambda { fetcher.fetch(
|
20
|
+
it { lambda { fetcher.fetch(url, 10) }.should raise_exception(ArgumentError) }
|
18
21
|
|
19
22
|
end
|
20
23
|
|
21
24
|
context "when no http error" do
|
22
25
|
|
23
26
|
before do
|
24
|
-
stub_request(:get,
|
27
|
+
stub_request(:get, url).to_return(:status => 200, :body => 'foo', :headers => {})
|
25
28
|
end
|
26
29
|
|
27
|
-
|
30
|
+
it "returns body response" do
|
31
|
+
fetcher.fetch(url).should eq('foo')
|
32
|
+
end
|
33
|
+
|
34
|
+
it "sets fetcher url" do
|
35
|
+
fetcher.fetch(url)
|
36
|
+
fetcher.url.to_s.should eq(url)
|
37
|
+
end
|
38
|
+
|
39
|
+
end
|
40
|
+
|
41
|
+
context "when http redirection" do
|
42
|
+
|
43
|
+
let(:another_url) { 'http://bar.com' }
|
28
44
|
|
29
|
-
|
45
|
+
before do
|
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
|
+
end
|
49
|
+
|
50
|
+
it "returns body response" do
|
51
|
+
fetcher.fetch(url).should eq('bar')
|
52
|
+
end
|
53
|
+
|
54
|
+
it "sets fetcher url" do
|
55
|
+
fetcher.fetch(url)
|
56
|
+
fetcher.url.to_s.should eq(another_url)
|
57
|
+
end
|
30
58
|
|
31
59
|
end
|
32
60
|
|
33
61
|
context "when http error" do
|
34
62
|
|
35
63
|
before do
|
36
|
-
stub_request(:get,
|
64
|
+
stub_request(:get, url).to_return(:status => 500, :body => 'foo', :headers => {})
|
37
65
|
end
|
38
66
|
|
39
|
-
it { lambda { fetcher.fetch(
|
67
|
+
it { lambda { fetcher.fetch(url) }.should raise_exception(Net::HTTPFatalError) }
|
40
68
|
|
41
69
|
end
|
42
70
|
|
@@ -10,41 +10,37 @@ describe LinkThumbnailer do
|
|
10
10
|
it { should respond_to :config }
|
11
11
|
it { should respond_to :generate }
|
12
12
|
|
13
|
-
describe "
|
13
|
+
describe ".configure" do
|
14
14
|
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
LinkThumbnailer.configure {|config|}
|
20
|
-
end
|
21
|
-
|
22
|
-
before do
|
23
|
-
LinkThumbnailer.configure {|config|
|
24
|
-
config.mandatory_attributes = %w(foo bar)
|
25
|
-
config.strict = false
|
26
|
-
config.redirect_limit = 5
|
27
|
-
config.blacklist_urls = []
|
28
|
-
config.rmagick_attributes = []
|
29
|
-
config.limit = 5
|
30
|
-
config.top = 10
|
31
|
-
}
|
32
|
-
end
|
33
|
-
|
34
|
-
after do
|
35
|
-
LinkThumbnailer.configuration = nil
|
36
|
-
end
|
15
|
+
it "should yields self" do
|
16
|
+
LinkThumbnailer.should_receive(:configure).and_yield(LinkThumbnailer)
|
17
|
+
LinkThumbnailer.configure {|config|}
|
18
|
+
end
|
37
19
|
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
20
|
+
before do
|
21
|
+
LinkThumbnailer.configure {|config|
|
22
|
+
config.mandatory_attributes = %w(foo bar)
|
23
|
+
config.strict = false
|
24
|
+
config.redirect_limit = 5
|
25
|
+
config.blacklist_urls = []
|
26
|
+
config.rmagick_attributes = []
|
27
|
+
config.limit = 5
|
28
|
+
config.top = 10
|
29
|
+
}
|
30
|
+
end
|
45
31
|
|
32
|
+
after do
|
33
|
+
LinkThumbnailer.configuration = nil
|
46
34
|
end
|
47
35
|
|
36
|
+
specify { LinkThumbnailer.configuration.mandatory_attributes.should eq(%w(foo bar)) }
|
37
|
+
specify { LinkThumbnailer.configuration.strict.should be_false }
|
38
|
+
specify { LinkThumbnailer.configuration.redirect_limit.should eq(5) }
|
39
|
+
specify { LinkThumbnailer.configuration.blacklist_urls.should eq([]) }
|
40
|
+
specify { LinkThumbnailer.configuration.rmagick_attributes.should eq([]) }
|
41
|
+
specify { LinkThumbnailer.configuration.limit.should eq(5) }
|
42
|
+
specify { LinkThumbnailer.configuration.top.should eq(10) }
|
43
|
+
|
48
44
|
end
|
49
45
|
|
50
46
|
context "default values" do
|
@@ -68,7 +64,12 @@ describe LinkThumbnailer do
|
|
68
64
|
|
69
65
|
end
|
70
66
|
|
71
|
-
|
67
|
+
describe ".generate" do
|
68
|
+
|
69
|
+
it "should set default options" do
|
70
|
+
LinkThumbnailer.should_receive(:config)
|
71
|
+
LinkThumbnailer.generate('foo')
|
72
|
+
end
|
72
73
|
|
73
74
|
context "with valid arguments" do
|
74
75
|
|
@@ -79,7 +80,27 @@ describe LinkThumbnailer do
|
|
79
80
|
end
|
80
81
|
|
81
82
|
it "should set limit option" do
|
82
|
-
expect { LinkThumbnailer.generate('foo', :limit => 20).to change(LinkThumbnailer.configuration.
|
83
|
+
expect { LinkThumbnailer.generate('foo', :limit => 20).to change(LinkThumbnailer.configuration.limit).from(10).to(20) }
|
84
|
+
end
|
85
|
+
|
86
|
+
it "should set mandatory_attributes option" do
|
87
|
+
expect { LinkThumbnailer.generate('foo', :mandatory_attributes => %w(one two)).to change(LinkThumbnailer.configuration.mandatory_attributes).from(%w(url title images)).to(%w(one two)) }
|
88
|
+
end
|
89
|
+
|
90
|
+
it "should set strict option" do
|
91
|
+
expect { LinkThumbnailer.generate('foo', :strict => false).to change(LinkThumbnailer.configuration.strict).from(true).to(false) }
|
92
|
+
end
|
93
|
+
|
94
|
+
it "should set redirect_limit option" do
|
95
|
+
expect { LinkThumbnailer.generate('foo', :redirect_limit => 5).to change(LinkThumbnailer.configuration.redirect_limit).from(3).to(5) }
|
96
|
+
end
|
97
|
+
|
98
|
+
it "should set blacklist_urls option" do
|
99
|
+
expect { LinkThumbnailer.generate('foo', :blacklist_urls => [%r{^http://foo\.bar\.com/}]).to change(LinkThumbnailer.configuration.blacklist_urls).to([%r{^http://foo\.bar\.com/}]) }
|
100
|
+
end
|
101
|
+
|
102
|
+
it "should set rmagick_attributes option" do
|
103
|
+
expect { LinkThumbnailer.generate('foo', :rmagick_attributes => %w(one two)).to change(LinkThumbnailer.configuration.rmagick_attributes).to(%w(one two)) }
|
83
104
|
end
|
84
105
|
|
85
106
|
end
|
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.3
|
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-03-
|
12
|
+
date: 2013-03-31 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: nokogiri
|