class ApiManager
Handles the external API call to the “Open Trivia Database” to get trivia question hashes
Constants
- BASE_URL
base api url
Public Class Methods
create_questions(response)
click to toggle source
# File lib/cli_trivia/apimanager.rb, line 36 def self.create_questions(response) response['results'].each do |question| Question.new(question) end end
generate_categories()
click to toggle source
# File lib/cli_trivia/apimanager.rb, line 18 def self.generate_categories @categories = HTTParty.get('https://opentdb.com/api_category.php') @categories['trivia_categories'].each do |category| Category.new(category['name'], category['id']) end end
generate_questions_by_id(category_id)
click to toggle source
# File lib/cli_trivia/apimanager.rb, line 30 def self.generate_questions_by_id(category_id) category_url = "&category=#{category_id}" response = HTTParty.get("#{BASE_URL}#{@token}#{category_url}") create_questions(response) end
generate_random_questions()
click to toggle source
# File lib/cli_trivia/apimanager.rb, line 25 def self.generate_random_questions response = HTTParty.get("#{BASE_URL}#{@token}") create_questions(response) end
generate_token()
click to toggle source
# File lib/cli_trivia/apimanager.rb, line 13 def self.generate_token @token = HTTParty.get('https://opentdb.com/api_token.php?command=request10&token=') @token end
new()
click to toggle source
# File lib/cli_trivia/apimanager.rb, line 8 def initialize @categories = nil @token = nil end