This repository has been archived by the owner on Jul 29, 2021. It is now read-only.
/
wikipedia_api_spec.rb
590 lines (494 loc) · 19.9 KB
/
wikipedia_api_spec.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
# encoding: utf-8
require 'spec_helper'
require 'wikipedia_api'
describe WikipediaApi do
context "escaping a page title to a DBpedia key" do
it "should apply the encoding rules from dbpedia.org" do
WikipediaApi.title_to_dbpedia_key('Mozambique (Portugal)').should == 'Mozambique_(Portugal)'
WikipediaApi.title_to_dbpedia_key('S/2012_P_1').should == 'S/2012_P_1'
end
end
context "parsing a page" do
before :each do
FakeWeb.register_uri(
:get, 'https://en.wikipedia.org/w/api.php?action=parse&format=json&pageid=934787&prop=text%7Cdisplaytitle',
:body => fixture_data('parse-934787.json'),
:content_type => 'application/json'
)
@data = WikipediaApi.parse(934787)
end
it "should return a Hash" do
@data.should be_a(Hash)
end
it "should return the article's page url title" do
@data['title'].should == 'Ceres, Fife'
end
it "should return the article display title" do
@data['displaytitle'].should == 'Ceres, Fife'
end
it "should return the date it was last updated" do
@data['updated_at'].to_s.should == '2012-05-05T04:35:21+00:00'
@data['updated_at'].class.should == DateTime
end
it "should return the longitude" do
@data['longitude'].should == -2.971445
end
it "should return the latitude" do
@data['latitude'].should == 56.29205
end
it "should return the article abstract" do
@data['abstract'].should =~ /\ACeres is a village in Fife, Scotland/
end
it "should return an array of images" do
@data['images'].should == [
'http://upload.wikimedia.org/wikipedia/commons/c/cd/Fife_UK_location_map.svg',
'http://upload.wikimedia.org/wikipedia/commons/0/04/Ceres%2C_Fife.jpg',
'http://upload.wikimedia.org/wikipedia/commons/5/5e/The_Green_at_Ceres%2C_Fife.jpg',
'http://upload.wikimedia.org/wikipedia/commons/1/19/Ceres_Church%2C_Fife_Scotland.jpg',
'http://upload.wikimedia.org/wikipedia/commons/3/36/The_Provost%2C_Ceres_Fife.jpg',
]
end
it "should return an array of external links" do
@data['externallinks'].should == [
'http://www.fife.50megs.com/ceres-history.htm'
]
end
end
context "parsing a page titled with a lowercase first letter" do
before :each do
FakeWeb.register_uri(
:get, 'https://en.wikipedia.org/w/api.php?action=parse&format=json&pageid=21492980&prop=text%7Cdisplaytitle',
:body => fixture_data('parse-21492980.json'),
:content_type => 'application/json'
)
@data = WikipediaApi.parse(21492980)
end
it "should return a hash" do
@data.should be_a(Hash)
end
it "should return the article's page url title" do
@data['title'].should == 'IMac'
end
it "should return the article display title" do
@data['displaytitle'].should == 'iMac'
end
it "should have no latitude and longitude" do
@data['latitude'].should be_nil
@data['longitude'].should be_nil
end
it "should return the article abstract" do
@data['abstract'].should =~ /^The iMac is a range of all-in-one Macintosh desktop computers built by Apple Inc\./
end
end
context "parsing a page with HTML in the display title" do
before :each do
FakeWeb.register_uri(
:get, 'https://en.wikipedia.org/w/api.php?action=parse&format=json&pageid=6268880&prop=text%7Cdisplaytitle',
:body => fixture_data('parse-6268880.json'),
:content_type => 'application/json'
)
@data = WikipediaApi.parse(6268880)
end
it "should return a hash" do
@data.should be_a(Hash)
end
it "should return the article's page url title" do
@data['title'].should == "On Her Majesty's Secret Service (film)"
end
it "should return the article display title" do
@data['displaytitle'].should == "On Her Majesty's Secret Service (film)"
end
it "should return the article abstract" do
@data['abstract'].should =~ /^On Her Majesty's Secret Service \(1969\) is the sixth spy film in the James Bond series/
end
end
context "parsing a page with <p> in the infobox" do
before :each do
FakeWeb.register_uri(
:get, 'https://en.wikipedia.org/w/api.php?action=parse&format=json&pageid=26471&prop=text%7Cdisplaytitle',
:body => fixture_data('parse-26471.json'),
:content_type => 'application/json'
)
@data = WikipediaApi.parse(26471)
end
it "should return a hash" do
@data.should be_a(Hash)
end
it "should return the article's page url title" do
@data['title'].should == 'Rat'
end
it "should return the article display title" do
@data['displaytitle'].should == 'Rat'
end
it "should have no latitude and longitude" do
@data['latitude'].should be_nil
@data['longitude'].should be_nil
end
it "should return the article abstract" do
@data['abstract'].should =~ /\ARats are various medium-sized, long-tailed rodents of the superfamily Muroidea/
end
end
context "parsing a page with a superscript in the title and abstract" do
before :each do
FakeWeb.register_uri(
:get, 'https://en.wikipedia.org/w/api.php?action=parse&format=json&pageid=10841865&prop=text%7Cdisplaytitle',
:body => fixture_data('parse-10841865.json'),
:content_type => 'application/json'
)
@data = WikipediaApi.parse(10841865)
end
it "should return the article's page url title" do
@data['title'].should == 'E=MC2 (song)'
end
it "should return the article display title" do
@data['displaytitle'].should == 'E=MC2 (song)'
end
it "should return the article abstract" do
@data['abstract'].should =~ /^"E=MC2" is a 1985 single by Big Audio Dynamite/
end
end
context "parsing a page with multiple paragraphs" do
before :each do
FakeWeb.register_uri(
:get, 'https://en.wikipedia.org/w/api.php?action=parse&format=json&pageid=18624945&prop=text%7Cdisplaytitle',
:body => fixture_data('parse-18624945.json'),
:content_type => 'application/json'
)
@data = WikipediaApi.parse(18624945)
end
it "should return a hash" do
@data.should be_a(Hash)
end
it "should return the article's page url title" do
@data['title'].should == 'True Blood'
end
it "should return the article display title without HTML" do
@data['displaytitle'].should == 'True Blood'
end
it "should the first paragraph of the abstract" do
@data['abstract'].should =~ %r[^True Blood is an American television series created and produced by Alan Ball]
end
it "should truncate the end of the second paragraph" do
@data['abstract'].should =~ %r[received critical acclaim and won several awards, including one Golden\.\.\.\Z]
end
end
context "removing pronunciation from abstracts" do
it "should remove the IPA pronunciation from the article about London" do
FakeWeb.register_uri(
:get, 'https://en.wikipedia.org/w/api.php?action=parse&format=json&pageid=17867&prop=text%7Cdisplaytitle',
:body => fixture_data('parse-17867.json'),
:content_type => 'application/json'
)
data = WikipediaApi.parse(17867)
data['abstract'].should =~ /^London is the capital city of England and the United Kingdom/
end
it "should remove the IPA pronunciation from the article about Paris" do
FakeWeb.register_uri(
:get, 'https://en.wikipedia.org/w/api.php?action=parse&format=json&pageid=22989&prop=text%7Cdisplaytitle',
:body => fixture_data('parse-22989.json'),
:content_type => 'application/json'
)
data = WikipediaApi.parse(22989)
data['abstract'].should =~ /^Paris is the capital and largest city of France\./
end
it "should remove the IPA pronunciation from the article about Berlin" do
FakeWeb.register_uri(
:get, 'https://en.wikipedia.org/w/api.php?action=parse&format=json&pageid=3354&prop=text%7Cdisplaytitle',
:body => fixture_data('parse-3354.json'),
:content_type => 'application/json'
)
data = WikipediaApi.parse(3354)
data['abstract'].should =~ /^Berlin is the capital city of Germany/
end
it "should remove the IPA pronunciation from the article about Lyon" do
FakeWeb.register_uri(
:get, 'https://en.wikipedia.org/w/api.php?action=parse&format=json&pageid=8638634&prop=text%7Cdisplaytitle',
:body => fixture_data('parse-8638634.json'),
:content_type => 'application/json'
)
data = WikipediaApi.parse(8638634)
data['abstract'].should =~ /^Lyon, traditionally spelt Lyons in English, is a city in east-central France in the Rhône-Alpes region/
end
it "should remove the pronunciation audio link from the article about Auchtertool" do
FakeWeb.register_uri(
:get, 'https://en.wikipedia.org/w/api.php?action=parse&format=json&pageid=9259027&prop=text%7Cdisplaytitle',
:body => fixture_data('parse-9259027.json'),
:content_type => 'application/json'
)
data = WikipediaApi.parse(9259027)
data['abstract'].should =~ /^Auchtertool is a small village in Fife, Scotland\./
end
it "should remove the pronunciation audio link from the article about Canada" do
FakeWeb.register_uri(
:get, 'https://en.wikipedia.org/w/api.php?action=parse&format=json&pageid=5042916&prop=text%7Cdisplaytitle',
:body => fixture_data('parse-5042916.json'),
:content_type => 'application/json'
)
data = WikipediaApi.parse(5042916)
data['abstract'].should =~ /^Canada is a North American country consisting of ten provinces and three territories\./
end
it "should remove the pronunciation from the article about Albert Camus" do
FakeWeb.register_uri(
:get, 'https://en.wikipedia.org/w/api.php?action=parse&format=json&pageid=983&prop=text%7Cdisplaytitle',
:body => fixture_data('parse-983.json'),
:content_type => 'application/json'
)
data = WikipediaApi.parse(983)
data['abstract'].should =~ /^Albert Camus was a French author, journalist, and philosopher\./
end
it "should remove the pronunciation from the article about Anton Corbijn" do
FakeWeb.register_uri(
:get, 'https://en.wikipedia.org/w/api.php?action=parse&format=json&pageid=1073796&prop=text%7Cdisplaytitle',
:body => fixture_data('parse-1073796.json'),
:content_type => 'application/json'
)
data = WikipediaApi.parse(1073796)
data['abstract'].should =~ /^Anton Corbijn is a Dutch photographer, music video and film director\./
end
it "should remove the pronunciation from the article about Sara Beth Bareilles article" do
FakeWeb.register_uri(
:get, 'https://en.wikipedia.org/w/api.php?action=parse&format=json&pageid=6100815&prop=text%7Cdisplaytitle',
:body => fixture_data('parse-6100815.json'),
:content_type => 'application/json'
)
data = WikipediaApi.parse(6100815)
data['abstract'].should =~ /^Sara Beth Bareilles is an American singer-songwriter and pianist\./
end
end
context "parsing a page with an edit link in the page" do
before :each do
FakeWeb.register_uri(
:get, 'https://en.wikipedia.org/w/api.php?action=parse&format=json&pageid=32838&prop=text%7Cdisplaytitle',
:body => fixture_data('parse-32838.json'),
:content_type => 'application/json'
)
@data = WikipediaApi.parse(32838)
end
it "should have a page url title" do
@data['title'].should == 'Vincent Ward'
end
it "should have a display title" do
@data['displaytitle'].should == 'Vincent Ward'
end
it "should pull out the correct abstract" do
@data['abstract'].should == 'Vincent Ward, ONZM (born 16 February 1956) is a film director and screenwriter.'
end
it "should have an array of external links" do
@data['externallinks'].should == [
'http://www.imdb.com/name/nm0911910/'
]
end
end
context "parsing a redirect page" do
before :each do
FakeWeb.register_uri(
:get, 'https://en.wikipedia.org/w/api.php?action=parse&format=json&pageid=440555&prop=text%7Cdisplaytitle',
:body => fixture_data('parse-440555.json'),
:content_type => 'application/json'
)
FakeWeb.register_uri(
:get, 'https://en.wikipedia.org/w/api.php?action=query&format=json&inprop=displaytitle&prop=info&redirects=1&titles=Bovine%20spongiform%20encephalopathy',
:body => fixture_data('pageinfo-bse.json'),
:content_type => 'application/json'
)
end
it "should raise a Redirect exception" do
lambda {WikipediaApi.parse(440555)}.should raise_error(
WikipediaApi::Redirect
)
end
end
context "parsing a non-existant page" do
before :each do
FakeWeb.register_uri(
:get, 'https://en.wikipedia.org/w/api.php?action=parse&format=json&pageid=504825766&prop=text%7Cdisplaytitle',
:body => fixture_data('parse-504825766.json'),
:content_type => 'application/json'
)
end
it "should raise an exception" do
lambda {WikipediaApi.parse(504825766)}.should raise_error(
MediaWikiApi::NotFound,
'There is no page with ID 504825766'
)
end
end
context "getting information about a page by title" do
before :each do
FakeWeb.register_uri(
:get, 'https://en.wikipedia.org/w/api.php?action=query&format=json&inprop=displaytitle&prop=info&redirects=1&titles=U2',
:body => fixture_data('pageinfo-u2.json'),
:content_type => 'application/json'
)
@data = WikipediaApi.page_info(:titles => 'U2')
end
it "should return the article's page url title" do
@data['title'].should == 'U2'
end
it "should return the article display title" do
@data['displaytitle'].should == 'U2'
end
it "should return the pageid" do
@data['pageid'].should == 52780
end
it "should return the namespace" do
@data['ns'].should == 0
end
it "should return the last modified date" do
@data['touched'].should == "2012-05-07T12:24:23Z"
end
end
context "getting information about a page by pageid" do
before :each do
FakeWeb.register_uri(
:get, 'https://en.wikipedia.org/w/api.php?action=query&format=json&inprop=displaytitle&pageids=4309010&prop=info&redirects=1',
:body => fixture_data('pageinfo-4309010.json'),
:content_type => 'application/json'
)
@data = WikipediaApi.page_info(:pageids => '4309010')
end
it "should return the page url title" do
@data['title'].should == 'Category:Villages in Fife'
end
it "should return the display title" do
@data['displaytitle'].should == 'Category:Villages in Fife'
end
it "should return the pageid" do
@data['pageid'].should == 4309010
end
it "should return the namespace" do
@data['ns'].should == 14
end
it "should return the last modified date" do
@data['touched'].should == "2018-09-19T14:17:35Z"
end
end
context "getting information about a page with HTML in the display title" do
before :each do
FakeWeb.register_uri(
:get, 'https://en.wikipedia.org/w/api.php?action=query&format=json&inprop=displaytitle&pageids=18624945&prop=info&redirects=1',
:body => fixture_data('pageinfo-18624945.json'),
:content_type => 'application/json'
)
@data = WikipediaApi.page_info(:pageids => '18624945')
end
it "should return the page url title" do
@data['title'].should == 'True Blood'
end
it "should return the display title without HTML" do
@data['displaytitle'].should == 'True Blood'
end
it "should return the pageid" do
@data['pageid'].should == 18624945
end
it "should return the namespace" do
@data['ns'].should == 0
end
it "should return the last modified date" do
@data['touched'].should == "2012-05-07T13:33:10Z"
end
end
context "getting information about a page title that doesn't exist" do
before :each do
FakeWeb.register_uri(
:get, 'https://en.wikipedia.org/w/api.php?action=query&format=json&inprop=displaytitle&prop=info&redirects=1&titles=zsefpfs',
:body => fixture_data('pageinfo-zsefpfs.json'),
:content_type => 'application/json'
)
end
it "should trow a PageNotFound exception" do
lambda { WikipediaApi.page_info(:titles => 'zsefpfs') }.should raise_error(
MediaWikiApi::NotFound
)
end
end
context "a call to Wikipedia API returns something that isn't JSON" do
before :each do
FakeWeb.register_uri(
:get, 'https://en.wikipedia.org/w/api.php?action=query&format=json',
:body => "<h1>There was an error</h1>",
:content_type => 'text/html'
)
end
it "should raise an exception" do
expect { WikipediaApi.get('query') }.to raise_error(
MediaWikiApi::Exception,
'Response from MediaWiki API was not of type application/json.'
)
end
end
context "searching for Rat" do
before :each do
FakeWeb.register_uri(
:get, 'https://en.wikipedia.org/w/api.php?action=query&format=json&list=search&srlimit=20&srprop=snippet%7Ctitlesnippet&srsearch=Rat',
:body => fixture_data('search-rat.json'),
:content_type => 'application/json'
)
@data = WikipediaApi.search('Rat', :srlimit => 20)
end
it "the first result should have a title" do
@data.first['title'].should == 'Rat'
end
it "the first result should have a title snippet" do
@data.first['titlesnippet'].should == "<span class='searchmatch'>Rat</span>"
end
it "the first result should have a namespace" do
@data.first['ns'].should == 0
end
it "the first result should have a snippet" do
@data.first['snippet'].should =~ /^"True rats" are members of the genus Rattus/
end
end
context "getting the members of a category" do
before :each do
FakeWeb.register_uri(
:get, 'https://en.wikipedia.org/w/api.php?action=query&format=json&gcmlimit=500&gcmnamespace=0%7C14&gcmpageid=4309010&generator=categorymembers&inprop=displaytitle&prop=info',
:body => fixture_data('categorymembers-4309010.json'),
:content_type => 'application/json'
)
@data = WikipediaApi.category_members(4309010)
@data.sort! {|a,b| a['pageid'] <=> b['pageid']}
end
it "should return eighty three results" do
@data.size.should == 83
end
it "should return a page url title for the first result" do
@data.first['title'].should == 'Aberdour'
end
it "should return the page display title" do
@data.first['displaytitle'].should == 'Aberdour'
end
it "should return a pageid for the first result" do
@data.first['pageid'].should == 2712
end
it "should return a namespace for the first result" do
@data.first['ns'].should == 0
end
end
context "getting the categories that something is a member of" do
before :each do
FakeWeb.register_uri(
:get, 'https://en.wikipedia.org/w/api.php?action=query&format=json&gcllimit=500&generator=categories&inprop=displaytitle&pageids=934787&prop=info',
:body => fixture_data('categories-934787.json'),
:content_type => 'application/json'
)
@data = WikipediaApi.page_categories(934787)
@data.sort! {|a,b| a['pageid'] <=> b['pageid']}
end
it "should return 4 results" do
@data.size.should == 4
end
it "should return a title for the first result" do
@data.first['title'].should == 'Category:Villages in Fife'
end
it "should return the article display title" do
@data.first['displaytitle'].should == 'Category:Villages in Fife'
end
it "should return a pageid for the first result" do
@data.first['pageid'].should == 4309010
end
it "should return a namespace for the first result" do
@data.first['ns'].should == 14
end
end
end