Skip to content

Instantly share code, notes, and snippets.

@tatat
Last active August 29, 2015 14:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tatat/855ff3fc395c75bcd840 to your computer and use it in GitHub Desktop.
Save tatat/855ff3fc395c75bcd840 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
require "json"
require "open-uri"
class LoveLiveQuiz
API_URL = 'https://lovelive-quiz-api.herokuapp.com/quiz'
def initialize
fetch
end
def type
@quiz['type']
end
def format_quiz
__send__ :"format_quiz_#{type}"
end
def print_quiz
STDOUT.print format_quiz
end
def format_answer
'答え: ' + __send__(:"format_answer_#{type}")
end
def print_answer
STDOUT.print format_answer
end
private
def format_quiz_choice
choices = @quiz['choices'].map {|c| c['text']}.map.with_index {|t, i| "#{i + 1}. #{t}"}.join ', '
"#{@quiz['body']}\n#{choices}"
end
def format_quiz_ranking
members = @quiz['choices'].map {|c| c['name'] }.join ', '
"#{@quiz['body']}\n#{members}"
end
def format_answer_choice
index = @quiz['choices'].index {|c| c['correct'] }
"#{index + 1}. #{@quiz['choices'][index]['text']}"
end
def format_answer_ranking
@quiz['choices'].sort_by {|c| c['rank'] }.map {|c| c['name'] }.join ', '
end
def fetch
@quiz = JSON.parse open(API_URL).read
@quiz['choices'].sort_by! { rand } if type == 'ranking'
end
end
quiz = LoveLiveQuiz.new
(1..5).to_a.reverse.each do |i|
system "clear"
quiz.print_quiz
puts
puts "[#{i}]"
sleep 1
end
system "clear"
quiz.print_quiz
puts
quiz.print_answer
puts
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment