Class: ApiManager

Inherits:
Object
  • Object
show all
Defined in:
lib/cli_trivia/apimanager.rb

Overview

Handles the external API call to the “Open Trivia Database” to get trivia question hashes

Constant Summary collapse

BASE_URL =

base api url

'https://opentdb.com/api.php?amount=10&token='

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeApiManager

Returns a new instance of ApiManager.



8
9
10
11
# File 'lib/cli_trivia/apimanager.rb', line 8

def initialize
  @categories = nil
  @token = nil
end

Class Method Details

.create_questions(response) ⇒ Object



36
37
38
39
40
# File 'lib/cli_trivia/apimanager.rb', line 36

def self.create_questions(response)
  response['results'].each do |question|
    Question.new(question)
  end
end

.generate_categoriesObject



18
19
20
21
22
23
# 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) ⇒ Object



30
31
32
33
34
# 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_questionsObject



25
26
27
28
# File 'lib/cli_trivia/apimanager.rb', line 25

def self.generate_random_questions
  response = HTTParty.get("#{BASE_URL}#{@token}")
  create_questions(response)
end

.generate_tokenObject



13
14
15
16
# 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