link_thumbnailer 1.0.3 → 1.0.4

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore CHANGED
@@ -15,3 +15,4 @@ spec/reports
15
15
  test/tmp
16
16
  test/version_tmp
17
17
  tmp
18
+ .project
data/CHANGELOG.md CHANGED
@@ -1,3 +1,12 @@
1
+ # 1.0.4
2
+
3
+ - Fix issue #7: nil img was returned when exception is raised. Now skiping nil images in results.
4
+ - Thanks to [phlegx](https://github.com/phlegx), support for SSL and User Agent customization through configurations.
5
+
6
+ # 1.0.3
7
+
8
+ - Fix issue #5: Url was incorect in case of HTTP Redirections.
9
+
1
10
  # 1.0.2
2
11
 
3
12
  - Feature: User can now set options at runtime by passing valid options to ```generate``` method
data/README.md CHANGED
@@ -85,7 +85,7 @@ You can check whether this object is valid or not (set mandatory attributes in t
85
85
  You also can set options at runtime:
86
86
 
87
87
  object = LinkThumbnailer.generate('http://foo.com', top: 10, limit: 20, redirect_limit: 5)
88
-
88
+
89
89
  ## Preview Controller
90
90
 
91
91
  For an easy integration into your application, use the builtin `PreviewController`.
@@ -98,15 +98,15 @@ Basically, all you have to do in your view is something like this:
98
98
  <%= text_field_tag :url %>
99
99
  <%= submit_tag 'Preview' %>
100
100
  <% end %>
101
-
101
+
102
102
  Don't forget to add this anywhere in your `routes.rb` file:
103
103
 
104
104
  use_link_thumbnailer
105
-
105
+
106
106
  Note: You won't have to bother with this if you did run the installer using:
107
107
 
108
108
  $ rails g link_thumbnailer:install
109
-
109
+
110
110
  The `PreviewController` will automatically respond to json calls, returning json version of the preview object. Just like in the IRB console above.
111
111
 
112
112
  ## Configuration
@@ -137,6 +137,12 @@ In `config/initializers/link_thumbnailer.rb`
137
137
 
138
138
  # Return top 5 images only.
139
139
  # config.top = 5
140
+
141
+ # Set user agent
142
+ # config.user_agent = 'linkthumbnailer'
143
+
144
+ # Enable or disable SSL verification
145
+ # config.verify_ssl = true
140
146
  end
141
147
 
142
148
  ## Features
@@ -162,3 +168,7 @@ Coming soon:
162
168
  4. Commit your changes (`git commit -am 'Added some feature'`)
163
169
  5. Push to the branch (`git push origin my-new-feature`)
164
170
  6. Create new Pull Request
171
+
172
+ ## Contributors
173
+
174
+ - [phlegx](https://github.com/phlegx)
@@ -29,4 +29,10 @@ LinkThumbnailer.configure do |config|
29
29
 
30
30
  # Return top 5 images only.
31
31
  # config.top = 5
32
+
33
+ # Set user agent
34
+ # config.user_agent = 'linkthumbnailer'
35
+
36
+ # Enable or disable SSL verification
37
+ # config.verify_ssl = true
32
38
  end
@@ -16,7 +16,8 @@ module LinkThumbnailer
16
16
 
17
17
  if self.url.is_a?(URI::HTTP)
18
18
  http = Net::HTTP::Persistent.new('linkthumbnailer')
19
- http.headers['User-Agent'] = 'linkthumbnailer'
19
+ http.headers['User-Agent'] = LinkThumbnailer.configuration.user_agent
20
+ http.verify_mode = OpenSSL::SSL::VERIFY_NONE unless LinkThumbnailer.configuration.verify_ssl
20
21
  resp = http.request(self.url)
21
22
  case resp
22
23
  when Net::HTTPSuccess; resp.body
@@ -19,27 +19,26 @@ module LinkThumbnailer
19
19
  img_urls.each { |i|
20
20
  break if count >= LinkThumbnailer.configuration.limit
21
21
  img = parse_one(i)
22
+ next unless img
22
23
  img.extend LinkThumbnailer::ImgComparator
23
24
  imgs << img
24
25
  count += 1
25
26
  }
26
27
 
27
- imgs.sort!
28
+ imgs.sort! unless imgs.count <= 1
28
29
 
29
30
  imgs.first(LinkThumbnailer.configuration.top)
30
31
  end
31
32
 
32
33
  def parse_one(img_url)
33
- begin
34
- img_data = @fetcher.fetch(img_url)
35
- img = Magick::ImageList.new.from_blob(img_data).extend(
36
- LinkThumbnailer::WebImage
37
- )
38
- img.source_url = img_url
39
- img
40
- rescue Exception
41
- nil
42
- end
34
+ img_data = @fetcher.fetch(img_url)
35
+ img = Magick::ImageList.new.from_blob(img_data).extend(
36
+ LinkThumbnailer::WebImage
37
+ )
38
+ img.source_url = img_url
39
+ img
40
+ rescue Exception
41
+ nil
43
42
  end
44
43
 
45
44
  end
@@ -1,3 +1,3 @@
1
1
  module LinkThumbnailer
2
- VERSION = "1.0.3"
2
+ VERSION = "1.0.4"
3
3
  end
@@ -35,7 +35,9 @@ module LinkThumbnailer
35
35
  ],
36
36
  :rmagick_attributes => %w(source_url mime_type colums rows filesize number_colors),
37
37
  :limit => 10,
38
- :top => 5
38
+ :top => 5,
39
+ :user_agent => 'linkthumbnailer',
40
+ :verify_ssl => true
39
41
  )
40
42
  end
41
43
 
@@ -15,12 +15,12 @@ Gem::Specification.new do |gem|
15
15
  gem.require_paths = ["lib"]
16
16
  gem.version = LinkThumbnailer::VERSION
17
17
 
18
- gem.add_dependency(%q{nokogiri}, ['~> 1.5.5'])
19
- gem.add_dependency(%q{hashie}, ['~> 1.2.0'])
18
+ gem.add_dependency(%q{nokogiri}, ['~> 1.5.5'])
19
+ gem.add_dependency(%q{hashie}, ['~> 1.2.0'])
20
20
  gem.add_dependency(%q{net-http-persistent}, ['~> 2.7'])
21
- gem.add_dependency(%q{rmagick}, ['~> 2.13.1'])
22
- gem.add_dependency(%q{json}, ['~> 1.7.6'])
21
+ gem.add_dependency(%q{rmagick}, ['~> 2.13.1'])
22
+ gem.add_dependency(%q{json}, ['~> 1.7.6'])
23
23
 
24
- gem.add_development_dependency(%q{rspec}, ['~> 2.11.0'])
24
+ gem.add_development_dependency(%q{rspec}, ['~> 2.11.0'])
25
25
  gem.add_development_dependency(%q{webmock}, ['~> 1.8.10'])
26
26
  end
@@ -0,0 +1,11 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
5
+ <meta charset="UTF-8">
6
+ <title>Foo.com</title>
7
+ </head>
8
+
9
+ <body>
10
+ </body>
11
+ </html>
@@ -2,8 +2,9 @@ require 'spec_helper'
2
2
 
3
3
  describe LinkThumbnailer do
4
4
 
5
- let(:og_example) { File.open(File.dirname(__FILE__) + '/examples/og_example.html').read() }
6
- let(:example) { File.open(File.dirname(__FILE__) + '/examples/example.html').read() }
5
+ let(:og_example) { File.open(File.dirname(__FILE__) + '/examples/og_example.html').read() }
6
+ let(:example) { File.open(File.dirname(__FILE__) + '/examples/example.html').read() }
7
+ let(:empty_example) { File.open(File.dirname(__FILE__) + '/examples/empty_example.html').read() }
7
8
 
8
9
  it { should respond_to :configuration }
9
10
  it { should respond_to :configure }
@@ -20,12 +21,14 @@ describe LinkThumbnailer do
20
21
  before do
21
22
  LinkThumbnailer.configure {|config|
22
23
  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
24
+ config.strict = false
25
+ config.redirect_limit = 5
26
+ config.blacklist_urls = []
27
+ config.rmagick_attributes = []
28
+ config.limit = 5
29
+ config.top = 10
30
+ config.user_agent = 'linkthumbnailer'
31
+ config.verify_ssl = true
29
32
  }
30
33
  end
31
34
 
@@ -34,12 +37,14 @@ describe LinkThumbnailer do
34
37
  end
35
38
 
36
39
  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) }
40
+ specify { LinkThumbnailer.configuration.strict.should be_false }
41
+ specify { LinkThumbnailer.configuration.redirect_limit.should eq(5) }
42
+ specify { LinkThumbnailer.configuration.blacklist_urls.should eq([]) }
43
+ specify { LinkThumbnailer.configuration.rmagick_attributes.should eq([]) }
44
+ specify { LinkThumbnailer.configuration.limit.should eq(5) }
45
+ specify { LinkThumbnailer.configuration.top.should eq(10) }
46
+ specify { LinkThumbnailer.configuration.user_agent.should eq('linkthumbnailer') }
47
+ specify { LinkThumbnailer.configuration.verify_ssl.should be_true }
43
48
 
44
49
  end
45
50
 
@@ -50,17 +55,19 @@ describe LinkThumbnailer do
50
55
  end
51
56
 
52
57
  specify { LinkThumbnailer.configuration.mandatory_attributes.should eq(%w(url title images)) }
53
- specify { LinkThumbnailer.configuration.strict.should be_true }
54
- specify { LinkThumbnailer.configuration.redirect_limit.should eq(3) }
55
- specify { LinkThumbnailer.configuration.blacklist_urls.should eq([
58
+ specify { LinkThumbnailer.configuration.strict.should be_true }
59
+ specify { LinkThumbnailer.configuration.redirect_limit.should eq(3) }
60
+ specify { LinkThumbnailer.configuration.blacklist_urls.should eq([
56
61
  %r{^http://ad\.doubleclick\.net/},
57
62
  %r{^http://b\.scorecardresearch\.com/},
58
63
  %r{^http://pixel\.quantserve\.com/},
59
64
  %r{^http://s7\.addthis\.com/}
60
65
  ]) }
61
- specify { LinkThumbnailer.configuration.rmagick_attributes.should eq(%w(source_url mime_type colums rows filesize number_colors)) }
62
- specify { LinkThumbnailer.configuration.limit.should eq(10) }
63
- specify { LinkThumbnailer.configuration.top.should eq(5) }
66
+ specify { LinkThumbnailer.configuration.rmagick_attributes.should eq(%w(source_url mime_type colums rows filesize number_colors)) }
67
+ specify { LinkThumbnailer.configuration.limit.should eq(10) }
68
+ specify { LinkThumbnailer.configuration.top.should eq(5) }
69
+ specify { LinkThumbnailer.configuration.user_agent.should eq('linkthumbnailer') }
70
+ specify { LinkThumbnailer.configuration.verify_ssl.should be_true }
64
71
 
65
72
  end
66
73
 
@@ -76,33 +83,40 @@ describe LinkThumbnailer do
76
83
  context "and options" do
77
84
 
78
85
  it "should set top option" do
79
- expect { LinkThumbnailer.generate('foo', :top => 20).to change(LinkThumbnailer.configuration.top).from(5).to(20) }
86
+ expect { LinkThumbnailer.generate('foo', top: 20).to change(LinkThumbnailer.configuration.top).from(5).to(20) }
80
87
  end
81
88
 
82
89
  it "should set limit option" do
83
- expect { LinkThumbnailer.generate('foo', :limit => 20).to change(LinkThumbnailer.configuration.limit).from(10).to(20) }
90
+ expect { LinkThumbnailer.generate('foo', limit: 20).to change(LinkThumbnailer.configuration.limit).from(10).to(20) }
84
91
  end
85
92
 
86
93
  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)) }
94
+ 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
95
  end
89
96
 
90
97
  it "should set strict option" do
91
- expect { LinkThumbnailer.generate('foo', :strict => false).to change(LinkThumbnailer.configuration.strict).from(true).to(false) }
98
+ expect { LinkThumbnailer.generate('foo', strict: false).to change(LinkThumbnailer.configuration.strict).from(true).to(false) }
92
99
  end
93
100
 
94
101
  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) }
102
+ expect { LinkThumbnailer.generate('foo', redirect_limit: 5).to change(LinkThumbnailer.configuration.redirect_limit).from(3).to(5) }
96
103
  end
97
104
 
98
105
  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/}]) }
106
+ expect { LinkThumbnailer.generate('foo', blacklist_urls: [%r{^http://foo\.bar\.com/}]).to change(LinkThumbnailer.configuration.blacklist_urls).to([%r{^http://foo\.bar\.com/}]) }
100
107
  end
101
108
 
102
109
  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)) }
110
+ expect { LinkThumbnailer.generate('foo', rmagick_attributes: %w(one two)).to change(LinkThumbnailer.configuration.rmagick_attributes).to(%w(one two)) }
104
111
  end
105
112
 
113
+ it "should set user_agent option" do
114
+ expect { LinkThumbnailer.generate('foo', user_agent: 'Mac Safari').to change(LinkThumbnailer.configuration.mandatory_attributes).from('linkthumbnailer').to('Mac Safari') }
115
+ end
116
+
117
+ it "should set verify_ssl option" do
118
+ expect { LinkThumbnailer.generate('foo', verify_ssl: false).to change(LinkThumbnailer.configuration.verify_ssl).from(true).to(false) }
119
+ end
106
120
  end
107
121
 
108
122
  context "when strict" do
@@ -111,20 +125,29 @@ describe LinkThumbnailer do
111
125
 
112
126
  subject { LinkThumbnailer.generate('foo') }
113
127
 
114
- it { should be_nil }
128
+ it { expect(LinkThumbnailer.generate('foo')).to be_nil }
115
129
 
116
130
  end
117
131
 
118
132
  context "and valid" do
119
133
 
120
134
  before do
121
- stub_request(:get, "http://foo.com/").to_return(:status => 200, :body => og_example, :headers => {})
135
+ stub_request(:get, 'http://foo.com/').to_return(status: 200, body: og_example, headers: {})
122
136
  end
123
137
 
124
- subject { LinkThumbnailer.generate('http://foo.com') }
138
+ it { expect(LinkThumbnailer.generate('http://foo.com')).to_not be_nil }
139
+ it { expect(LinkThumbnailer.generate('http://foo.com')).to be_valid }
140
+
141
+ end
142
+
143
+ context "and empty" do
144
+
145
+ before do
146
+ stub_request(:get, 'http://foo.com/').to_return(status: 200, body: empty_example, headers: {})
147
+ end
125
148
 
126
- it { should_not be_nil }
127
- it { should be_valid }
149
+ it { expect(LinkThumbnailer.generate('http://foo.com/')).to be_nil }
150
+ it { expect { LinkThumbnailer.generate('http://foo.com/') }.to_not raise_exception }
128
151
 
129
152
  end
130
153
 
@@ -133,28 +156,35 @@ describe LinkThumbnailer do
133
156
  context "when not strict" do
134
157
 
135
158
  before do
136
- LinkThumbnailer.configure {|config| config.strict = false}
159
+ LinkThumbnailer.configure {|config| config.strict = false }
137
160
  end
138
161
 
139
162
  context "and not valid" do
140
163
 
141
- subject { LinkThumbnailer.generate('foo') }
142
-
143
- it { should_not be_nil }
144
- it { should be_valid }
164
+ it { expect(LinkThumbnailer.generate('foo')).to_not be_nil }
165
+ it { expect(LinkThumbnailer.generate('foo')).to be_valid }
145
166
 
146
167
  end
147
168
 
148
169
  context "and valid" do
149
170
 
150
171
  before do
151
- stub_request(:get, "http://foo.com/").to_return(:status => 200, :body => og_example, :headers => {})
172
+ stub_request(:get, 'http://foo.com/').to_return(status: 200, body: og_example, headers: {})
152
173
  end
153
174
 
154
- subject { LinkThumbnailer.generate('http://foo.com') }
175
+ it { expect(LinkThumbnailer.generate('http://foo.com')).to_not be_nil }
176
+ it { expect(LinkThumbnailer.generate('http://foo.com')).to be_valid }
177
+
178
+ end
179
+
180
+ context "and empty" do
181
+
182
+ before do
183
+ stub_request(:get, 'http://foo.com/').to_return(status: 200, body: empty_example, headers: {})
184
+ end
155
185
 
156
- it { should_not be_nil }
157
- it { should be_valid }
186
+ it { expect(LinkThumbnailer.generate('http://foo.com/')).to_not be_nil }
187
+ it { expect { LinkThumbnailer.generate('http://foo.com/') }.to_not raise_exception }
158
188
 
159
189
  end
160
190
 
@@ -23,6 +23,10 @@ describe LinkThumbnailer::WebImage do
23
23
 
24
24
  let(:attributes) { [:source_url] }
25
25
 
26
+ before do
27
+ LinkThumbnailer.config
28
+ end
29
+
26
30
  subject { foo.to_hash }
27
31
 
28
32
  it { subject.keys.should eq(attributes.map(&:to_sym)) }
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.3
4
+ version: 1.0.4
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-31 00:00:00.000000000 Z
12
+ date: 2013-05-01 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: nokogiri
@@ -160,6 +160,7 @@ files:
160
160
  - link_thumbnailer.gemspec
161
161
  - spec/doc_parser_spec.rb
162
162
  - spec/doc_spec.rb
163
+ - spec/examples/empty_example.html
163
164
  - spec/examples/example.html
164
165
  - spec/examples/og_example.html
165
166
  - spec/fetcher_spec.rb
@@ -198,6 +199,7 @@ summary: Ruby gem ranking images from a given URL returning an object containing
198
199
  test_files:
199
200
  - spec/doc_parser_spec.rb
200
201
  - spec/doc_spec.rb
202
+ - spec/examples/empty_example.html
201
203
  - spec/examples/example.html
202
204
  - spec/examples/og_example.html
203
205
  - spec/fetcher_spec.rb