lita-howdoi 0.0.2 → 0.0.3

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: c56116b7c526963d01fc92219714341484088e75
4
- data.tar.gz: 0fd161f94ddc87c3b2e371682924b3cd50a07990
3
+ metadata.gz: 08a51a35b101b8ad418f41c7cebd29ea93ca220e
4
+ data.tar.gz: e90cb47de247a6de3eb64908cf5c9e60b248c46d
5
5
  SHA512:
6
- metadata.gz: fec1243b2181645292bc9a453e7fe80783b1369b4107277804f0a438e09ec22251576f90a249ff215b42689158711d0bef7c5ea08df596dea7f329a124eb32cf
7
- data.tar.gz: 6dfd96b880d6137c127c0b353d384b3806f768e7cf360e3d6a5dce023b44225367a31d0b2f453d2b49c1e4ce536dfeea85f12d51703138149cef90a02afd8eae
6
+ metadata.gz: 67d72065746769a71e5a7f804dbb4ccaa677d06dce1d0bbd31d6c8fc389555a6b1182100e87e343c4d811f81467938018bd3f1c0408e7b9c07d72b40f055913e
7
+ data.tar.gz: 15d61085c194105ac9ba0e810b700656c6aa4d8d14436c524646caadefc989999ce7c88b4104400eb84385e7100eb9c3e839681a8090d27b2fbcf22fe74f8ebf
@@ -3,60 +3,36 @@ require 'nokogiri'
3
3
  module Lita
4
4
  module Handlers
5
5
  class Howdoi < Handler
6
- GOOGLE_SEARCH_URL = "http://stackoverflow.com/search"
6
+ SEARCH_URL = "http://stackoverflow.com/search"
7
7
 
8
8
  route(/^howdoi (.*)$/, :howdoi, command: true, help: {
9
9
  "howdoi QUERY" => "Searches Stack Overflow for the first answer it finds with a solution to QUERY.",
10
10
  })
11
11
 
12
- def get_url(url, q="")
13
- http.get(url, q: q).body
14
- end
15
-
16
- def is_question?(stackoverflow_url)
17
- stackoverflow_url =~ %r(/questions/\d+/)
18
- end
19
-
20
- def get_google_links(args)
21
- page = get_url(GOOGLE_SEARCH_URL, args)
12
+ def search_stack_overflow_for(search_query)
13
+ page = http.get(SEARCH_URL, q: search_query).body
22
14
  html = Nokogiri.HTML(page)
23
- posts = []
24
- html.css('.result-link a').each do |link|
25
- posts << "http://stackoverflow.com#{link[:href]}"
26
- end
27
- posts
28
- end
29
-
30
- def get_link_at_pos(links, pos)
31
- link = nil
32
- pos.times do |i|
33
- break if i > links.size
34
- link = links[i]
35
- end
36
- link
15
+ html.css('.result-link a').map { |link| "http://stackoverflow.com#{link[:href]}" }
37
16
  end
38
17
 
39
18
  def howdoi(chat)
40
- args = chat.matches[0].last.split.join("+")
41
- links = get_google_links(args)
42
- link = get_link_at_pos(links, 1)
19
+ search_query = chat.matches[0].last.split.join("+")
20
+ first_result = search_stack_overflow_for(search_query).first
43
21
 
44
- if link
45
- page = get_url link
46
- html = Nokogiri.HTML(page)
47
- ans = html.at_css(".answer")
22
+ if first_result
23
+ html = Nokogiri.HTML(http.get(first_result).body)
24
+ answer = html.at_css(".answer")
48
25
 
49
- if ans
50
- instruction = ans.css("pre").children.
26
+ if answer
27
+ instruction = answer.css("pre").children.
51
28
  collect(&:content).
52
29
  join(" " * 5 + '-' * 50 + "\n") ||
53
- ans.at_css("code").content
30
+ answer.at_css("code").content
54
31
 
55
- unless instruction.empty?
32
+ if !instruction.empty?
56
33
  chat.reply "```\n#{instruction}```"
57
34
  else
58
- p ans.at_css('.post-text').content
59
- chat.reply "```\n#{ans.at_css('.post-text').content}```"
35
+ chat.reply "```\n#{answer.at_css('.post-text').content}```"
60
36
  end
61
37
  end
62
38
  else
data/lita-howdoi.gemspec CHANGED
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |spec|
2
2
  spec.name = "lita-howdoi"
3
- spec.version = "0.0.2"
3
+ spec.version = "0.0.3"
4
4
  spec.authors = ["Taylor Lapeyre"]
5
5
  spec.email = ["taylorlapeyre@gmail.com"]
6
6
  spec.description = "Crawls Stack Overflow for the first answer it finds with a solution to some query."
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lita-howdoi
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Taylor Lapeyre
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-03-04 00:00:00.000000000 Z
11
+ date: 2015-03-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: lita