link_preview 0.2.7

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.
@@ -0,0 +1,50 @@
1
+ # encoding: UTF-8
2
+
3
+ # Copyright (c) 2014, VMware, Inc. All Rights Reserved.
4
+ #
5
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ # of this software and associated documentation files (the "Software"), to deal
7
+ # in the Software without restriction, including without limitation the rights to
8
+ # use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
9
+ # of the Software, and to permit persons to whom the Software is furnished to do
10
+ # so, subject to the following conditions:
11
+ #
12
+ # The above copyright notice and this permission notice shall be included in all
13
+ # copies or substantial portions of the Software.
14
+ #
15
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ # SOFTWARE.
22
+
23
+ require 'spec_helper'
24
+
25
+ describe LinkPreview::HTTPCrawler do
26
+ let(:config) { LinkPreview::Configuration.new }
27
+
28
+ let(:crawler) { LinkPreview::HTTPCrawler.new(config) }
29
+
30
+ describe '#dequeue!' do
31
+ before do
32
+ crawler.enqueue!('http://example.com')
33
+ end
34
+
35
+ context 'when http_client.get raises an exception' do
36
+ before do
37
+ config.http_client.stub(:get).and_raise(Timeout::Error)
38
+ config.error_handler.should_receive(:call).once.and_return { 'something' }
39
+ end
40
+
41
+ subject(:response) { crawler.dequeue! }
42
+
43
+ it 'should receive error_handler call and return non successful response' do
44
+ should be_a(Faraday::Response)
45
+ should_not be_success
46
+ end
47
+ end
48
+ end
49
+ end
50
+
@@ -0,0 +1,99 @@
1
+ # Copyright (c) 2014, VMware, Inc. All Rights Reserved.
2
+ #
3
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ # of this software and associated documentation files (the "Software"), to deal
5
+ # in the Software without restriction, including without limitation the rights to
6
+ # use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
7
+ # of the Software, and to permit persons to whom the Software is furnished to do
8
+ # so, subject to the following conditions:
9
+ #
10
+ # The above copyright notice and this permission notice shall be included in all
11
+ # copies or substantial portions of the Software.
12
+ #
13
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19
+ # SOFTWARE.
20
+
21
+ require 'spec_helper'
22
+
23
+ describe LinkPreview::URI do
24
+ describe '.parse' do
25
+ let(:options) { {width: 420} }
26
+
27
+ subject(:parsed_uri) do
28
+ LinkPreview::URI.parse(uri, options)
29
+ end
30
+
31
+ context 'with nil' do
32
+ let(:uri) { nil }
33
+ it { parsed_uri.should be_nil }
34
+ end
35
+
36
+ context 'with parsed LinkPreview::URI' do
37
+ let(:uri) { LinkPreview::URI.parse('http://socialcast.com') }
38
+
39
+ it { parsed_uri.should be_a(LinkPreview::URI) }
40
+ it { parsed_uri.to_s.should == 'http://socialcast.com/' }
41
+ it { parsed_uri.should_not be_a_kaltura_uri }
42
+ it { parsed_uri.should_not be_a_oembed_uri }
43
+ end
44
+
45
+ context 'with common uri' do
46
+ let(:uri) { 'http://socialcast.com' }
47
+
48
+ it { parsed_uri.should be_a(LinkPreview::URI) }
49
+ it { parsed_uri.to_s.should == 'http://socialcast.com/' }
50
+ it { parsed_uri.should_not be_a_kaltura_uri }
51
+ it { parsed_uri.should_not be_a_oembed_uri }
52
+ end
53
+
54
+ context 'with kaltura uri' do
55
+ let(:uri) { 'http://demo.kaltura.com/mediaspace/media/index.php/action/oembed?url=http%3A%2F%2Fdemo.kaltura.com%2Fmediaspace%2Fmedia%2F%2Fid%2F1_h9tin5on&playerId=3073841&entryId=1_h9tin5on' }
56
+ it { parsed_uri.should be_a(LinkPreview::URI) }
57
+ it { parsed_uri.to_s.should == 'http://demo.kaltura.com/mediaspace/media/index.php/action/oembed/?url=http%3A%2F%2Fdemo.kaltura.com%2Fmediaspace%2Fmedia%2F%2Fid%2F1_h9tin5on&playerId=3073841&entryId=1_h9tin5on&width=420' }
58
+ it { parsed_uri.should be_a_kaltura_uri }
59
+ it { parsed_uri.should be_a_oembed_uri }
60
+ end
61
+
62
+ context 'with kaltura uri with space error' do
63
+ let(:uri) { 'https://cdnsecakmi.kaltura.com/ index.php/kwidget/wid/_1257971/uiconf_id//entry_id/0_aivu6h6k' }
64
+ it { parsed_uri.should be_a(LinkPreview::URI) }
65
+ it { parsed_uri.to_s.should == 'https://cdnsecakmi.kaltura.com/%20index.php/kwidget/wid/_1257971/uiconf_id//entry_id/0_aivu6h6k' }
66
+ it { parsed_uri.should_not be_a_kaltura_uri }
67
+ it { parsed_uri.should_not be_a_oembed_uri }
68
+ end
69
+ end
70
+
71
+ describe '#to_absolute' do
72
+ let(:reference_uri) { 'http://socialcast.com' }
73
+
74
+ subject(:absolute_uri) do
75
+ LinkPreview::URI.parse(uri).to_absolute(reference_uri)
76
+ end
77
+
78
+ context 'with absolute uri ' do
79
+ let(:uri) { 'http://socialcast.com/a/b/c' }
80
+
81
+ it { absolute_uri.should be_a(LinkPreview::URI) }
82
+ it { absolute_uri.to_s.should == 'http://socialcast.com/a/b/c' }
83
+ end
84
+
85
+ context 'with relative uri' do
86
+ let(:uri) { 'a/b/c' }
87
+
88
+ it { absolute_uri.should be_a(LinkPreview::URI) }
89
+ it { absolute_uri.to_s.should == 'http://socialcast.com/a/b/c' }
90
+ end
91
+
92
+ context 'with another relative uri' do
93
+ let(:uri) { 'a/b/../../z' }
94
+
95
+ it { absolute_uri.should be_a(LinkPreview::URI) }
96
+ it { absolute_uri.to_s.should == 'http://socialcast.com/z' }
97
+ end
98
+ end
99
+ end
@@ -0,0 +1,383 @@
1
+ # encoding: UTF-8
2
+
3
+ # Copyright (c) 2014, VMware, Inc. All Rights Reserved.
4
+ #
5
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ # of this software and associated documentation files (the "Software"), to deal
7
+ # in the Software without restriction, including without limitation the rights to
8
+ # use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
9
+ # of the Software, and to permit persons to whom the Software is furnished to do
10
+ # so, subject to the following conditions:
11
+ #
12
+ # The above copyright notice and this permission notice shall be included in all
13
+ # copies or substantial portions of the Software.
14
+ #
15
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ # SOFTWARE.
22
+
23
+ require 'spec_helper'
24
+
25
+ describe LinkPreview do
26
+ it { should be_a(Module) }
27
+
28
+ let(:http_client) { LinkPreview.configuration.http_client }
29
+
30
+ context 'open graph data', :vcr => {:cassette_name => 'ogp.me'} do
31
+ let(:url) { 'http://ogp.me' }
32
+
33
+ subject(:content) do
34
+ LinkPreview.fetch(url)
35
+ end
36
+
37
+ it { should be_a(LinkPreview::Content) }
38
+ its(:url) { should == url }
39
+ its(:title) { should == %Q{Open Graph protocol} }
40
+ its(:description) { should == %Q{The Open Graph protocol enables any web page to become a rich object in a social graph.} }
41
+ its(:site_name) { should == 'ogp.me' }
42
+ its(:site_url) { should == url }
43
+ its(:image_url) { should == 'http://ogp.me/logo.png' }
44
+ its(:image_data) { should be_a(StringIO) }
45
+ its(:image_content_type) { should == 'image/png' }
46
+ its(:image_file_name) { should == 'logo.png' }
47
+
48
+ it 'should issue minimum number of requests' do
49
+ http_client.should_receive(:get).with('http://ogp.me/').ordered.and_call_original
50
+ content.title
51
+ http_client.should_receive(:get).with('http://ogp.me/logo.png').ordered.and_call_original
52
+ content.image_data
53
+ end
54
+
55
+ context '#as_oembed' do
56
+ subject(:oembed) { content.as_oembed }
57
+
58
+ it 'should encode as link' do
59
+ should == {
60
+ :version => '1.0',
61
+ :provider_name => %Q{ogp.me},
62
+ :provider_url => 'http://ogp.me',
63
+ :title => %Q{Open Graph protocol},
64
+ :description => %Q{The Open Graph protocol enables any web page to become a rich object in a social graph.},
65
+ :type => 'link',
66
+ :thumbnail_url => 'http://ogp.me/logo.png'
67
+ }
68
+ end
69
+ end
70
+ end
71
+
72
+ context 'youtube oembed', :vcr => {:cassette_name => 'youtube'} do
73
+ subject(:content) do
74
+ LinkPreview.fetch('http://youtube.com/watch?v=M3r2XDceM6A')
75
+ end
76
+
77
+ it { should be_a(LinkPreview::Content) }
78
+ its(:url) { should == 'http://youtube.com/watch?v=M3r2XDceM6A' }
79
+ its(:title) { should == %Q{Amazing Nintendo Facts} }
80
+ its(:description) { should == %Q{Learn about the history of Nintendo, its gaming systems, and Mario! It's 21 amazing facts about Nintendo you may have never known. Update: As of late 2008, W...} }
81
+ its(:site_name) { should == 'YouTube' }
82
+ its(:site_url) { should == 'http://www.youtube.com/' }
83
+ its(:image_url) { should == 'http://i1.ytimg.com/vi/M3r2XDceM6A/hqdefault.jpg' }
84
+ its(:image_data) { should be_a(StringIO) }
85
+ its(:image_content_type) { should == 'image/jpeg' }
86
+ its(:image_file_name) { should == 'hqdefault.jpg' }
87
+
88
+ it 'should issue minimum number of requests' do
89
+ http_client.should_receive(:get).with('http://www.youtube.com/oembed?format=json&url=http%3A%2F%2Fyoutube.com%2Fwatch%3Fv%3DM3r2XDceM6A').ordered.and_call_original
90
+ content.title
91
+ http_client.should_receive(:get).with('http://youtube.com/watch?v=M3r2XDceM6A').ordered.and_call_original
92
+ content.description
93
+ http_client.should_receive(:get).with('http://i1.ytimg.com/vi/M3r2XDceM6A/hqdefault.jpg').ordered.and_call_original
94
+ content.image_data
95
+ end
96
+
97
+ context '#as_oembed' do
98
+ subject(:oembed) { content.as_oembed }
99
+
100
+ it 'should proxy oembed content' do
101
+ should == {
102
+ :version => '1.0',
103
+ :provider_name => %Q{YouTube},
104
+ :provider_url => 'http://www.youtube.com/',
105
+ :url => "http://youtube.com/watch?v=M3r2XDceM6A",
106
+ :title => %Q{Amazing Nintendo Facts},
107
+ :description => %Q{Learn about the history of Nintendo, its gaming systems, and Mario! It's 21 amazing facts about Nintendo you may have never known. Update: As of late 2008, W...},
108
+ :type => 'video',
109
+ :thumbnail_url => 'http://i1.ytimg.com/vi/M3r2XDceM6A/hqdefault.jpg',
110
+ :thumbnail_width => 480,
111
+ :thumbnail_height => 360,
112
+ :html => %Q{<iframe width="480" height="270" src="http://www.youtube.com/embed/M3r2XDceM6A?feature=oembed" frameborder="0" allowfullscreen></iframe>},
113
+ :width => 480,
114
+ :height => 270,
115
+ :author_name => 'ZackScott',
116
+ :author_url => 'http://www.youtube.com/user/ZackScott',
117
+ }
118
+ end
119
+ end
120
+ end
121
+
122
+ context 'kaltura oembed', :vcr => {:cassette_name => 'kaltura'} do
123
+ subject(:content) do
124
+ LinkPreview.fetch('http://videos.kaltura.com/oembed?url=http%3A%2F%2Fvideos.kaltura.com%2Fmedia%2F%2Fid%2F1_abxlxlll&playerId=3073841&entryId=1_abxlxlll', :width => 420)
125
+ end
126
+
127
+ it { should be_a(LinkPreview::Content) }
128
+ its(:url) { should == 'http://videos.kaltura.com/media//id/1_abxlxlll' }
129
+ its(:title) { should == %Q{KMC Overview | Kaltura KMC Tutorial} }
130
+ its(:description) { should be_nil }
131
+ its(:site_name) { should == 'Kaltura Videos' }
132
+ its(:site_url) { should == 'http://videos.kaltura.com/' }
133
+ its(:image_url) { should == "http://cdnbakmi.kaltura.com/p/811441/sp/81144100/thumbnail/entry_id/1_abxlxlll/version/100012/width//height/" }
134
+ its(:image_data) { should be_a(StringIO) }
135
+ its(:image_content_type) { should == 'image/jpeg' }
136
+ its(:image_file_name) { should == 'height' }
137
+
138
+ it 'should issue minimum number of requests' do
139
+ http_client.should_receive(:get).with('http://videos.kaltura.com/oembed/?url=http%3A%2F%2Fvideos.kaltura.com%2Fmedia%2F%2Fid%2F1_abxlxlll&playerId=3073841&entryId=1_abxlxlll&width=420').ordered.and_call_original
140
+ content.title
141
+ http_client.should_receive(:get).with('http://cdnbakmi.kaltura.com/p/811441/sp/81144100/thumbnail/entry_id/1_abxlxlll/version/100012/width//height/').ordered.and_call_original
142
+ content.image_data
143
+ http_client.should_receive(:get).with('http://videos.kaltura.com/media//id/1_abxlxlll').ordered.and_call_original
144
+ content.description
145
+ end
146
+ end
147
+
148
+ context 'sliderocket oembed discovery', :vcr => {:cassette_name => 'sliderocket'} do
149
+ subject(:content) do
150
+ LinkPreview.fetch('http://portal.sliderocket.com/SlideRocket-Presentations/Hoshyar-Foundation', :width => 420)
151
+ end
152
+
153
+ it { should be_a(LinkPreview::Content) }
154
+ its(:url) { should == 'http://portal.sliderocket.com/SlideRocket-Presentations/Hoshyar-Foundation' }
155
+ its(:title) { should == %Q{Hoshyar-Foundation} }
156
+ its(:description) { should == %Q{Proudly crafted with SlideRocket.} }
157
+ its(:site_name) { should == 'SlideRocket' }
158
+ its(:site_url) { should == 'http://sliderocket.com/' }
159
+ its(:image_url) { should == 'http://cdn.sliderocket.com/thumbnails/4/43/43b475a4-192e-455e-832f-4a40697d8d25.jpg' }
160
+ its(:image_data) { should be_a(StringIO) }
161
+ its(:image_content_type) { should == 'binary/octet-stream' }
162
+ its(:image_file_name) { should == '43b475a4-192e-455e-832f-4a40697d8d25.jpg' }
163
+
164
+ it 'should issue minimum number of requests' do
165
+ http_client.should_receive(:get).with('http://portal.sliderocket.com/SlideRocket-Presentations/Hoshyar-Foundation').ordered.and_call_original
166
+ content.title
167
+ http_client.should_receive(:get).with('http://cdn.sliderocket.com/thumbnails/4/43/43b475a4-192e-455e-832f-4a40697d8d25.jpg').ordered.and_call_original
168
+ content.image_data
169
+ http_client.should_receive(:get).with('http://app.sliderocket.com/app/oEmbed.aspx?url=http%3A%2F%2Fapp.sliderocket.com%2Fapp%2Ffullplayer.aspx%3Fid%3Df614ec65-0f9b-4167-bb2a-b384dad535f3&maxwidth=420').ordered.and_call_original
170
+ content.as_oembed
171
+ end
172
+
173
+ context '#as_oembed' do
174
+ subject(:oembed) { content.as_oembed }
175
+
176
+ it 'should proxy oembed content' do
177
+ should == {
178
+ :version => '1.0',
179
+ :provider_name => %Q{SlideRocket},
180
+ :provider_url => 'http://sliderocket.com/',
181
+ :url => 'http://app.sliderocket.com/app/fullplayer.aspx?id=f614ec65-0f9b-4167-bb2a-b384dad535f3',
182
+ :title => %Q{Hoshyar-Foundation},
183
+ :description => %Q{Proudly crafted with SlideRocket.},
184
+ :thumbnail_url => 'http://cdn.sliderocket.com/thumbnails/4/43/43b475a4-192e-455e-832f-4a40697d8d25.jpg',
185
+ :type => 'rich',
186
+ :html => %Q{<iframe src="http://app.sliderocket.com/app/fullplayer.aspx?id=f614ec65-0f9b-4167-bb2a-b384dad535f3" width="420" height="315" frameborder="0" webkitAllowFullScreen mozallowfullscreen allowFullScreen></iframe>},
187
+ :width => 420,
188
+ :height => 315
189
+ }
190
+ end
191
+ end
192
+ end
193
+
194
+ context 'html data with unescaped html', :vcr => {:cassette_name => 'support.apple.com'} do
195
+ subject(:content) do
196
+ LinkPreview.fetch('http://support.apple.com/kb/HT5642')
197
+ end
198
+
199
+ it { should be_a(LinkPreview::Content) }
200
+ its(:url) { should == 'http://support.apple.com/kb/HT5642' }
201
+ its(:title) { should == %Q{About the security content of iOS 6.1 Software Update} }
202
+ its(:description) { should == %Q{This document describes the security content of iOS 6.1.\nFor the protection of our customers, Apple does not disclose, discuss, or confirm security issues until a full investigation has occurred and any necessary patches or releases are available. To learn more about Apple Product Security, see the Apple Product Security website.\nFor information about the Apple Product Security PGP Key, see How to use the Apple Product Security PGP Key.\nWhere possible, CVE IDs are used to reference the vulnerabilities for further information.\nTo learn about other Security Updates, see Apple Security Updates.} }
203
+ its(:site_name) { should == 'support.apple.com' }
204
+ its(:site_url) { should == 'http://support.apple.com' }
205
+ its(:image_url) { should be_nil }
206
+ its(:image_data) { should be_nil }
207
+ its(:image_content_type) { should be_nil }
208
+ its(:image_file_name) { should be_nil }
209
+
210
+ it 'should issue minimum number of requests' do
211
+ http_client.should_receive(:get).with('http://support.apple.com/kb/HT5642').ordered.and_call_original
212
+ content.title
213
+ content.image_data
214
+ end
215
+
216
+ context '#as_oembed' do
217
+ subject(:oembed) { content.as_oembed }
218
+
219
+ it 'should convert to oembed link' do
220
+ should == {
221
+ :version => '1.0',
222
+ :provider_name => %Q{support.apple.com},
223
+ :provider_url => 'http://support.apple.com',
224
+ :title => %Q{About the security content of iOS 6.1 Software Update},
225
+ :description => %Q{This document describes the security content of iOS 6.1.\nFor the protection of our customers, Apple does not disclose, discuss, or confirm security issues until a full investigation has occurred and any necessary patches or releases are available. To learn more about Apple Product Security, see the Apple Product Security website.\nFor information about the Apple Product Security PGP Key, see How to use the Apple Product Security PGP Key.\nWhere possible, CVE IDs are used to reference the vulnerabilities for further information.\nTo learn about other Security Updates, see Apple Security Updates.},
226
+ :type => 'link'
227
+ }
228
+ end
229
+ end
230
+ end
231
+
232
+ context 'image data', :vcr => {:cassette_name => 'ggp.png'} do
233
+ subject(:content) do
234
+ LinkPreview.fetch('http://www.golden-gate-park.com/wp-content/uploads/2011/02/Golden_Gate_Park_Logo_Header.png')
235
+ end
236
+
237
+ it { should be_a(LinkPreview::Content) }
238
+ its(:url) { should == 'http://www.golden-gate-park.com/wp-content/uploads/2011/02/Golden_Gate_Park_Logo_Header.png' }
239
+ its(:title) { should == 'http://www.golden-gate-park.com/wp-content/uploads/2011/02/Golden_Gate_Park_Logo_Header.png' }
240
+ its(:description) { should be_nil }
241
+ its(:site_name) { should == 'www.golden-gate-park.com' }
242
+ its(:site_url) { should == 'http://www.golden-gate-park.com' }
243
+ its(:image_url) { should == 'http://www.golden-gate-park.com/wp-content/uploads/2011/02/Golden_Gate_Park_Logo_Header.png' }
244
+ its(:image_data) { should be_a(StringIO) }
245
+ its(:image_content_type) { should == 'image/png' }
246
+ its(:image_file_name) { should == 'Golden_Gate_Park_Logo_Header.png' }
247
+
248
+ # FIXME should convert to photo via paperclip
249
+ context '#as_oembed' do
250
+ subject(:oembed) { content.as_oembed }
251
+
252
+ it 'should convert to oembed link' do
253
+ should == {
254
+ :version => '1.0',
255
+ :provider_name => %Q{www.golden-gate-park.com},
256
+ :provider_url => 'http://www.golden-gate-park.com',
257
+ :title => %Q{http://www.golden-gate-park.com/wp-content/uploads/2011/02/Golden_Gate_Park_Logo_Header.png},
258
+ :type => 'link',
259
+ :thumbnail_url => 'http://www.golden-gate-park.com/wp-content/uploads/2011/02/Golden_Gate_Park_Logo_Header.png'
260
+ }
261
+ end
262
+ end
263
+ end
264
+
265
+ context 'youtube oembed 404', :vcr => {:cassette_name => 'youtube 404'} do
266
+ subject(:content) do
267
+ LinkPreview.fetch('http://youtube.com/watch?v=1')
268
+ end
269
+
270
+ it { should_not be_found }
271
+ it 'should issue minimum number of requests' do
272
+ http_client.should_receive(:get).with('http://www.youtube.com/oembed?format=json&url=http%3A%2F%2Fyoutube.com%2Fwatch%3Fv%3D1').ordered.and_call_original
273
+ http_client.should_receive(:get).with('http://youtube.com/watch?v=1').ordered.and_call_original
274
+ content.title
275
+ end
276
+
277
+ context '#as_oembed' do
278
+ subject(:oembed) { content.as_oembed }
279
+ it 'should return basic oembed' do
280
+ should == {
281
+ :version => '1.0',
282
+ :provider_name => 'youtube.com',
283
+ :provider_url => 'http://youtube.com',
284
+ :title => 'YouTube',
285
+ :type => 'link'
286
+ }
287
+ end
288
+ end
289
+ end
290
+
291
+ context 'kaltura opengraph', :vcr => {:cassette_name => 'kaltura_opengraph'} do
292
+ subject(:content) do
293
+ LinkPreview.fetch('https://media.mediaspace.kaltura.com/media/Despicable+Me/0_w2zsofdj/6065172')
294
+ end
295
+
296
+ it { should be_a(LinkPreview::Content) }
297
+ its(:url) { should == 'http://media.mediaspace.kaltura.com/media/Despicable+Me/0_w2zsofdj' }
298
+ its(:title) { should == %Q{Despicable Me} }
299
+ its(:description) { should == %Q{In a happy suburban neighborhood surrounded by white picket fences with flowering rose bushes, sits a black house with a dead lawn. Unbeknownst to the neighbors, hidden beneath this home is a vast secret hideout. Surrounded by a small army of minions, we discover Gru planning the biggest heist in the history of the world. He is going to steal the moon, yes, the moon. Gru delights in all things wicked. Armed with his arsenal of shrink rays, freeze rays, and battle-ready vehicles for land and air, he vanquishes all who stand in his way. Until the day he encounters the immense will of three little orphaned girls who look at him and see something that no one else has ever seen: a potential Dad. The world's greatest villain has just met his greatest challenge: three little girls named Margo, Edith and Agnes.} }
300
+ its(:site_name) { should == 'MediaSpace Demo Site' }
301
+ its(:site_url) { should == 'http://media.mediaspace.kaltura.com' }
302
+ its(:image_url) { should == 'https://cdnbakmi.kaltura.com/p/1059491/sp/105949100/thumbnail/entry_id/0_w2zsofdj/version/100021/width/400' }
303
+ its(:image_data) { should be_a(StringIO) }
304
+ its(:image_content_type) { should == 'image/jpeg' }
305
+ its(:image_file_name) { should == '400' }
306
+
307
+ it 'should issue minimum number of requests' do
308
+ http_client.should_receive(:get).with('https://media.mediaspace.kaltura.com/media/Despicable+Me/0_w2zsofdj/6065172').ordered.and_call_original
309
+ content.title
310
+ http_client.should_receive(:get).with('https://cdnbakmi.kaltura.com/p/1059491/sp/105949100/thumbnail/entry_id/0_w2zsofdj/version/100021/width/400').ordered.and_call_original
311
+ http_client.should_receive(:get).with('http://cdnbakmi.kaltura.com/p/1059491/sp/105949100/thumbnail/entry_id/0_w2zsofdj/version/100021/width/400').ordered.and_call_original
312
+ content.image_data
313
+ content.description
314
+ end
315
+
316
+ context '#as_oembed' do
317
+ subject(:oembed) { content.as_oembed }
318
+
319
+ it 'should convert opengraph to oembed' do
320
+ should == {
321
+ :version => '1.0',
322
+ :provider_name => %Q{MediaSpace Demo Site},
323
+ :provider_url => 'http://media.mediaspace.kaltura.com',
324
+ :title => %Q{Despicable Me},
325
+ :description => %Q{In a happy suburban neighborhood surrounded by white picket fences with flowering rose bushes, sits a black house with a dead lawn. Unbeknownst to the neighbors, hidden beneath this home is a vast secret hideout. Surrounded by a small army of minions, we discover Gru planning the biggest heist in the history of the world. He is going to steal the moon, yes, the moon. Gru delights in all things wicked. Armed with his arsenal of shrink rays, freeze rays, and battle-ready vehicles for land and air, he vanquishes all who stand in his way. Until the day he encounters the immense will of three little orphaned girls who look at him and see something that no one else has ever seen: a potential Dad. The world's greatest villain has just met his greatest challenge: three little girls named Margo, Edith and Agnes.},
326
+ :type => 'video',
327
+ :thumbnail_url => "http://cdnbakmi.kaltura.com/p/1059491/sp/105949100/thumbnail/entry_id/0_w2zsofdj/version/100021/width/400",
328
+ :html => %Q{<object width=\"400\" height=\"333\"><param name=\"movie\" value=\"https://www.kaltura.com/index.php/kwidget/wid/_1059491/uiconf_id/16199142/entry_id/0_w2zsofdj\"></param><param name=\"allowScriptAccess\" value=\"always\"></param><param name=\"allowFullScreen\" value=\"true\"></param><embed src=\"https://www.kaltura.com/index.php/kwidget/wid/_1059491/uiconf_id/16199142/entry_id/0_w2zsofdj\" type=\"application/x-shockwave-flash\" allowscriptaccess=\"always\" allowfullscreen=\"true\" width=\"400\" height=\"333\"></embed></object>},
329
+ :width => 400,
330
+ :height => 333
331
+ }
332
+ end
333
+ end
334
+ end
335
+
336
+ context 'elasticsearch', :vcr => {:cassette_name => 'elasticsearch'} do
337
+ let(:url) { 'http://www.elasticsearch.org/overview/hadoop' }
338
+
339
+ subject(:content) do
340
+ LinkPreview.fetch(url)
341
+ end
342
+
343
+ its(:url) { should == url }
344
+ its(:title) { should == %Q{Hadoop | Elasticsearch} }
345
+ its(:description) { should == %Q{Search your Hadoop Data and Get Real-Time Results Deep API integration makes searching data in Hadoop easy Elasticsearch for Apache Hadoop enables real-time searching against data stored in Apache Hadoop. It provides native integration with Map/Reduce, Hive, Pig, and Cascading, all with no customization. Download Elasticsearch for Apache Hadoop Documentation Great fit for “Big Data” [...]} }
346
+ its(:site_name) { should == 'Elasticsearch.org' }
347
+ its(:site_url) { should_not be_nil }
348
+ its(:image_url) { should == 'http://www.elasticsearch.org/content/uploads/2013/10/blank_hero.png' }
349
+ its(:image_data) { should be_a(StringIO) }
350
+ its(:image_content_type) { should == 'image/png' }
351
+ its(:image_file_name) { should == 'blank_hero.png' }
352
+
353
+ context '#as_oembed' do
354
+ subject(:oembed) { content.as_oembed }
355
+
356
+ it 'should encode as link' do
357
+ should == {
358
+ :version => '1.0',
359
+ :provider_name => %Q{Elasticsearch.org},
360
+ :provider_url => 'http://www.elasticsearch.org',
361
+ :title => %Q{Hadoop | Elasticsearch},
362
+ :description => %Q{Search your Hadoop Data and Get Real-Time Results Deep API integration makes searching data in Hadoop easy Elasticsearch for Apache Hadoop enables real-time searching against data stored in Apache Hadoop. It provides native integration with Map/Reduce, Hive, Pig, and Cascading, all with no customization. Download Elasticsearch for Apache Hadoop Documentation Great fit for “Big Data” [...]},
363
+ :type => 'link',
364
+ :thumbnail_url => 'http://www.elasticsearch.org/content/uploads/2013/10/blank_hero.png'
365
+ }
366
+ end
367
+ end
368
+ end
369
+
370
+ context 'resource with bad utf-8 in response', :vcr => {:cassette_name => 'bad_utf8'} do
371
+ let(:url) { 'http://s.taobao.com' }
372
+ subject(:content) do
373
+ LinkPreview.fetch(url)
374
+ end
375
+
376
+ its(:url) { should == url }
377
+ its(:title) { should == url }
378
+ its(:description) { should be_nil }
379
+ its(:site_name) { should == 's.taobao.com' }
380
+ its(:site_url) { should == url }
381
+ its(:image_url) { should be_nil }
382
+ end
383
+ end
@@ -0,0 +1,39 @@
1
+ # Copyright (c) 2014, VMware, Inc. All Rights Reserved.
2
+ #
3
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ # of this software and associated documentation files (the "Software"), to deal
5
+ # in the Software without restriction, including without limitation the rights to
6
+ # use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
7
+ # of the Software, and to permit persons to whom the Software is furnished to do
8
+ # so, subject to the following conditions:
9
+ #
10
+ # The above copyright notice and this permission notice shall be included in all
11
+ # copies or substantial portions of the Software.
12
+ #
13
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19
+ # SOFTWARE.
20
+
21
+ require 'bundler/setup'
22
+ Bundler.require(:default, :development, :test)
23
+
24
+ VCR.configure do |config|
25
+ config.cassette_library_dir = 'spec/files/requests'
26
+ config.hook_into :webmock
27
+ config.configure_rspec_metadata!
28
+ config.preserve_exact_body_bytes do |http_message|
29
+ true
30
+ end
31
+ end
32
+
33
+ RSpec.configure do |config|
34
+ config.mock_with :rspec
35
+
36
+ # so we can use :vcr rather than :vcr => true;
37
+ # in RSpec 3 this will no longer be necessary.
38
+ config.treat_symbols_as_metadata_keys_with_true_values = true
39
+ end
@@ -0,0 +1,26 @@
1
+ module LinkPreviewStubs
2
+ DEFAULT_PROPERTIES = Hash[LinkPreview::Content::PROPERTIES.map { |property| [property, nil] }]
3
+
4
+ def stub_content_not_found(uri = an_instance_of(String))
5
+ content = double('content', DEFAULT_PROPERTIES.merge(:found? => false, :source => {}))
6
+ stub_content_helper uri, content
7
+ end
8
+
9
+ def stub_content(uri = an_instance_of(String), properties = {})
10
+ nested_properties = Hash.new { |h, k| h[k] = {} }
11
+ nested_properties.merge!(properties.select { |k, v| v.is_a?(Hash) })
12
+ simple_properties = properties.reject { |k, v| v.is_a?(Hash) }
13
+
14
+ content = double('content', DEFAULT_PROPERTIES.merge(:found? => true).merge(simple_properties))
15
+ content.stub(:sources).and_return(nested_properties)
16
+ content.stub(:as_oembed).and_return({ :version => '1.0', :type => 'link' }.merge(nested_properties[:oembed]))
17
+
18
+ stub_content_helper uri, content
19
+ end
20
+
21
+ private
22
+
23
+ def stub_content_helper(uri, content)
24
+ LinkPreview.stub(:fetch).with(uri, an_instance_of(Hash), an_instance_of(Hash)).and_return(content)
25
+ end
26
+ end