Class: Okbot::CLI::Stack

Inherits:
Thor
  • Object
show all
Defined in:
lib/okbot/cli/stack.rb

Instance Method Summary collapse

Instance Method Details

#spin_it(fps = 10) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/okbot/cli/stack.rb', line 30

def spin_it(fps=10)
  chars = %w[⣾ ⣽ ⣻ ⢿ ⡿ ⣟ ⣯ ⣷]
  delay = 1.0/fps
  iter = 0
  spinner = Thread.new do
    while iter do  # Keep spinning until told otherwise
      print chars[(iter+=1) % chars.length]
      sleep delay
      print "\b"
    end
  end
  yield.tap{       # After yielding to the block, save the return value
    iter = false   # Tell the thread to exit, cleaning up after itself…
    spinner.join   # …and wait for it to do so.
  }                # Use the block's return value as the method's
end

#titleObject



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/okbot/cli/stack.rb', line 6

def title
  title = ask "Input text: "
  print "Doing your title..."
  spin_it{
    sleep rand(4)+2
  }
  
  url = "https://api.stackexchange.com/2.2/search/advanced?pagesize=5&order=desc&sort=activity&title=#{title}&site=stackoverflow&filter=!1zSk2G.tfS9G_M7ErPqm("
  uri = URI(url)
  response  = Net::HTTP.get(uri)
  output    = JSON.parse(response)
  rows = []

  output["items"].each_index do |i, t|
    rows << ['Tags', output["items"][i]["tags"]]
    rows << ['Title', output["items"][i]["title"]]
    rows << ['Is answered', output["items"][i]["is_answered"]]
    rows << ['Link', output["items"][i]["link"]]
    rows << :separator
  end
  puts table([nil, Rainbow("Result for: #{title}").red], *rows)
end