class NsdaQaSearch
Public Class Methods
search(keywords, options={})
click to toggle source
# File lib/nsda_qa_search.rb, line 4 def self.search(keywords, options={}) url = "http://api.stackexchange.com/2.2/search?site=stackoverflow" #parameters without default value - start if options['with-body'] url += "&filter=!9WgJfiTRL" end if keywords url += "&tagged=%s" % keywords end #end #parameters with default values - start url += "&page=%d" % options['page'] ||= 1 url += "&pagesize=%d" % options['pagesize'] ||= 5 url += "&order=%s" % options['order'] ||= :desc url += "&sort=%s" % options['sort'] ||= :creation #end result ||= "api url: %s\n" % url response = HTTParty.get(url) result += "-" * 80 + "\n" result += "-" * 80 + "\n" response['items'].each_with_index do |item, index| result += "### %s. %s\n" % [:id, item['question_id']] result += "### %d. %s\n" % [index+1, item['title']] result += "### %s: %d\n" % [:answers, item['answer_count']] result += "#" * 60 + "\n" result += "### %s: %s\n" % [:content, item['body']] if item['body'] result += "#" * 80 + "\n" result += "### Command for show answers: nsda_qa_search --show %s\n" % item['question_id'] result += "#" * 80 + "\n" result += "#" * 80 + "\n" result += "-" * 80 + "\n" result += "-" * 80 + "\n" end result end
show(id, action="all")
click to toggle source
# File lib/nsda_qa_search.rb, line 46 def self.show(id, action="all") url_answers = "http://api.stackexchange.com/2.2/questions/%s/answers?order=desc&sort=activity&site=stackoverflow&filter=!9WgJfjI5u" % id url_question = "http://api.stackexchange.com/2.2/questions/%s?order=desc&sort=creation&site=stackoverflow&filter=!9WgJfiTRL" % id response_answers = HTTParty.get(url_answers) response_question = HTTParty.get(url_question) result ||= "question api url: %s\n" % url_question result ||= "answer url : %s\n" % url_answers result += "-" * 80 + "\n" question = response_question['items'][0] result += "######%s. %s######\n" % [question['question_id'], question['title']] result += "+" * 80 + "\n" result += "+" * 80 + "\n" response_answers['items'].each_with_index do |item, index| result += "### %s. %s\n" % [:id, item['answer_id']] result += "### %s: %s\n" % [:is_accepted, item['is_accepted']] result += "#" * 80 + "\n" result += "###%s: %s\n" % [:content, item['body']] result += "#" * 80 + "\n" result += "-" * 80 + "\n" end result end