30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
|
# File 'lib/googem/helper.rb', line 30
def self.show_question question_id
url = "https://api.stackexchange.com/2.2/questions/#{question_id}?pagesize=10&order=desc&sort=activity&site=stackoverflow&filter=!)5s.rMpmn9TU*hMdF1gjTFCFqR0v"
spinner = TTY::Spinner.new("[:spinner] Getting data #{question_id} ... ", format: :pulse_2)
prompt = -> (page_num) { output.puts "Page -#{page_num}- Press enter to continue" }
= TTY::Pager::BasicPager.new
spinner.auto_spin
response = Unirest.get url, headers:{ "Accept" => "application/json" }
document = JSON.parse response.raw_body
content = "Q: #{document["items"][0]["title"]}\nA: #{document["items"][0]["link"]}\n";
content << "# QUESTION\n----------------------------------------------------------\n"
content << "#{CGI.unescapeHTML(document["items"][0]["body_markdown"])}\n\n"
content << "# ANSWERS\n----------------------------------------------------------\n"
if document["items"][0]["answers"].nil?
content << "No answers\n"
else
document["items"][0]["answers"].each_with_index do |i, index|
content << "▷ #{index+ 1} ◇ by #{i["owner"]["display_name"]} ◇ #{minutes_in_words Time.at(i["creation_date"])}\n"
content << "▷ #{i["link"]}\n"
content << "----------------------------------------------------------\n"
content << CGI.unescapeHTML(i["body_markdown"]) + "\n----------------------------------------------------------\n"
end
end
spinner.success('Done!') .page content.encode('utf-8')
end
|