lita-wikipedia 0.0.3 → 0.0.4

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: 40b8f40a3e943e471aecad7d9b0456a23f8ea769
4
- data.tar.gz: bc09c63e6a5c07ad198289c638aafbfb2fa8cd1b
3
+ metadata.gz: 72e591050c1024e2dc4b514a9187cd59e415d395
4
+ data.tar.gz: f32aa2df972accca079eafeb969c2fa64783fe02
5
5
  SHA512:
6
- metadata.gz: 487aa8c98c6f40fc139f3b4bf5cfa9a3bf93dece4b6c0114c53e3e782809ce999f8cf0be008bd326848aedfaf5d28101a51bf3fb1ed3f27497be1d4b2fc6efaf
7
- data.tar.gz: cac8d98a51381107bb6c17822144b4b117a5eac31591fd4a84c60cb9de437fb3ab1ac80cee93f0b856d4a29242da0164156ca98df6ed2a849a8ee312c1afba68
6
+ metadata.gz: 680a12419842dc1c8a4bd83e69fa3da774c55365e25d11ca1996bf9e8c505dfac83d1ca154ef255e59ea9c31cd1b0806512c0b119a201fc3a4388194fd858320
7
+ data.tar.gz: 23a9e12371f4726dcd18210a03ef7b48b22f6a463784abc345730576ae77390fb34e66bedb4fdd27e54a7465abea1f6511fa06b5f9a22863faa066620b5a6ec7
data/README.md CHANGED
@@ -17,6 +17,7 @@ To request a Wikipedia article:
17
17
  ```
18
18
  Lita: wiki ruby language
19
19
  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.
20
+ Source: http://en.wikipedia.org/wiki/Ruby_(programming_language)
20
21
  ```
21
22
 
22
23
  ## License
@@ -1,23 +1,7 @@
1
1
  require "json"
2
+ require "nokogiri"
2
3
  require "open-uri"
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
- if not page.has_key? 'extract'
9
- return ["No Wikipedia entry found for '#{term}'.", nil]
10
- end
11
- extract = page['extract'].split("\n").first
12
- if page.fetch('pageprops', {}).has_key? 'disambiguation'
13
- links = page['links'].map {
14
- |x| x['title'] if not x['title'].downcase.include? 'disambiguation'}
15
- links = links.select {|x| x != nil}
16
- return disambiguate(links.sample)
17
- end
18
- url = page['fullurl']
19
- [extract, url]
20
- end
4
+ require "sanitize"
21
5
 
22
6
  module Lita
23
7
  module Handlers
@@ -26,12 +10,24 @@ module Lita
26
10
  help: { t("help.wikipedia_key") => t("help.wikipedia_value")})
27
11
 
28
12
  def wikipedia(response)
29
- extract, url = disambiguate(response.matches.first.first)
30
- response.reply(extract)
31
- if url != nil
13
+ term = response.matches.first.first
14
+ search_url = "http://en.wikipedia.org/w/api.php?action=opensearch&search=#{term}&format=json"
15
+ search_result = JSON.parse(open(URI.parse(URI.encode(search_url.strip))).read)
16
+ titles = search_result[1]
17
+ if titles.empty?
18
+ response.reply("No Wikipedia entry found for '#{term}'")
19
+ else
20
+ query_url = "http://en.wikipedia.org/w/api.php?action=query&prop=extracts|info&format=json&exintro=&inprop=url&titles=#{titles.first}&redirects="
21
+ query_result = JSON.parse(open(URI.parse(URI.encode(query_url.strip))).read)
22
+ page = query_result['query']['pages'].first[1]
23
+ html = Nokogiri::HTML(page['extract'])
24
+ extract = Sanitize.fragment(html.xpath('//p[1]').first).strip
25
+ url = page['fullurl']
26
+ response.reply(extract)
32
27
  response.reply("Source: #{url}")
33
28
  end
34
29
  end
30
+
35
31
  end
36
32
 
37
33
  Lita.register_handler(Wikipedia)
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |spec|
2
2
  spec.name = "lita-wikipedia"
3
- spec.version = "0.0.3"
3
+ spec.version = "0.0.4"
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.}
@@ -15,6 +15,8 @@ Gem::Specification.new do |spec|
15
15
  spec.require_paths = ["lib"]
16
16
 
17
17
  spec.add_runtime_dependency "lita", ">= 3.1"
18
+ spec.add_runtime_dependency "nokogiri", ">= 1.6.6"
19
+ spec.add_runtime_dependency "sanitize", ">= 3.1.2"
18
20
 
19
21
  spec.add_development_dependency "bundler", "~> 1.3"
20
22
  spec.add_development_dependency "rake"
@@ -1,8 +1,8 @@
1
1
  require "spec_helper"
2
2
 
3
3
  describe Lita::Handlers::Wikipedia, lita_handler: true do
4
- it { routes_command("wikipedia ruby language").to(:wikipedia) }
5
- it { routes_command("wiki ruby language").to(:wikipedia) }
4
+ it { is_expected.to route_command("wikipedia ruby language").to(:wikipedia) }
5
+ it { is_expected.to route_command("wiki ruby language").to(:wikipedia) }
6
6
 
7
7
  describe "#wikipedia" do
8
8
  it "returns the 1st paragraph & link to the Wikipedia entry for a query" do
@@ -11,33 +11,26 @@ describe Lita::Handlers::Wikipedia, lita_handler: true do
11
11
  expect(replies[1]).to match 'Source: http://en.wikipedia.org/wiki/Ruby_(programming_language)'
12
12
  end
13
13
 
14
- it "responds with a random matching article when disambiguation occurs" do
14
+ it "handles commas in queries correctly" do
15
+ send_command "wiki indio, ca"
16
+ expect(replies[1]).to match 'Source: http://en.wikipedia.org/wiki/Indio,_California'
17
+ end
18
+
19
+ it "handles lowercase queries and articles with templates correctly" do
20
+ send_command "wiki gabriola island"
21
+ expect(replies[0]).not_to include 'redirects here'
22
+ expect(replies[1]).to match 'Source: http://en.wikipedia.org/wiki/Gabriola_Island'
23
+ end
24
+
25
+ it "responds with the disambiguation page on appropriate queries" do
15
26
  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])
27
+ expect(replies[0]).to match 'The phrase 12th Man or Twelfth Man can refer to:'
28
+ expect(replies[1]).to match 'Source: http://en.wikipedia.org/wiki/12th_Man'
23
29
  end
24
30
 
25
31
  it "returns an error message if no article is found" do
26
32
  send_command "wiki asdfasdfa"
27
- expect(replies.first).to match "No Wikipedia entry found for 'asdfasdfa'."
28
- end
29
- end
30
-
31
- describe "#disambiguate" do
32
- it "resolves disambiguation by returning a random matching article" do
33
- extract, url = disambiguate('12th man')
34
- urls = [
35
- "http://en.wikipedia.org/wiki/The_Twelfth_Man",
36
- "http://en.wikipedia.org/wiki/The_12th_Man_(album)",
37
- "http://en.wikipedia.org/wiki/12th_man_(football)",
38
- "http://en.wikipedia.org/wiki/Glossary_of_cricket_terms"
39
- ]
40
- expect(urls).to include(url)
33
+ expect(replies.first).to match "No Wikipedia entry found for 'asdfasdfa'"
41
34
  end
42
35
  end
43
36
  end
data/spec/spec_helper.rb CHANGED
@@ -8,3 +8,4 @@ SimpleCov.start { add_filter "/spec/" }
8
8
 
9
9
  require "lita-wikipedia"
10
10
  require "lita/rspec"
11
+ Lita.version_3_compatibility_mode = false
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lita-wikipedia
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tristan Chong
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-05-04 00:00:00.000000000 Z
11
+ date: 2015-04-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: lita
@@ -24,6 +24,34 @@ dependencies:
24
24
  - - ">="
25
25
  - !ruby/object:Gem::Version
26
26
  version: '3.1'
27
+ - !ruby/object:Gem::Dependency
28
+ name: nokogiri
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: 1.6.6
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: 1.6.6
41
+ - !ruby/object:Gem::Dependency
42
+ name: sanitize
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: 3.1.2
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: 3.1.2
27
55
  - !ruby/object:Gem::Dependency
28
56
  name: bundler
29
57
  requirement: !ruby/object:Gem::Requirement