lita-wolfram-alpha 0.1.1 → 0.1.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: 25305ff0dfbb1bdaee1124d6fcdff9fd2f3d5a9c
4
- data.tar.gz: f3e634e4a6b4954ce0532f6172bda36be9d76689
3
+ metadata.gz: 1de85e0695441eee310926d81fa4c45c6610883d
4
+ data.tar.gz: 4aa9fffde8f4ca63934fdf72c1842624b0692090
5
5
  SHA512:
6
- metadata.gz: babb79e4ee75e6bf621ae413a0e3c732850a002833e229fcdbbae956208062bac14047a602ceedcaa5b18d47beab01147294ce97f50f716999b163115575beba
7
- data.tar.gz: 74c83bc1481b2e6614cf1c5f64824da78aa3f57cdec2fb3d4358c538baf47673b484b682daf45184a8cc20fc1bd1a8614da36f9868d3fdee880bd924325cc95d
6
+ metadata.gz: fbd8e1caf7eb4f9457b217b56be8d518f540df621bed7de50ec596926ee3c58590e3ae516c96c7a77872475d1344a4cae95394961571241004c4b602bc62c998
7
+ data.tar.gz: b4bdfd7da310d9d88068e78e7f6e6599c4255fc64be14e12cf3f0a7bf4b8297f7300c53757fd38d6d1a0f585b231e4b343056efb56def465ec76608b24add92c
data/README.md CHANGED
@@ -24,7 +24,7 @@ end
24
24
  ```
25
25
 
26
26
  ### Optional attributes
27
- * `hide_link` (boolean) - By default, Lita returns a Wolfram Alpha link containing additional information after providing the best answer. If `hide_link` is set to true, this link is omitted.
27
+ * `hide_link` (boolean) - By default, Lita returns a Wolfram Alpha link containing additional information after providing the best answer. If `hide_link` is set to `true`, this link is omitted.
28
28
 
29
29
  ``` ruby
30
30
  Lita.configure do |config|
@@ -22,16 +22,16 @@ module Lita
22
22
  appid: config.app_id
23
23
  )
24
24
  return if http_response.status != 200
25
- link = "Learn more: http://www.wolframalpha.com/input/?i=#{CGI.escape(input)}"
25
+ link = "http://www.wolframalpha.com/input/?i=#{CGI.escape(input)}"
26
26
  xml = Nokogiri::XML(http_response.body)
27
27
  primary = xml.at_xpath("//pod[@primary='true']//plaintext[string-length(text()) > 0]")
28
28
  if primary
29
- response.reply primary.content
29
+ response.reply format_reply(primary)
30
30
  response.reply link unless config.hide_link
31
31
  else
32
32
  secondary = xml.at_xpath("//pod[not(@id='Input')]//plaintext[string-length(text()) > 0]")
33
33
  if secondary
34
- response.reply secondary.content
34
+ response.reply format_reply(secondary)
35
35
  response.reply link unless config.hide_link
36
36
  else
37
37
  response.reply "¯\\_(ツ)_/¯"
@@ -39,6 +39,23 @@ module Lita
39
39
  end
40
40
  end
41
41
 
42
+ private
43
+
44
+ # pod: plaintext
45
+ # pod -> subpod: plaintext
46
+ # subpod: plaintext
47
+ # plaintext
48
+ def format_reply(node)
49
+ subpod = node.parent
50
+ subpod_title = subpod.attribute("title").content
51
+ pod = subpod.parent
52
+ pod_title = pod.attribute("title").content
53
+ title = [pod_title, subpod_title].select {|t| t.length > 0 && t !~ /^Results?$/}.join(" -> ")
54
+ colon = title.length > 0 ? ":" : ""
55
+ whitespace = node.content =~ /\n/ ? "\n" : " "
56
+ "#{title}#{colon}#{whitespace}#{node.content}".strip
57
+ end
58
+
42
59
  end
43
60
 
44
61
  Lita.register_handler(WolframAlpha)
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |spec|
2
2
  spec.name = "lita-wolfram-alpha"
3
- spec.version = "0.1.1"
3
+ spec.version = "0.1.2"
4
4
  spec.authors = ["Tristan Chong"]
5
5
  spec.email = ["ong@tristaneuan.ch"]
6
6
  spec.description = "A Lita handler that performs Wolfram Alpha queries."
@@ -10,32 +10,46 @@ describe Lita::Handlers::WolframAlpha, lita_handler: true do
10
10
 
11
11
  describe "#query" do
12
12
 
13
- it "returns the primary pod's plaintext" do
13
+ it "returns the primary pod's title and plaintext" do
14
14
  send_command "wa pi"
15
15
  expect(replies.count).to eq 2
16
- expect(replies.first).to match(/^3\.14159/)
17
- expect(replies.last).to match(/^Learn more: http:\/\/www\.wolframalpha\.com\/input\/\?i=pi$/)
16
+ expect(replies.first).to match(/^Decimal approximation: 3\.14159/)
17
+ expect(replies.last).to match(/^http:\/\/www\.wolframalpha\.com\/input\/\?i=pi$/)
18
18
  end
19
19
 
20
- it "returns the first subpod containing plaintext when the primary pod has multiple subpods" do
20
+ it "returns the title and plaintext of the first subpod containing plaintext when the primary pod has multiple subpods, omitting the pod's title when it contains 'Result'" do
21
21
  send_command "wa sky chart"
22
22
  expect(replies.count).to eq 2
23
- expect(replies.first).to match(/^\d+\^h \d+\^m [\d\.]+\^s$/)
24
- expect(replies.last).to match(/^Learn more: http:\/\/www\.wolframalpha\.com\/input\/\?i=sky\+chart$/)
23
+ expect(replies.first).to match(/^Local sidereal time: \d+\^h \d+\^m [\d\.]+\^s$/)
24
+ expect(replies.last).to match(/^http:\/\/www\.wolframalpha\.com\/input\/\?i=sky\+chart$/)
25
25
  end
26
26
 
27
- it "returns the first non-input, non-empty pod's plaintext when no primary pod exists" do
27
+ it "returns the first non-input, non-empty pod's title and plaintext when no primary pod exists" do
28
28
  send_command "wa U+00F8"
29
29
  expect(replies.count).to eq 2
30
- expect(replies.first).to match(/^Latin small letter o with stroke/)
31
- expect(replies.last).to match(/^Learn more: http:\/\/www\.wolframalpha\.com\/input\/\?i=U%2B00F8$/)
30
+ expect(replies.first).to match(/^Name: Latin small letter o with stroke/)
31
+ expect(replies.last).to match(/^http:\/\/www\.wolframalpha\.com\/input\/\?i=U%2B00F8$/)
32
+ end
33
+
34
+ it "returns the concatenated pod and subpod titles when they exist, separating the titles from the answer with a newline when the answer contains multiple lines" do
35
+ send_command "wa weather forecast for san francisco"
36
+ expect(replies.count).to eq 2
37
+ expect(replies.first).to match(/^Weather forecast for San Francisco, California -> \w+:\nbetween/)
38
+ expect(replies.last).to match(/^http:\/\/www\.wolframalpha\.com\/input\/\?i=weather\+forecast\+for\+san\+francisco$/)
39
+ end
40
+
41
+ it "returns only the plaintext when the pod and subpod titles contain 'Result' or do not exist" do
42
+ send_command "wa warp speed"
43
+ expect(replies.count).to eq 2
44
+ expect(replies.first).to match(/^speed/)
45
+ expect(replies.last).to match(/^http:\/\/www\.wolframalpha\.com\/input\/\?i=warp\+speed$/)
32
46
  end
33
47
 
34
48
  it "does not return a link if hide_link is true" do
35
49
  registry.config.handlers.wolfram_alpha.hide_link = true
36
50
  send_command "wa pi"
37
51
  expect(replies.count).to eq 1
38
- expect(replies.first).to match(/^3\.14159/)
52
+ expect(replies.first).to match(/^Decimal approximation: 3\.14159/)
39
53
  end
40
54
 
41
55
  it "returns a shrug emoticon for an unrecognized query" do
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lita-wolfram-alpha
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tristan Chong
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-06-07 00:00:00.000000000 Z
11
+ date: 2015-06-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: lita