lita-wikipedia 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 12f1d142cd1b10e672409e74011f685d5216eccc
4
- data.tar.gz: 9f3f55d0747dafdb7dcafeb67637f603f1a69e9b
3
+ metadata.gz: 1d7a4b0b1a9e02e1cd16d926732544161225143c
4
+ data.tar.gz: 7c58e5072db8990a2bbf1a992c6aa0747544a102
5
5
  SHA512:
6
- metadata.gz: 4c9ba9a065c9112b74c57390241d7fa0257b06e560089be1ba6d3a4d70c531cd73d6850bcb4681b31d33277233c98b58c52c34135b7f549be4776b95780221e8
7
- data.tar.gz: 84bc302f77a9a0b3d5a059c8eee25abe6743301aaf7415206151d4a6e77e7deb031cf06b58c19b6a728e438bd4887d5159be953dd98b8a89861cf3f263072b6f
6
+ metadata.gz: f193331786e2a15fdf77aa2a8a9c0ca4dd3555a5c14025d36d113514cb9122aa496460acab713109b7cbe0f7a28b9c8ea6624a9c8f76055fc356ca9da242ef55
7
+ data.tar.gz: b3542a293b98b39fca8dd8fe6ce30b25ee0433739f4dc278064857e7ab3d20eb8221b31459af380bc0226b2a8abffca0663c096bc499ef422c037b3697a558db
@@ -1,7 +1,21 @@
1
- require "cgi"
2
1
  require "json"
3
2
  require "open-uri"
4
3
 
4
+ def disambiguate(term)
5
+ url = "http://en.wikipedia.org/w/api.php?action=query&prop=extracts|info|links|pageprops&format=json&exintro=&explaintext=&inprop=url&ppprop=disambiguation&titles=#{term}&redirects="
6
+ result = JSON.parse(open(URI.parse(URI.encode(url.strip))).read)
7
+ page = result['query']['pages'].first[1]
8
+ extract = page['extract'].split("\n").first
9
+ if page.fetch('pageprops', {}).has_key? 'disambiguation'
10
+ links = page['links'].map {
11
+ |x| x['title'] if not x['title'].downcase.include? 'disambiguation'}
12
+ links = links.select {|x| x != nil}
13
+ return disambiguate(links.sample)
14
+ end
15
+ url = page['fullurl']
16
+ [extract, url]
17
+ end
18
+
5
19
  module Lita
6
20
  module Handlers
7
21
  class Wikipedia < Handler
@@ -9,11 +23,9 @@ module Lita
9
23
  help: { t("help.wikipedia_key") => t("help.wikipedia_value")})
10
24
 
11
25
  def wikipedia(response)
12
- url = "http://en.wikipedia.org/w/api.php?action=query&prop=extracts|info&format=json&exintro=&explaintext=&inprop=url&titles=#{CGI.escape(response.matches.first.first)}&redirects="
13
- result = JSON.parse(open(URI.parse(URI.encode(url.strip))).read)
14
- page = result['query']['pages'].first[1]
15
- response.reply(page['extract'].split("\n").first)
16
- response.reply("Source: #{page['fullurl']}")
26
+ extract, url = disambiguate(response.matches.first.first)
27
+ response.reply(extract)
28
+ response.reply("Source: #{url}")
17
29
  end
18
30
  end
19
31
 
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |spec|
2
2
  spec.name = "lita-wikipedia"
3
- spec.version = "0.0.1"
3
+ spec.version = "0.0.2"
4
4
  spec.authors = ["Tristan Chong"]
5
5
  spec.email = ["ong@tristaneuan.ch"]
6
6
  spec.description = %q{A Lita handler that returns a requested Wikipedia article.}
@@ -10,5 +10,29 @@ describe Lita::Handlers::Wikipedia, lita_handler: true do
10
10
  expect(replies[0]).to match 'Ruby is a dynamic, reflective, object-oriented, general-purpose programming language. It was designed and developed in the mid-1990s by Yukihiro "Matz" Matsumoto in Japan.'
11
11
  expect(replies[1]).to match 'Source: http://en.wikipedia.org/wiki/Ruby_(programming_language)'
12
12
  end
13
+
14
+ it "responds with a random matching article when disambiguation occurs" do
15
+ send_command "wiki 12th man"
16
+ responses = [
17
+ "Source: http://en.wikipedia.org/wiki/The_Twelfth_Man",
18
+ "Source: http://en.wikipedia.org/wiki/The_12th_Man_(album)",
19
+ "Source: http://en.wikipedia.org/wiki/12th_man_(football)",
20
+ "Source: http://en.wikipedia.org/wiki/Glossary_of_cricket_terms"
21
+ ]
22
+ expect(responses).to include(replies[1])
23
+ end
24
+ end
25
+
26
+ describe "#disambiguate" do
27
+ it "resolves disambiguation by returning a random matching article" do
28
+ extract, url = disambiguate('12th man')
29
+ urls = [
30
+ "http://en.wikipedia.org/wiki/The_Twelfth_Man",
31
+ "http://en.wikipedia.org/wiki/The_12th_Man_(album)",
32
+ "http://en.wikipedia.org/wiki/12th_man_(football)",
33
+ "http://en.wikipedia.org/wiki/Glossary_of_cricket_terms"
34
+ ]
35
+ expect(urls).to include(url)
36
+ end
13
37
  end
14
38
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lita-wikipedia
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tristan Chong