rcarvalho-link_thumbnailer 1.0.9.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.
Files changed (43) hide show
  1. data/.gitignore +19 -0
  2. data/.rspec +2 -0
  3. data/.travis.yml +6 -0
  4. data/CHANGELOG.md +91 -0
  5. data/Gemfile +12 -0
  6. data/LICENSE +22 -0
  7. data/README.md +184 -0
  8. data/Rakefile +7 -0
  9. data/app/controllers/link_thumbnailer/application_controller.rb +4 -0
  10. data/app/controllers/link_thumbnailer/previews_controller.rb +11 -0
  11. data/lib/generators/link_thumbnailer/install_generator.rb +19 -0
  12. data/lib/generators/templates/initializer.rb +41 -0
  13. data/lib/link_thumbnailer.rb +96 -0
  14. data/lib/link_thumbnailer/configuration.rb +6 -0
  15. data/lib/link_thumbnailer/doc.rb +65 -0
  16. data/lib/link_thumbnailer/doc_parser.rb +15 -0
  17. data/lib/link_thumbnailer/engine.rb +9 -0
  18. data/lib/link_thumbnailer/fetcher.rb +34 -0
  19. data/lib/link_thumbnailer/img_comparator.rb +18 -0
  20. data/lib/link_thumbnailer/img_parser.rb +46 -0
  21. data/lib/link_thumbnailer/img_url_filter.rb +13 -0
  22. data/lib/link_thumbnailer/object.rb +41 -0
  23. data/lib/link_thumbnailer/opengraph.rb +20 -0
  24. data/lib/link_thumbnailer/rails/routes.rb +47 -0
  25. data/lib/link_thumbnailer/rails/routes/mapper.rb +30 -0
  26. data/lib/link_thumbnailer/rails/routes/mapping.rb +33 -0
  27. data/lib/link_thumbnailer/version.rb +3 -0
  28. data/lib/link_thumbnailer/web_image.rb +18 -0
  29. data/link_thumbnailer.gemspec +28 -0
  30. data/spec/doc_parser_spec.rb +25 -0
  31. data/spec/doc_spec.rb +23 -0
  32. data/spec/examples/empty_example.html +11 -0
  33. data/spec/examples/example.html +363 -0
  34. data/spec/examples/og_example.html +12 -0
  35. data/spec/fetcher_spec.rb +97 -0
  36. data/spec/img_comparator_spec.rb +16 -0
  37. data/spec/img_url_filter_spec.rb +31 -0
  38. data/spec/link_thumbnailer_spec.rb +205 -0
  39. data/spec/object_spec.rb +130 -0
  40. data/spec/opengraph_spec.rb +7 -0
  41. data/spec/spec_helper.rb +13 -0
  42. data/spec/web_image_spec.rb +57 -0
  43. metadata +245 -0
@@ -0,0 +1,16 @@
1
+ require 'spec_helper'
2
+
3
+ describe LinkThumbnailer::ImgComparator do
4
+
5
+ class Foo
6
+ end
7
+
8
+ let(:foo) { Foo.new }
9
+
10
+ before do
11
+ foo.extend(LinkThumbnailer::ImgComparator)
12
+ end
13
+
14
+ it { should respond_to :<=> }
15
+
16
+ end
@@ -0,0 +1,31 @@
1
+ require 'spec_helper'
2
+
3
+ describe LinkThumbnailer::ImgUrlFilter do
4
+
5
+ it { should respond_to :reject? }
6
+
7
+ describe ".reject?" do
8
+
9
+ let(:img_url_filter) { LinkThumbnailer::ImgUrlFilter.new }
10
+
11
+ before do
12
+ LinkThumbnailer.configure {|config| config.blacklist_urls = [
13
+ %r{^http://not_valid\.net/}
14
+ ]}
15
+ end
16
+
17
+ context "when img_url does not contain any blacklisted urls" do
18
+
19
+ it { expect(img_url_filter.reject?('http://valid.com/foo/bar.png')).to be_false }
20
+
21
+ end
22
+
23
+ context "when img_url does contain any blacklisted urls" do
24
+
25
+ it { expect(img_url_filter.reject?('http://not_valid.net/foo/bar.png')).to be_true }
26
+
27
+ end
28
+
29
+ end
30
+
31
+ end
@@ -0,0 +1,205 @@
1
+ require 'spec_helper'
2
+
3
+ describe LinkThumbnailer do
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() }
7
+ let(:empty_example) { File.open(File.dirname(__FILE__) + '/examples/empty_example.html').read() }
8
+
9
+ it { should respond_to :configuration }
10
+ it { should respond_to :configure }
11
+ it { should respond_to :config }
12
+ it { should respond_to :generate }
13
+
14
+ describe ".configure" do
15
+
16
+ it "should yields self" do
17
+ LinkThumbnailer.should_receive(:configure).and_yield(LinkThumbnailer)
18
+ LinkThumbnailer.configure {|config|}
19
+ end
20
+
21
+ before do
22
+ LinkThumbnailer.configure {|config|
23
+ config.mandatory_attributes = %w(foo bar)
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
32
+ config.http_timeout = 5
33
+ }
34
+ end
35
+
36
+ after do
37
+ LinkThumbnailer.configuration = nil
38
+ end
39
+
40
+ specify { LinkThumbnailer.configuration.mandatory_attributes.should eq(%w(foo bar)) }
41
+ specify { LinkThumbnailer.configuration.strict.should be_false }
42
+ specify { LinkThumbnailer.configuration.redirect_limit.should eq(5) }
43
+ specify { LinkThumbnailer.configuration.blacklist_urls.should eq([]) }
44
+ specify { LinkThumbnailer.configuration.rmagick_attributes.should eq([]) }
45
+ specify { LinkThumbnailer.configuration.limit.should eq(5) }
46
+ specify { LinkThumbnailer.configuration.top.should eq(10) }
47
+ specify { LinkThumbnailer.configuration.user_agent.should eq('linkthumbnailer') }
48
+ specify { LinkThumbnailer.configuration.verify_ssl.should be_true }
49
+ specify { LinkThumbnailer.configuration.http_timeout.should eq(5) }
50
+
51
+ end
52
+
53
+ context "default values" do
54
+
55
+ before do
56
+ LinkThumbnailer.configure {|config| }
57
+ end
58
+
59
+ specify { LinkThumbnailer.configuration.mandatory_attributes.should eq(%w(url title images)) }
60
+ specify { LinkThumbnailer.configuration.strict.should be_true }
61
+ specify { LinkThumbnailer.configuration.redirect_limit.should eq(3) }
62
+ specify { LinkThumbnailer.configuration.blacklist_urls.should eq([
63
+ %r{^http://ad\.doubleclick\.net/},
64
+ %r{^http://b\.scorecardresearch\.com/},
65
+ %r{^http://pixel\.quantserve\.com/},
66
+ %r{^http://s7\.addthis\.com/}
67
+ ]) }
68
+ specify { LinkThumbnailer.configuration.rmagick_attributes.should eq(%w(source_url mime_type colums rows filesize number_colors)) }
69
+ specify { LinkThumbnailer.configuration.limit.should eq(10) }
70
+ specify { LinkThumbnailer.configuration.top.should eq(5) }
71
+ specify { LinkThumbnailer.configuration.user_agent.should eq('linkthumbnailer') }
72
+ specify { LinkThumbnailer.configuration.verify_ssl.should be_true }
73
+ specify { LinkThumbnailer.configuration.http_timeout.should eq(5) }
74
+
75
+ end
76
+
77
+ describe ".generate" do
78
+
79
+ it "should set default options" do
80
+ LinkThumbnailer.should_receive(:config)
81
+ LinkThumbnailer.generate('foo')
82
+ end
83
+
84
+ context "with valid arguments" do
85
+
86
+ context "and options" do
87
+
88
+ it "should set top option" do
89
+ expect { LinkThumbnailer.generate('foo', top: 20).to change(LinkThumbnailer.configuration.top).from(5).to(20) }
90
+ end
91
+
92
+ it "should set limit option" do
93
+ expect { LinkThumbnailer.generate('foo', limit: 20).to change(LinkThumbnailer.configuration.limit).from(10).to(20) }
94
+ end
95
+
96
+ it "should set mandatory_attributes option" do
97
+ expect { LinkThumbnailer.generate('foo', mandatory_attributes: %w(one two)).to change(LinkThumbnailer.configuration.mandatory_attributes).from(%w(url title images)).to(%w(one two)) }
98
+ end
99
+
100
+ it "should set strict option" do
101
+ expect { LinkThumbnailer.generate('foo', strict: false).to change(LinkThumbnailer.configuration.strict).from(true).to(false) }
102
+ end
103
+
104
+ it "should set redirect_limit option" do
105
+ expect { LinkThumbnailer.generate('foo', redirect_limit: 5).to change(LinkThumbnailer.configuration.redirect_limit).from(3).to(5) }
106
+ end
107
+
108
+ it "should set blacklist_urls option" do
109
+ expect { LinkThumbnailer.generate('foo', blacklist_urls: [%r{^http://foo\.bar\.com/}]).to change(LinkThumbnailer.configuration.blacklist_urls).to([%r{^http://foo\.bar\.com/}]) }
110
+ end
111
+
112
+ it "should set rmagick_attributes option" do
113
+ expect { LinkThumbnailer.generate('foo', rmagick_attributes: %w(one two)).to change(LinkThumbnailer.configuration.rmagick_attributes).to(%w(one two)) }
114
+ end
115
+
116
+ it "should set user_agent option" do
117
+ expect { LinkThumbnailer.generate('foo', user_agent: 'Mac Safari').to change(LinkThumbnailer.configuration.mandatory_attributes).from('linkthumbnailer').to('Mac Safari') }
118
+ end
119
+
120
+ it "should set verify_ssl option" do
121
+ expect { LinkThumbnailer.generate('foo', verify_ssl: false).to change(LinkThumbnailer.configuration.verify_ssl).from(true).to(false) }
122
+ end
123
+
124
+ it "should set http_timeout option" do
125
+ expect { LinkThumbnailer.generate('foo', http_timeout: 2).to change(LinkThumbnailer.configuration.http_timeout).from(5).to(2) }
126
+ end
127
+
128
+ end
129
+
130
+ context "when strict" do
131
+
132
+ context "and not valid" do
133
+
134
+ subject { LinkThumbnailer.generate('foo') }
135
+
136
+ it { expect(LinkThumbnailer.generate('foo')).to be_nil }
137
+
138
+ end
139
+
140
+ context "and valid" do
141
+
142
+ before do
143
+ stub_request(:get, 'http://foo.com/').to_return(status: 200, body: og_example, headers: {})
144
+ end
145
+
146
+ it { expect(LinkThumbnailer.generate('http://foo.com')).to_not be_nil }
147
+ it { expect(LinkThumbnailer.generate('http://foo.com')).to be_valid }
148
+
149
+ end
150
+
151
+ context "and empty" do
152
+
153
+ before do
154
+ stub_request(:get, 'http://foo.com/').to_return(status: 200, body: empty_example, headers: {})
155
+ end
156
+
157
+ it { expect(LinkThumbnailer.generate('http://foo.com/')).to be_nil }
158
+ it { expect { LinkThumbnailer.generate('http://foo.com/') }.to_not raise_exception }
159
+
160
+ end
161
+
162
+ end
163
+
164
+ context "when not strict" do
165
+
166
+ before do
167
+ LinkThumbnailer.configure {|config| config.strict = false }
168
+ end
169
+
170
+ context "and not valid" do
171
+
172
+ it { expect(LinkThumbnailer.generate('foo')).to_not be_nil }
173
+ it { expect(LinkThumbnailer.generate('foo')).to be_valid }
174
+
175
+ end
176
+
177
+ context "and valid" do
178
+
179
+ before do
180
+ stub_request(:get, 'http://foo.com/').to_return(status: 200, body: og_example, headers: {})
181
+ end
182
+
183
+ it { expect(LinkThumbnailer.generate('http://foo.com')).to_not be_nil }
184
+ it { expect(LinkThumbnailer.generate('http://foo.com')).to be_valid }
185
+
186
+ end
187
+
188
+ context "and empty" do
189
+
190
+ before do
191
+ stub_request(:get, 'http://foo.com/').to_return(status: 200, body: empty_example, headers: {})
192
+ end
193
+
194
+ it { expect(LinkThumbnailer.generate('http://foo.com/')).to_not be_nil }
195
+ it { expect { LinkThumbnailer.generate('http://foo.com/') }.to_not raise_exception }
196
+
197
+ end
198
+
199
+ end
200
+
201
+ end
202
+
203
+ end
204
+
205
+ end
@@ -0,0 +1,130 @@
1
+ require 'spec_helper'
2
+
3
+ describe LinkThumbnailer::Object do
4
+
5
+ it { should respond_to :valid? }
6
+ it { should respond_to :to_hash }
7
+ it { should respond_to :to_json }
8
+
9
+ let(:object) { LinkThumbnailer::Object.new }
10
+
11
+ describe ".method_missing" do
12
+
13
+ before do
14
+ object[:foo] = 'foo'
15
+ end
16
+
17
+ subject { object }
18
+
19
+ it { subject.foo.should eq('foo') }
20
+ it { subject.foo?.should be_true }
21
+ it { subject.bar.should be_nil }
22
+ it { subject.bar?.should be_false }
23
+
24
+ end
25
+
26
+ describe ".valid" do
27
+
28
+ before do
29
+ LinkThumbnailer.configure {|config|}
30
+ end
31
+
32
+ context "when strict" do
33
+
34
+ before do
35
+ LinkThumbnailer.configure { |config| config.strict = true }
36
+ end
37
+
38
+ context "and valid" do
39
+
40
+ before do
41
+ LinkThumbnailer.configuration.mandatory_attributes.each { |a| object[a] = 'foo' }
42
+ end
43
+
44
+ subject { object }
45
+
46
+ it { should be_valid }
47
+ it { subject.keys.should eq(LinkThumbnailer.configuration.mandatory_attributes) }
48
+ it { subject.values.should include 'foo' }
49
+
50
+ end
51
+
52
+ context "and not valid" do
53
+
54
+ subject { object }
55
+
56
+ it { should_not be_valid }
57
+ it { subject.keys.should be_empty }
58
+ it { subject.values.should be_empty }
59
+
60
+ end
61
+
62
+ end
63
+
64
+ context "when not strict" do
65
+
66
+ before do
67
+ LinkThumbnailer.configure { |config| config.strict = false }
68
+ end
69
+
70
+ context "and empty" do
71
+
72
+ subject { object }
73
+
74
+ it { should_not be_valid }
75
+ it { subject.keys.should be_empty }
76
+ it { subject.values.should be_empty }
77
+
78
+ end
79
+
80
+ context "and not empty" do
81
+
82
+ before do
83
+ object[:foo] = 'foo'
84
+ end
85
+
86
+ subject { object }
87
+
88
+ it { should be_valid }
89
+ it { subject.foo.should eq('foo') }
90
+
91
+ end
92
+
93
+ end
94
+
95
+ end
96
+
97
+ context "dealing with WebImage module" do
98
+
99
+ class Foo
100
+ end
101
+
102
+ let(:foo) { Foo.new }
103
+
104
+ before do
105
+ foo.extend LinkThumbnailer::WebImage
106
+ object['images'] = [foo]
107
+ end
108
+
109
+ describe ".to_hash" do
110
+
111
+ subject { object.to_hash }
112
+
113
+ it { subject.is_a?(Hash).should be_true }
114
+ it { should include('images') }
115
+ it { subject['images'].is_a?(Array).should be_true }
116
+
117
+ end
118
+
119
+ describe ".to_json" do
120
+
121
+ subject { object.to_json }
122
+
123
+ it { subject.is_a?(String).should be_true }
124
+ it { should include('images') }
125
+
126
+ end
127
+
128
+ end
129
+
130
+ end
@@ -0,0 +1,7 @@
1
+ require 'spec_helper'
2
+
3
+ describe LinkThumbnailer::Opengraph do
4
+
5
+ it { LinkThumbnailer::Opengraph.should respond_to(:parse).with(2).arguments }
6
+
7
+ end
@@ -0,0 +1,13 @@
1
+ require 'simplecov'
2
+ require 'coveralls'
3
+ Coveralls.wear!
4
+
5
+ require 'link_thumbnailer'
6
+ require 'rspec'
7
+ require 'webmock/rspec'
8
+ require 'json'
9
+
10
+ SimpleCov.formatter = Coveralls::SimpleCov::Formatter
11
+
12
+ RSpec.configure do |config|
13
+ end
@@ -0,0 +1,57 @@
1
+ require 'spec_helper'
2
+
3
+ describe LinkThumbnailer::WebImage do
4
+
5
+ class Foo
6
+ end
7
+
8
+ let(:foo) { Foo.new }
9
+
10
+ before do
11
+ foo.extend LinkThumbnailer::WebImage
12
+ end
13
+
14
+ subject { foo }
15
+
16
+ it { should respond_to :to_hash }
17
+ it { should respond_to :source_url }
18
+ it { should respond_to :doc }
19
+
20
+ describe ".to_hash" do
21
+
22
+ context "with default attributes" do
23
+
24
+ let(:attributes) { [:source_url] }
25
+
26
+ before do
27
+ LinkThumbnailer.config
28
+ end
29
+
30
+ subject { foo.to_hash }
31
+
32
+ it { subject.keys.should eq(attributes.map(&:to_sym)) }
33
+
34
+ end
35
+
36
+ context "with all attributes" do
37
+
38
+ let(:attributes) { LinkThumbnailer.configuration.rmagick_attributes }
39
+
40
+ before do
41
+ attributes.each {|a| foo.class.send(:define_method, a.to_sym) { 'foo' } }
42
+ end
43
+
44
+ after do
45
+ attributes.each {|a| foo.class.send(:undef_method, a.to_sym) }
46
+ end
47
+
48
+ subject { foo.to_hash }
49
+
50
+ it { subject.keys.should eq(attributes.map(&:to_sym)) }
51
+ it { subject.values.should include('foo') }
52
+
53
+ end
54
+
55
+ end
56
+
57
+ end