ruby-oembed 0.14.1 → 0.15.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (37) hide show
  1. checksums.yaml +4 -4
  2. data/.travis.yml +3 -0
  3. data/CHANGELOG.rdoc +13 -1
  4. data/lib/oembed.rb +1 -0
  5. data/lib/oembed/provider.rb +54 -5
  6. data/lib/oembed/providers.rb +39 -300
  7. data/lib/oembed/providers/aggregators/embedly_urls.yml +167 -6
  8. data/lib/oembed/providers/builtin_providers.rb +292 -0
  9. data/lib/oembed/providers/facebook_post.rb +27 -17
  10. data/lib/oembed/providers/facebook_video.rb +22 -10
  11. data/lib/oembed/providers/instagram.rb +37 -20
  12. data/lib/oembed/providers/tik_tok.rb +11 -0
  13. data/lib/oembed/version.rb +2 -2
  14. data/spec/cassettes/OEmbed_Provider.yml +117 -16
  15. data/spec/cassettes/OEmbed_Providers_CodePen.yml +177 -0
  16. data/spec/cassettes/OEmbed_Providers_FacebookPost.yml +539 -0
  17. data/spec/cassettes/OEmbed_Providers_FacebookVideo.yml +267 -0
  18. data/spec/cassettes/OEmbed_Providers_Instagram.yml +1473 -0
  19. data/spec/cassettes/OEmbed_Providers_Slideshare.yml +420 -834
  20. data/spec/cassettes/OEmbed_Providers_TikTok.yml +293 -0
  21. data/spec/cassettes/OEmbed_Providers_Twitter.yml +84 -357
  22. data/spec/cassettes/OEmbed_Providers_Youtube.yml +188 -26
  23. data/spec/provider_spec.rb +315 -138
  24. data/spec/providers/code_pen_spec.rb +21 -0
  25. data/spec/providers/facebook_post_spec.rb +54 -0
  26. data/spec/providers/facebook_video_spec.rb +48 -0
  27. data/spec/providers/instagram_spec.rb +48 -0
  28. data/spec/providers/slideshare_spec.rb +2 -9
  29. data/spec/providers/tik_tok_spec.rb +26 -0
  30. data/spec/providers/twitter_spec.rb +3 -10
  31. data/spec/providers/youtube_spec.rb +3 -9
  32. data/spec/providers_spec.rb +151 -16
  33. data/spec/response_spec.rb +2 -2
  34. data/spec/spec_helper.rb +19 -1
  35. data/spec/support/shared_examples_for_providers.rb +32 -20
  36. metadata +25 -5
  37. data/spec/providers/facebook_spec.rb +0 -50
@@ -12,7 +12,7 @@ http_interactions:
12
12
  Accept:
13
13
  - "*/*"
14
14
  User-Agent:
15
- - Mozilla/5.0 (compatible; ruby-oembed/0.14.0)
15
+ - Mozilla/5.0 (compatible; ruby-oembed/0.14.1)
16
16
  response:
17
17
  status:
18
18
  code: 200
@@ -20,19 +20,73 @@ http_interactions:
20
20
  headers:
21
21
  Strict-Transport-Security:
22
22
  - max-age=31536000
23
+ P3p:
24
+ - CP="This is not a P3P policy! See http://support.google.com/accounts/answer/151657?hl=en
25
+ for more info."
26
+ Content-Type:
27
+ - application/json
23
28
  Cache-Control:
24
29
  - no-cache
30
+ Expires:
31
+ - Tue, 27 Apr 1971 19:44:06 GMT
32
+ X-Content-Type-Options:
33
+ - nosniff
34
+ Date:
35
+ - Fri, 01 Jan 2021 20:36:36 GMT
36
+ Server:
37
+ - YouTube Frontend Proxy
38
+ X-Xss-Protection:
39
+ - '0'
40
+ Alt-Svc:
41
+ - h3-29=":443"; ma=2592000,h3-T051=":443"; ma=2592000,h3-Q050=":443"; ma=2592000,h3-Q046=":443";
42
+ ma=2592000,h3-Q043=":443"; ma=2592000,quic=":443"; ma=2592000; v="46,43"
43
+ Accept-Ranges:
44
+ - none
45
+ Vary:
46
+ - Accept-Encoding
47
+ Transfer-Encoding:
48
+ - chunked
49
+ body:
50
+ encoding: UTF-8
51
+ string: '{"title":"CKEditor 4.5 - Embedding Media Resources with oEmbed","author_name":"CKEditor
52
+ Ecosystem","author_url":"https://www.youtube.com/c/CKEditor","type":"video","height":270,"width":480,"version":"1.0","provider_name":"YouTube","provider_url":"https://www.youtube.com/","thumbnail_height":360,"thumbnail_width":480,"thumbnail_url":"https://i.ytimg.com/vi/pO5L6vXtxsI/hqdefault.jpg","html":"\u003ciframe
53
+ width=\u0022480\u0022 height=\u0022270\u0022 src=\u0022https://www.youtube.com/embed/pO5L6vXtxsI?feature=oembed\u0022
54
+ frameborder=\u00220\u0022 allow=\u0022accelerometer; autoplay; clipboard-write;
55
+ encrypted-media; gyroscope; picture-in-picture\u0022 allowfullscreen\u003e\u003c/iframe\u003e"}'
56
+ recorded_at: Fri, 01 Jan 2021 20:36:36 GMT
57
+ - request:
58
+ method: get
59
+ uri: https://www.youtube.com/oembed/?format=json&scheme=https&url=http://www.youtube.com/watch?v=pO5L6vXtxsI
60
+ body:
61
+ encoding: US-ASCII
62
+ string: ''
63
+ headers:
64
+ Accept-Encoding:
65
+ - gzip;q=1.0,deflate;q=0.6,identity;q=0.3
66
+ Accept:
67
+ - "*/*"
68
+ User-Agent:
69
+ - Mozilla/5.0 (compatible; ruby-oembed/0.14.1)
70
+ response:
71
+ status:
72
+ code: 200
73
+ message: OK
74
+ headers:
25
75
  Content-Type:
26
76
  - application/json
77
+ Cache-Control:
78
+ - no-cache
79
+ Expires:
80
+ - Tue, 27 Apr 1971 19:44:06 GMT
27
81
  P3p:
28
82
  - CP="This is not a P3P policy! See http://support.google.com/accounts/answer/151657?hl=en
29
83
  for more info."
30
- Expires:
31
- - Tue, 27 Apr 1971 19:44:06 GMT
32
84
  X-Content-Type-Options:
33
85
  - nosniff
86
+ Strict-Transport-Security:
87
+ - max-age=31536000
34
88
  Date:
35
- - Mon, 28 Dec 2020 15:32:04 GMT
89
+ - Fri, 01 Jan 2021 20:36:36 GMT
36
90
  Server:
37
91
  - YouTube Frontend Proxy
38
92
  X-Xss-Protection:
@@ -53,10 +107,10 @@ http_interactions:
53
107
  width=\u0022480\u0022 height=\u0022270\u0022 src=\u0022https://www.youtube.com/embed/pO5L6vXtxsI?feature=oembed\u0022
54
108
  frameborder=\u00220\u0022 allow=\u0022accelerometer; autoplay; clipboard-write;
55
109
  encrypted-media; gyroscope; picture-in-picture\u0022 allowfullscreen\u003e\u003c/iframe\u003e"}'
56
- recorded_at: Mon, 28 Dec 2020 15:32:04 GMT
110
+ recorded_at: Fri, 01 Jan 2021 20:36:36 GMT
57
111
  - request:
58
112
  method: get
59
- uri: https://www.youtube.com/oembed/?format=json&scheme=https&url=http://www.youtube.com/watch?v=pO5L6vXtxsI
113
+ uri: https://www.youtube.com/oembed/?format=json&scheme=https&url=https://youtu.be/pO5L6vXtxsI
60
114
  body:
61
115
  encoding: US-ASCII
62
116
  string: ''
@@ -66,27 +120,135 @@ http_interactions:
66
120
  Accept:
67
121
  - "*/*"
68
122
  User-Agent:
69
- - Mozilla/5.0 (compatible; ruby-oembed/0.14.0)
123
+ - Mozilla/5.0 (compatible; ruby-oembed/0.14.1)
70
124
  response:
71
125
  status:
72
126
  code: 200
73
127
  message: OK
74
128
  headers:
129
+ Strict-Transport-Security:
130
+ - max-age=31536000
75
131
  P3p:
76
132
  - CP="This is not a P3P policy! See http://support.google.com/accounts/answer/151657?hl=en
77
133
  for more info."
78
134
  Cache-Control:
79
135
  - no-cache
136
+ Expires:
137
+ - Tue, 27 Apr 1971 19:44:06 GMT
138
+ Content-Type:
139
+ - application/json
140
+ X-Content-Type-Options:
141
+ - nosniff
142
+ Date:
143
+ - Fri, 01 Jan 2021 20:36:36 GMT
144
+ Server:
145
+ - YouTube Frontend Proxy
146
+ X-Xss-Protection:
147
+ - '0'
148
+ Alt-Svc:
149
+ - h3-29=":443"; ma=2592000,h3-T051=":443"; ma=2592000,h3-Q050=":443"; ma=2592000,h3-Q046=":443";
150
+ ma=2592000,h3-Q043=":443"; ma=2592000,quic=":443"; ma=2592000; v="46,43"
151
+ Accept-Ranges:
152
+ - none
153
+ Vary:
154
+ - Accept-Encoding
155
+ Transfer-Encoding:
156
+ - chunked
157
+ body:
158
+ encoding: UTF-8
159
+ string: '{"title":"CKEditor 4.5 - Embedding Media Resources with oEmbed","author_name":"CKEditor
160
+ Ecosystem","author_url":"https://www.youtube.com/c/CKEditor","type":"video","height":270,"width":480,"version":"1.0","provider_name":"YouTube","provider_url":"https://www.youtube.com/","thumbnail_height":360,"thumbnail_width":480,"thumbnail_url":"https://i.ytimg.com/vi/pO5L6vXtxsI/hqdefault.jpg","html":"\u003ciframe
161
+ width=\u0022480\u0022 height=\u0022270\u0022 src=\u0022https://www.youtube.com/embed/pO5L6vXtxsI?feature=oembed\u0022
162
+ frameborder=\u00220\u0022 allow=\u0022accelerometer; autoplay; clipboard-write;
163
+ encrypted-media; gyroscope; picture-in-picture\u0022 allowfullscreen\u003e\u003c/iframe\u003e"}'
164
+ recorded_at: Fri, 01 Jan 2021 20:36:36 GMT
165
+ - request:
166
+ method: get
167
+ uri: https://www.youtube.com/oembed/?format=json&scheme=https&url=https://www.youtube.com/watch?v=pO5L6vXtxsI
168
+ body:
169
+ encoding: US-ASCII
170
+ string: ''
171
+ headers:
172
+ Accept-Encoding:
173
+ - gzip;q=1.0,deflate;q=0.6,identity;q=0.3
174
+ Accept:
175
+ - "*/*"
176
+ User-Agent:
177
+ - Mozilla/5.0 (compatible; ruby-oembed/0.14.1)
178
+ response:
179
+ status:
180
+ code: 200
181
+ message: OK
182
+ headers:
183
+ P3p:
184
+ - CP="This is not a P3P policy! See http://support.google.com/accounts/answer/151657?hl=en
185
+ for more info."
186
+ Expires:
187
+ - Tue, 27 Apr 1971 19:44:06 GMT
80
188
  Content-Type:
81
189
  - application/json
82
190
  Strict-Transport-Security:
83
191
  - max-age=31536000
192
+ Cache-Control:
193
+ - no-cache
194
+ X-Content-Type-Options:
195
+ - nosniff
196
+ Date:
197
+ - Fri, 01 Jan 2021 20:36:36 GMT
198
+ Server:
199
+ - YouTube Frontend Proxy
200
+ X-Xss-Protection:
201
+ - '0'
202
+ Alt-Svc:
203
+ - h3-29=":443"; ma=2592000,h3-T051=":443"; ma=2592000,h3-Q050=":443"; ma=2592000,h3-Q046=":443";
204
+ ma=2592000,h3-Q043=":443"; ma=2592000,quic=":443"; ma=2592000; v="46,43"
205
+ Accept-Ranges:
206
+ - none
207
+ Vary:
208
+ - Accept-Encoding
209
+ Transfer-Encoding:
210
+ - chunked
211
+ body:
212
+ encoding: UTF-8
213
+ string: '{"title":"CKEditor 4.5 - Embedding Media Resources with oEmbed","author_name":"CKEditor
214
+ Ecosystem","author_url":"https://www.youtube.com/c/CKEditor","type":"video","height":270,"width":480,"version":"1.0","provider_name":"YouTube","provider_url":"https://www.youtube.com/","thumbnail_height":360,"thumbnail_width":480,"thumbnail_url":"https://i.ytimg.com/vi/pO5L6vXtxsI/hqdefault.jpg","html":"\u003ciframe
215
+ width=\u0022480\u0022 height=\u0022270\u0022 src=\u0022https://www.youtube.com/embed/pO5L6vXtxsI?feature=oembed\u0022
216
+ frameborder=\u00220\u0022 allow=\u0022accelerometer; autoplay; clipboard-write;
217
+ encrypted-media; gyroscope; picture-in-picture\u0022 allowfullscreen\u003e\u003c/iframe\u003e"}'
218
+ recorded_at: Fri, 01 Jan 2021 20:36:36 GMT
219
+ - request:
220
+ method: get
221
+ uri: https://www.youtube.com/oembed/?format=json&scheme=https&url=http://www.youtube.com/watch?v=pO5L6vXtxsI
222
+ body:
223
+ encoding: US-ASCII
224
+ string: ''
225
+ headers:
226
+ Accept-Encoding:
227
+ - gzip;q=1.0,deflate;q=0.6,identity;q=0.3
228
+ Accept:
229
+ - "*/*"
230
+ User-Agent:
231
+ - Mozilla/5.0 (compatible; ruby-oembed/0.14.1)
232
+ response:
233
+ status:
234
+ code: 200
235
+ message: OK
236
+ headers:
237
+ Content-Type:
238
+ - application/json
84
239
  Expires:
85
240
  - Tue, 27 Apr 1971 19:44:06 GMT
241
+ P3p:
242
+ - CP="This is not a P3P policy! See http://support.google.com/accounts/answer/151657?hl=en
243
+ for more info."
244
+ Strict-Transport-Security:
245
+ - max-age=31536000
86
246
  X-Content-Type-Options:
87
247
  - nosniff
248
+ Cache-Control:
249
+ - no-cache
88
250
  Date:
89
- - Mon, 28 Dec 2020 15:32:04 GMT
251
+ - Fri, 01 Jan 2021 20:36:36 GMT
90
252
  Server:
91
253
  - YouTube Frontend Proxy
92
254
  X-Xss-Protection:
@@ -107,7 +269,7 @@ http_interactions:
107
269
  width=\u0022480\u0022 height=\u0022270\u0022 src=\u0022https://www.youtube.com/embed/pO5L6vXtxsI?feature=oembed\u0022
108
270
  frameborder=\u00220\u0022 allow=\u0022accelerometer; autoplay; clipboard-write;
109
271
  encrypted-media; gyroscope; picture-in-picture\u0022 allowfullscreen\u003e\u003c/iframe\u003e"}'
110
- recorded_at: Mon, 28 Dec 2020 15:32:04 GMT
272
+ recorded_at: Fri, 01 Jan 2021 20:36:36 GMT
111
273
  - request:
112
274
  method: get
113
275
  uri: https://www.youtube.com/oembed/?format=json&scheme=https&url=https://youtu.be/pO5L6vXtxsI
@@ -120,7 +282,7 @@ http_interactions:
120
282
  Accept:
121
283
  - "*/*"
122
284
  User-Agent:
123
- - Mozilla/5.0 (compatible; ruby-oembed/0.14.0)
285
+ - Mozilla/5.0 (compatible; ruby-oembed/0.14.1)
124
286
  response:
125
287
  status:
126
288
  code: 200
@@ -131,16 +293,16 @@ http_interactions:
131
293
  P3p:
132
294
  - CP="This is not a P3P policy! See http://support.google.com/accounts/answer/151657?hl=en
133
295
  for more info."
134
- Content-Type:
135
- - application/json
136
296
  Cache-Control:
137
297
  - no-cache
138
- X-Content-Type-Options:
139
- - nosniff
140
298
  Strict-Transport-Security:
141
299
  - max-age=31536000
300
+ X-Content-Type-Options:
301
+ - nosniff
302
+ Content-Type:
303
+ - application/json
142
304
  Date:
143
- - Mon, 28 Dec 2020 15:32:04 GMT
305
+ - Fri, 01 Jan 2021 20:36:36 GMT
144
306
  Server:
145
307
  - YouTube Frontend Proxy
146
308
  X-Xss-Protection:
@@ -161,7 +323,7 @@ http_interactions:
161
323
  width=\u0022480\u0022 height=\u0022270\u0022 src=\u0022https://www.youtube.com/embed/pO5L6vXtxsI?feature=oembed\u0022
162
324
  frameborder=\u00220\u0022 allow=\u0022accelerometer; autoplay; clipboard-write;
163
325
  encrypted-media; gyroscope; picture-in-picture\u0022 allowfullscreen\u003e\u003c/iframe\u003e"}'
164
- recorded_at: Mon, 28 Dec 2020 15:32:04 GMT
326
+ recorded_at: Fri, 01 Jan 2021 20:36:36 GMT
165
327
  - request:
166
328
  method: get
167
329
  uri: https://www.youtube.com/oembed/?format=json&scheme=https&url=https://youtu.be/NHriYTkvd0g
@@ -174,29 +336,29 @@ http_interactions:
174
336
  Accept:
175
337
  - "*/*"
176
338
  User-Agent:
177
- - Mozilla/5.0 (compatible; ruby-oembed/0.14.0)
339
+ - Mozilla/5.0 (compatible; ruby-oembed/0.14.1)
178
340
  response:
179
341
  status:
180
342
  code: 403
181
343
  message: Forbidden
182
344
  headers:
345
+ P3p:
346
+ - CP="This is not a P3P policy! See http://support.google.com/accounts/answer/151657?hl=en
347
+ for more info."
348
+ X-Content-Type-Options:
349
+ - nosniff
183
350
  Strict-Transport-Security:
184
351
  - max-age=31536000
185
352
  Content-Length:
186
353
  - '9'
187
- X-Content-Type-Options:
188
- - nosniff
189
- Content-Type:
190
- - text/html; charset=utf-8
191
354
  Cache-Control:
192
355
  - no-cache
193
- P3p:
194
- - CP="This is not a P3P policy! See http://support.google.com/accounts/answer/151657?hl=en
195
- for more info."
356
+ Content-Type:
357
+ - text/html; charset=utf-8
196
358
  Expires:
197
359
  - Tue, 27 Apr 1971 19:44:06 GMT
198
360
  Date:
199
- - Mon, 28 Dec 2020 15:32:04 GMT
361
+ - Fri, 01 Jan 2021 20:36:37 GMT
200
362
  Server:
201
363
  - YouTube Frontend Proxy
202
364
  X-Xss-Protection:
@@ -207,5 +369,5 @@ http_interactions:
207
369
  body:
208
370
  encoding: UTF-8
209
371
  string: Forbidden
210
- recorded_at: Mon, 28 Dec 2020 15:32:04 GMT
372
+ recorded_at: Fri, 01 Jan 2021 20:36:37 GMT
211
373
  recorded_with: VCR 6.0.0
@@ -13,8 +13,8 @@ describe OEmbed::Provider do
13
13
  before(:all) do
14
14
  @default = OEmbed::Formatter.default
15
15
  @flickr = OEmbed::Provider.new("http://www.flickr.com/services/oembed/")
16
- @qik = OEmbed::Provider.new("http://qik.com/api/oembed.{format}", :xml)
17
- @viddler = OEmbed::Provider.new("http://lab.viddler.com/services/oembed/", :json)
16
+ @qik = OEmbed::Provider.new("http://qik.com/api/oembed.{format}", format: :xml)
17
+ @viddler = OEmbed::Provider.new("http://lab.viddler.com/services/oembed/", format: :json)
18
18
 
19
19
  @flickr << "http://*.flickr.com/*"
20
20
  @qik << "http://qik.com/video/*"
@@ -22,187 +22,297 @@ describe OEmbed::Provider do
22
22
  @viddler << "http://*.viddler.com/*"
23
23
  end
24
24
 
25
- it "should require a valid endpoint for a new instance" do
26
- expect { OEmbed::Provider.new("http://foo.com/oembed/") }.
27
- not_to raise_error
25
+ describe "initialize" do
26
+ it "should by default use OEmbed::Formatter.default" do
27
+ provider = OEmbed::Provider.new("http://foo.com/oembed/")
28
+ expect(provider.format).to eq(OEmbed::Formatter.default)
29
+ end
28
30
 
29
- expect { OEmbed::Provider.new("https://foo.com/oembed/") }.
30
- not_to raise_error
31
- end
31
+ [:xml, :json].each do |given_format|
32
+ it "should allow #{given_format} format via positional argument" do
33
+ provider = OEmbed::Provider.new("http://foo.com/oembed/", given_format)
34
+ expect( provider.format ).to eq(given_format)
35
+ end
32
36
 
33
- it "should allow a {format} string in the endpoint for a new instance" do
34
- expect { OEmbed::Provider.new("http://foo.com/oembed.{format}/get") }.
35
- not_to raise_error
36
- end
37
+ it "should allow #{given_format} format via named argument" do
38
+ provider = OEmbed::Provider.new("http://foo.com/oembed/", format: given_format)
39
+ expect( provider.format ).to eq(given_format)
40
+ end
41
+ end
37
42
 
38
- it "should raise an ArgumentError given an invalid endpoint for a new instance" do
39
- [
40
- "httpx://foo.com/oembed/",
41
- "ftp://foo.com/oembed/",
42
- "foo.com/oembed/",
43
- "http://not a uri",
44
- nil, 1,
45
- ].each do |endpoint|
46
- expect { OEmbed::Provider.new(endpoint) }.
47
- to raise_error(ArgumentError)
43
+ it "should require a valid endpoint for a new instance" do
44
+ expect { OEmbed::Provider.new("http://foo.com/oembed/") }.
45
+ not_to raise_error
46
+
47
+ expect { OEmbed::Provider.new("https://foo.com/oembed/") }.
48
+ not_to raise_error
48
49
  end
49
- end
50
50
 
51
- it "should allow no URI schema to be given" do
52
- provider = OEmbed::Provider.new("http://foo.com/oembed")
51
+ it "should allow a {format} string in the endpoint for a new instance" do
52
+ expect { OEmbed::Provider.new("http://foo.com/oembed.{format}/get") }.
53
+ not_to raise_error
54
+ end
53
55
 
54
- expect(provider).to include("http://foo.com/1")
55
- expect(provider).to include("http://bar.foo.com/1")
56
- expect(provider).to include("http://bar.foo.com/show/1")
57
- expect(provider).to include("https://bar.foo.com/1")
58
- expect(provider).to include("http://asdf.com/1")
59
- expect(provider).to include("asdf")
60
- end
56
+ it "should raise an ArgumentError given an invalid endpoint for a new instance" do
57
+ [
58
+ "httpx://foo.com/oembed/",
59
+ "ftp://foo.com/oembed/",
60
+ "foo.com/oembed/",
61
+ "http://not a uri",
62
+ nil, 1,
63
+ ].each do |endpoint|
64
+ expect { OEmbed::Provider.new(endpoint) }.
65
+ to raise_error(ArgumentError)
66
+ end
67
+ end
61
68
 
62
- it "should allow a String as a URI schema" do
63
- provider = OEmbed::Provider.new("http://foo.com/oembed")
64
- provider << "http://bar.foo.com/*"
69
+ it "should allow random formats on initialization" do
70
+ expect {
71
+ yaml_provider = OEmbed::Provider.new("http://foo.com/api/oembed.{format}", format: :yml)
72
+ yaml_provider << "http://foo.com/*"
73
+ }.
74
+ not_to raise_error
75
+ end
65
76
 
66
- expect(provider).to include("http://bar.foo.com/1")
67
- expect(provider).to include("http://bar.foo.com/show/1")
77
+ it "should not allow random formats to be parsed" do
78
+ yaml_provider = OEmbed::Provider.new("http://foo.com/api/oembed.{format}", format: :yml)
79
+ yaml_provider << "http://foo.com/*"
80
+ yaml_url = "http://foo.com/video/1"
68
81
 
69
- expect(provider).to_not include("https://bar.foo.com/1")
70
- expect(provider).to_not include("http://foo.com/1")
71
- end
82
+ expect(yaml_provider).to receive(:raw).
83
+ with(yaml_url, {:format=>:yml}).
84
+ and_return(valid_response(:json))
72
85
 
73
- it "should allow multiple path wildcards in a String URI schema" do
74
- provider = OEmbed::Provider.new("http://foo.com/oembed")
75
- provider << "http://bar.foo.com/*/show/*"
86
+ expect { yaml_provider.get(yaml_url) }.
87
+ to raise_error(OEmbed::FormatNotSupported)
88
+ end
76
89
 
77
- expect(provider).to include("http://bar.foo.com/photo/show/1")
78
- expect(provider).to include("http://bar.foo.com/video/show/2")
79
- expect(provider).to include("http://bar.foo.com/help/video/show/2")
90
+ it "should allow no URI schema to be given" do
91
+ provider = OEmbed::Provider.new("http://foo.com/oembed")
80
92
 
81
- expect(provider).to_not include("https://bar.foo.com/photo/show/1")
82
- expect(provider).to_not include("http://foo.com/video/show/2")
83
- expect(provider).to_not include("http://bar.foo.com/show/1")
84
- expect(provider).to_not include("http://bar.foo.com/1")
85
- end
93
+ expect(provider).to include("http://foo.com/1")
94
+ expect(provider).to include("http://bar.foo.com/1")
95
+ expect(provider).to include("http://bar.foo.com/show/1")
96
+ expect(provider).to include("https://bar.foo.com/1")
97
+ expect(provider).to include("http://asdf.com/1")
98
+ expect(provider).to include("asdf")
99
+ end
86
100
 
87
- it "should NOT allow multiple domain wildcards in a String URI schema", :pending => true do
88
- provider = OEmbed::Provider.new("http://foo.com/oembed")
101
+ describe "the required_query_params option" do
102
+ let(:provider) {
103
+ OEmbed::Provider.new("http://foo.com/oembed", required_query_params: { send_with_query: 'PROVIDER_ENV_VAR' })
104
+ }
105
+
106
+ around(:example) { |example|
107
+ orig_value = ENV['PROVIDER_ENV_VAR']
108
+ ENV['PROVIDER_ENV_VAR'] = env_var_value
109
+ example.run
110
+ ENV['PROVIDER_ENV_VAR'] = orig_value
111
+ }
112
+
113
+ context "with a non-blank env var" do
114
+ let(:env_var_value) { 'non-blank-value' }
115
+
116
+ it "has a working getter" do
117
+ expect(provider.send_with_query).to eq(env_var_value)
118
+ end
119
+
120
+ it "has a working setter" do
121
+ provider.send_with_query = env_var_value.succ
122
+ expect(provider.send_with_query).to eq(env_var_value.succ)
123
+ end
124
+
125
+ it "still throws NoMethodError errors generally" do
126
+ expect { provider.other_query }
127
+ .to raise_error(NoMethodError)
128
+
129
+ expect { provider.other_query = 'val' }
130
+ .to raise_error(NoMethodError)
131
+ end
132
+
133
+ context 'that requires CGI escaping' do
134
+ let(:env_var_value) { 'non-blank y *muy* especiál' }
135
+
136
+ it "has a working getter with escaping" do
137
+ expect(provider.send_with_query).to eq('non-blank+y+%2Amuy%2A+especi%C3%A1l')
138
+ end
139
+ end
140
+
141
+ context "but no env var name for the required_query_params" do
142
+ let(:provider) {
143
+ OEmbed::Provider.new("http://foo.com/oembed", required_query_params: { send_with_query: false })
144
+ }
145
+
146
+ it "has a working getter" do
147
+ expect(provider.send_with_query).to be_nil
148
+ end
149
+
150
+ it "has a working setter" do
151
+ provider.send_with_query = 'non-blank-value'
152
+ expect(provider.send_with_query).to eq('non-blank-value')
153
+ end
154
+ end
155
+ end
89
156
 
90
- expect { provider << "http://*.com/*" }.
91
- to raise_error(ArgumentError)
157
+ context "with a nil env var" do
158
+ let(:env_var_value) { nil }
92
159
 
93
- expect(provider).to_not include("http://foo.com/1")
94
- end
160
+ it "has a working getter" do
161
+ expect(provider.send_with_query).to be_nil
162
+ end
95
163
 
96
- it "should allow a sub-domain wildcard in String URI schema" do
97
- provider = OEmbed::Provider.new("http://foo.com/oembed")
98
- provider << "http://*.foo.com/*"
164
+ it "has a working setter" do
165
+ provider.send_with_query = 'a-new-val'
166
+ expect(provider.send_with_query).to eq('a-new-val')
167
+ end
99
168
 
100
- expect(provider).to include("http://bar.foo.com/1")
101
- expect(provider).to include("http://foo.foo.com/2")
102
- expect(provider).to include("http://foo.com/3")
169
+ it "still throws NoMethodError errors generally" do
170
+ expect { provider.other_query }
171
+ .to raise_error(NoMethodError)
103
172
 
104
- expect(provider).to_not include("https://bar.foo.com/1")
105
- expect(provider).to_not include("http://my.bar.foo.com/1")
173
+ expect { provider.other_query = 'val' }
174
+ .to raise_error(NoMethodError)
175
+ end
176
+ end
106
177
 
107
- provider << "http://my.*.foo.com/*"
178
+ context "where the required_query_param conflicts with an existing method name" do
179
+ let(:provider) {
180
+ OEmbed::Provider.new("http://foo.com/oembed", required_query_params: { get: 'PROVIDER_ENV_VAR' })
181
+ }
182
+ let(:env_var_value) { 'a-conflicted-val' }
183
+
184
+ it "does NOT override the get method" do
185
+ expect { provider.get }
186
+ .to raise_error(ArgumentError) # because get requires arguments!
187
+ end
188
+
189
+ it "still sets up the @required_query_params internals correctly" do
190
+ expect(provider.instance_variable_get('@required_query_params')).to eq({ get: env_var_value })
191
+ end
192
+
193
+ it "DOES have a working setter" do
194
+ provider.get = 'a-new-val'
195
+ expect(provider.instance_variable_get('@required_query_params')).to eq({ get: 'a-new-val' })
196
+ end
197
+ end
198
+ end
108
199
  end
109
200
 
110
- it "should allow multiple sub-domain wildcards in a String URI schema" do
111
- provider = OEmbed::Provider.new("http://foo.com/oembed")
112
- provider << "http://*.my.*.foo.com/*"
201
+ describe "<<" do
202
+ it "should add URL schemes" do
203
+ expect(@flickr.urls).to eq([%r{^http://([^\.]+\.)?flickr\.com/(.*?)}])
204
+ expect(@qik.urls).to eq([
205
+ %r{^http://qik\.com/video/(.*?)},
206
+ %r{^http://qik\.com/(.*?)}
207
+ ])
208
+ end
113
209
 
114
- expect(provider).to include("http://my.bar.foo.com/1")
115
- expect(provider).to include("http://my.foo.com/2")
116
- expect(provider).to include("http://bar.my.bar.foo.com/3")
210
+ it "should match URLs" do
211
+ expect(@flickr).to include(example_url(:flickr))
212
+ expect(@qik).to include(example_url(:qik))
213
+ end
117
214
 
118
- expect(provider).to_not include("http://bar.foo.com/1")
119
- expect(provider).to_not include("http://foo.bar.foo.com/1")
120
- end
215
+ it "should allow a String as a URI schema" do
216
+ provider = OEmbed::Provider.new("http://foo.com/oembed")
217
+ provider << "http://bar.foo.com/*"
121
218
 
122
- it "should NOT allow a scheme wildcard in a String URI schema", :pending => true do
123
- provider = OEmbed::Provider.new("http://foo.com/oembed")
219
+ expect(provider).to include("http://bar.foo.com/1")
220
+ expect(provider).to include("http://bar.foo.com/show/1")
124
221
 
125
- expect { provider << "*://foo.com/*" }.
126
- to raise_error(ArgumentError)
222
+ expect(provider).to_not include("https://bar.foo.com/1")
223
+ expect(provider).to_not include("http://foo.com/1")
224
+ end
127
225
 
128
- expect(provider).to_not include("http://foo.com/1")
129
- end
226
+ it "should allow multiple path wildcards in a String URI schema" do
227
+ provider = OEmbed::Provider.new("http://foo.com/oembed")
228
+ provider << "http://bar.foo.com/*/show/*"
130
229
 
131
- it "should allow a scheme other than http in a String URI schema" do
132
- provider = OEmbed::Provider.new("http://foo.com/oembed")
133
- provider << "https://foo.com/*"
230
+ expect(provider).to include("http://bar.foo.com/photo/show/1")
231
+ expect(provider).to include("http://bar.foo.com/video/show/2")
232
+ expect(provider).to include("http://bar.foo.com/help/video/show/2")
134
233
 
135
- expect(provider).to include("https://foo.com/1")
234
+ expect(provider).to_not include("https://bar.foo.com/photo/show/1")
235
+ expect(provider).to_not include("http://foo.com/video/show/2")
236
+ expect(provider).to_not include("http://bar.foo.com/show/1")
237
+ expect(provider).to_not include("http://bar.foo.com/1")
238
+ end
136
239
 
137
- gopher_url = "gopher://foo.com/1"
138
- expect(provider).to_not include(gopher_url)
139
- provider << "gopher://foo.com/*"
140
- expect(provider).to include(gopher_url)
141
- end
240
+ it "should NOT allow multiple domain wildcards in a String URI schema", :pending => true do
241
+ provider = OEmbed::Provider.new("http://foo.com/oembed")
142
242
 
143
- it "should allow a Regexp as a URI schema" do
144
- provider = OEmbed::Provider.new("http://foo.com/oembed")
145
- provider << %r{^https?://([^\.]*\.)?foo.com/(show/)?\d+}
243
+ expect { provider << "http://*.com/*" }.
244
+ to raise_error(ArgumentError)
146
245
 
147
- expect(provider).to include("http://bar.foo.com/1")
148
- expect(provider).to include("http://bar.foo.com/show/1")
149
- expect(provider).to include("http://foo.com/1")
150
- expect(provider).to include("https://bar.foo.com/1")
246
+ expect(provider).to_not include("http://foo.com/1")
247
+ end
151
248
 
152
- expect(provider).to_not include("http://bar.foo.com/video/1")
153
- expect(provider).to_not include("gopher://foo.com/1")
154
- end
249
+ it "should allow a sub-domain wildcard in String URI schema" do
250
+ provider = OEmbed::Provider.new("http://foo.com/oembed")
251
+ provider << "http://*.foo.com/*"
155
252
 
156
- it "should by default use OEmbed::Formatter.default" do
157
- expect(@flickr.format).to eq(@default)
158
- end
253
+ expect(provider).to include("http://bar.foo.com/1")
254
+ expect(provider).to include("http://foo.foo.com/2")
255
+ expect(provider).to include("http://foo.com/3")
159
256
 
160
- it "should allow xml" do
161
- expect(@qik.format).to eq(:xml)
162
- end
257
+ expect(provider).to_not include("https://bar.foo.com/1")
258
+ expect(provider).to_not include("http://my.bar.foo.com/1")
163
259
 
164
- it "should allow json" do
165
- expect(@viddler.format).to eq(:json)
166
- end
260
+ provider << "http://my.*.foo.com/*"
261
+ end
167
262
 
168
- it "should allow random formats on initialization" do
169
- expect {
170
- yaml_provider = OEmbed::Provider.new("http://foo.com/api/oembed.{format}", :yml)
171
- yaml_provider << "http://foo.com/*"
172
- }.
173
- not_to raise_error
174
- end
263
+ it "should allow multiple sub-domain wildcards in a String URI schema" do
264
+ provider = OEmbed::Provider.new("http://foo.com/oembed")
265
+ provider << "http://*.my.*.foo.com/*"
175
266
 
176
- it "should not allow random formats to be parsed" do
177
- yaml_provider = OEmbed::Provider.new("http://foo.com/api/oembed.{format}", :yml)
178
- yaml_provider << "http://foo.com/*"
179
- yaml_url = "http://foo.com/video/1"
267
+ expect(provider).to include("http://my.bar.foo.com/1")
268
+ expect(provider).to include("http://my.foo.com/2")
269
+ expect(provider).to include("http://bar.my.bar.foo.com/3")
180
270
 
181
- expect(yaml_provider).to receive(:raw).
182
- with(yaml_url, {:format=>:yml}).
183
- and_return(valid_response(:json))
271
+ expect(provider).to_not include("http://bar.foo.com/1")
272
+ expect(provider).to_not include("http://foo.bar.foo.com/1")
273
+ end
184
274
 
185
- expect { yaml_provider.get(yaml_url) }.
186
- to raise_error(OEmbed::FormatNotSupported)
187
- end
275
+ it "should NOT allow a scheme wildcard in a String URI schema", :pending => true do
276
+ provider = OEmbed::Provider.new("http://foo.com/oembed")
188
277
 
189
- it "should add URL schemes" do
190
- expect(@flickr.urls).to eq([%r{^http://([^\.]+\.)?flickr\.com/(.*?)}])
191
- expect(@qik.urls).to eq([%r{^http://qik\.com/video/(.*?)},
192
- %r{^http://qik\.com/(.*?)}])
193
- end
278
+ expect { provider << "*://foo.com/*" }.
279
+ to raise_error(ArgumentError)
194
280
 
195
- it "should match URLs" do
196
- expect(@flickr).to include(example_url(:flickr))
197
- expect(@qik).to include(example_url(:qik))
198
- end
281
+ expect(provider).to_not include("http://foo.com/1")
282
+ end
283
+
284
+ it "should allow a scheme other than http in a String URI schema" do
285
+ provider = OEmbed::Provider.new("http://foo.com/oembed")
286
+ provider << "https://foo.com/*"
199
287
 
200
- it "should raise error if the URL is invalid" do
201
- expect{ @flickr.send(:build, example_url(:fake)) }.to raise_error(OEmbed::NotFound)
202
- expect{ @qik.send(:build, example_url(:fake)) }.to raise_error(OEmbed::NotFound)
288
+ expect(provider).to include("https://foo.com/1")
289
+
290
+ gopher_url = "gopher://foo.com/1"
291
+ expect(provider).to_not include(gopher_url)
292
+ provider << "gopher://foo.com/*"
293
+ expect(provider).to include(gopher_url)
294
+ end
295
+
296
+ it "should allow a Regexp as a URI schema" do
297
+ provider = OEmbed::Provider.new("http://foo.com/oembed")
298
+ provider << %r{^https?://([^\.]*\.)?foo.com/(show/)?\d+}
299
+
300
+ expect(provider).to include("http://bar.foo.com/1")
301
+ expect(provider).to include("http://bar.foo.com/show/1")
302
+ expect(provider).to include("http://foo.com/1")
303
+ expect(provider).to include("https://bar.foo.com/1")
304
+
305
+ expect(provider).to_not include("http://bar.foo.com/video/1")
306
+ expect(provider).to_not include("gopher://foo.com/1")
307
+ end
203
308
  end
204
309
 
205
310
  describe "#build" do
311
+ it "should raise error if the URL is invalid" do
312
+ expect{ @flickr.send(:build, example_url(:fake)) }.to raise_error(OEmbed::NotFound)
313
+ expect{ @qik.send(:build, example_url(:fake)) }.to raise_error(OEmbed::NotFound)
314
+ end
315
+
206
316
  it "should return a proper URL" do
207
317
  uri = @flickr.send(:build, example_url(:flickr))
208
318
  expect(uri.host).to eq("www.flickr.com")
@@ -357,10 +467,77 @@ describe OEmbed::Provider do
357
467
  @viddler.get(example_url(:viddler))
358
468
  end
359
469
 
470
+ context "with require_query_params" do
471
+ let(:provider) { OEmbed::Provider.new("http://foo.com/oembed", required_query_params: { send_with_query: 'PROVIDER_ENV_VAR' }) }
472
+
473
+ it "should add the required_query_params to the URI" do
474
+ provider.send_with_query = 'non-blank-value'
475
+
476
+ expect(provider).to receive(:http_get).
477
+ with(have_attributes(query: match(/send_with_query=non-blank-value/)), :format => @default).
478
+ and_return(valid_response(:json))
479
+ provider.get(example_url(:fake))
480
+ end
481
+
482
+ it "should claim to not match if the required_query_params are missing" do
483
+ allow(provider).to receive(:http_get).and_return(valid_response(:json))
484
+
485
+ expect { provider.get(example_url(:fake)) }.
486
+ to raise_error(OEmbed::NotFound)
487
+ end
488
+ end
489
+
360
490
  it "handles the :timeout option", pending: true do
361
491
  expect_any_instance_of(Net::HTTP).to receive(:open_timeout=).with(5)
362
492
  expect_any_instance_of(Net::HTTP).to receive(:read_timeout=).with(5)
363
493
  @flickr.get(example_url(:flickr), :timeout => 5)
364
494
  end
365
495
  end
496
+
497
+ describe "#set_required_query_params" do
498
+ let(:provider) { OEmbed::Provider.new("http://foo.com/oembed", required_query_params: { send_with_query: 'PROVIDER_ENV_VAR' }) }
499
+
500
+ around(:example) { |example|
501
+ orig_value = ENV['PROVIDER_ENV_VAR']
502
+ ENV['PROVIDER_ENV_VAR'] = 'a non-nil value'
503
+ example.run
504
+ ENV['PROVIDER_ENV_VAR'] = orig_value
505
+ }
506
+
507
+ it 'META: the around works as expected' do
508
+ expect(provider.send_with_query).to eq('a+non-nil+value')
509
+ expect(provider.required_query_params_set?).to be_truthy
510
+ end
511
+
512
+ [
513
+ [true, 'true'],
514
+ [false, 'false'],
515
+ ['one two', 'one+two'],
516
+ ['a@&?%25', 'a%40%26%3F%2525'],
517
+ ].each do |given_value, expected_value|
518
+ context "given #{given_value.inspect}" do
519
+ before(:each) { provider.send_with_query = given_value }
520
+
521
+ it "stringifies and escapes the value" do
522
+ expect(provider.send_with_query).to eq(expected_value)
523
+ end
524
+
525
+ it "satisfies required_query_params_set?" do
526
+ expect(provider.required_query_params_set?).to be_truthy
527
+ end
528
+ end
529
+ end
530
+
531
+ context "given nil" do
532
+ before(:each) { provider.send_with_query = nil }
533
+
534
+ it "nils the existing value" do
535
+ expect(provider.send_with_query).to be_nil
536
+ end
537
+
538
+ it "sets required_query_params_set? to falsey" do
539
+ expect(provider.required_query_params_set?).to be_falsey
540
+ end
541
+ end
542
+ end
366
543
  end