nsda_qa_search 0.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. data/bin/nsda_qa_search +60 -0
  2. data/lib/nsda_qa_search.rb +73 -0
  3. metadata +65 -0
@@ -0,0 +1,60 @@
1
+ #!/usr/bin/env ruby
2
+ require 'nsda_qa_search'
3
+ require 'optparse'
4
+
5
+ options = {
6
+
7
+ }
8
+
9
+ parser = OptionParser.new do|opts|
10
+ opts.banner = "You need to provide an action."
11
+ opts.on('-s', '--search keywords', 'Search questions by keywords') do |search|
12
+ options['search'] = search;
13
+ end
14
+
15
+ opts.on('-w', '--with-body', 'Return results with content') do
16
+ options['with-body'] = true;
17
+ end
18
+
19
+ opts.on('-p', '--page PAGE', 'Page number') do |page|
20
+ options['page'] = page;
21
+ end
22
+
23
+ opts.on('-x', '--page-size PAGE-SIZE', 'Page size') do |pagesize|
24
+ options['pagesize'] = pagesize;
25
+ end
26
+
27
+ opts.on('-o', '--order TYPE', 'Result ordering type, asc or desc') do |order|
28
+ options['order'] = order;
29
+ end
30
+
31
+ opts.on('-t', '--sort TYPE', 'Sorting the results by types, activity, creation (default), votes, hot, week, and month') do |sort|
32
+ options['sort'] = sort
33
+ end
34
+
35
+ opts.on('-r', '--show ID', 'Show answers by question ID') do |show|
36
+ options['show'] = show
37
+ end
38
+
39
+ opts.on('-h', '--help', 'Displays Help') do
40
+ puts opts
41
+ exit
42
+ end
43
+ end
44
+
45
+ begin
46
+ parser.parse!
47
+ rescue OptionParser::InvalidOption
48
+ puts parser
49
+ exit
50
+ end
51
+
52
+ if options['search']
53
+ puts "Your search keywords: %s" % options['search']
54
+ puts NsdaQaSearch.search(options['search'], options)
55
+ elsif options['show']
56
+ puts "Your question ID: %s" % options['show']
57
+ puts NsdaQaSearch.show(options['show'])
58
+ else
59
+ puts parser
60
+ end
@@ -0,0 +1,73 @@
1
+ require 'httparty'
2
+
3
+ class NsdaQaSearch
4
+ def self.search(keywords, options={})
5
+ url = "http://api.stackexchange.com/2.2/search?site=stackoverflow"
6
+
7
+ #parameters without default value - start
8
+ if options['with-body']
9
+ url += "&filter=!9WgJfiTRL"
10
+ end
11
+
12
+ if keywords
13
+ url += "&tagged=%s" % keywords
14
+ end
15
+ #end
16
+
17
+ #parameters with default values - start
18
+ url += "&page=%d" % options['page'] ||= 1
19
+ url += "&pagesize=%d" % options['pagesize'] ||= 5
20
+ url += "&order=%s" % options['order'] ||= :desc
21
+ url += "&sort=%s" % options['sort'] ||= :creation
22
+ #end
23
+
24
+ result ||= "api url: %s\n" % url
25
+ response = HTTParty.get(url)
26
+
27
+ result += "-" * 80 + "\n"
28
+ result += "-" * 80 + "\n"
29
+ response['items'].each_with_index do |item, index|
30
+ result += "### %s. %s\n" % [:id, item['question_id']]
31
+ result += "### %d. %s\n" % [index+1, item['title']]
32
+ result += "### %s: %d\n" % [:answers, item['answer_count']]
33
+ result += "#" * 60 + "\n"
34
+ result += "### %s: %s\n" % [:content, item['body']] if item['body']
35
+ result += "#" * 80 + "\n"
36
+ result += "### Command for show answers: nsda_qa_search --show %s\n" % item['question_id']
37
+ result += "#" * 80 + "\n"
38
+ result += "#" * 80 + "\n"
39
+ result += "-" * 80 + "\n"
40
+ result += "-" * 80 + "\n"
41
+ end
42
+
43
+ result
44
+ end
45
+
46
+ def self.show(id, action="all")
47
+ url_answers = "http://api.stackexchange.com/2.2/questions/%s/answers?order=desc&sort=activity&site=stackoverflow&filter=!9WgJfjI5u" % id
48
+ url_question = "http://api.stackexchange.com/2.2/questions/%s?order=desc&sort=creation&site=stackoverflow&filter=!9WgJfiTRL" % id
49
+
50
+ response_answers = HTTParty.get(url_answers)
51
+ response_question = HTTParty.get(url_question)
52
+
53
+ result ||= "question api url: %s\n" % url_question
54
+ result ||= "answer url : %s\n" % url_answers
55
+ result += "-" * 80 + "\n"
56
+
57
+ question = response_question['items'][0]
58
+ result += "######%s. %s######\n" % [question['question_id'], question['title']]
59
+ result += "+" * 80 + "\n"
60
+ result += "+" * 80 + "\n"
61
+ response_answers['items'].each_with_index do |item, index|
62
+ result += "### %s. %s\n" % [:id, item['answer_id']]
63
+ result += "### %s: %s\n" % [:is_accepted, item['is_accepted']]
64
+ result += "#" * 80 + "\n"
65
+ result += "###%s: %s\n" % [:content, item['body']]
66
+ result += "#" * 80 + "\n"
67
+ result += "-" * 80 + "\n"
68
+ end
69
+
70
+ result
71
+ end
72
+ end
73
+
metadata ADDED
@@ -0,0 +1,65 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: nsda_qa_search
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Lim Chee Hau
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2014-03-03 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: httparty
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ description: This application allow users to search questions and answers in console
31
+ mode, no more browser needed!
32
+ email: ch33hau@gmail.com
33
+ executables:
34
+ - nsda_qa_search
35
+ extensions: []
36
+ extra_rdoc_files: []
37
+ files:
38
+ - lib/nsda_qa_search.rb
39
+ - bin/nsda_qa_search
40
+ homepage: http://rubygems.org/gems/nsda_qa_search
41
+ licenses:
42
+ - MIT
43
+ post_install_message:
44
+ rdoc_options: []
45
+ require_paths:
46
+ - lib
47
+ required_ruby_version: !ruby/object:Gem::Requirement
48
+ none: false
49
+ requirements:
50
+ - - ! '>='
51
+ - !ruby/object:Gem::Version
52
+ version: '0'
53
+ required_rubygems_version: !ruby/object:Gem::Requirement
54
+ none: false
55
+ requirements:
56
+ - - ! '>='
57
+ - !ruby/object:Gem::Version
58
+ version: '0'
59
+ requirements: []
60
+ rubyforge_project:
61
+ rubygems_version: 1.8.24
62
+ signing_key:
63
+ specification_version: 3
64
+ summary: Search questions and answers in console mode!
65
+ test_files: []