Class: PokeAPI::Requester

Inherits:
Object
  • Object
show all
Defined in:
lib/pokeapi/requester.rb

Constant Summary collapse

NotFoundError =
Class.new(StandardError)
TimeoutError =
Class.new(StandardError)
APIError =
Class.new(StandardError)
API_ENDPOINT =
"https://pokeapi.co/api/v2/"
POKEMON_ENDPOINT =
"pokemon/"
POKEMON_SPECIES_ENDPOINT =
"pokemon-species/"
EVOLUTION_CHAIN_ENDPOINT =
"evolution-chain/"

Class Method Summary collapse

Class Method Details

.evolution_chain(id) ⇒ Object



25
26
27
28
# File 'lib/pokeapi/requester.rb', line 25

def evolution_chain(id)
  data = get("#{EVOLUTION_CHAIN_ENDPOINT}#{id}")
  Parser::EvolutionChain.parse(data)
end

.get(path) ⇒ Object



30
31
32
33
34
35
36
# File 'lib/pokeapi/requester.rb', line 30

def get(path)
  http = HTTP.get("#{API_ENDPOINT}#{path}/")
  fail NotFoundError if http.code == 404
  fail TimeoutError if http.code == 504
  fail APIError if http.code == 500
  JSON.parse(http.to_s, symbolize_names: true)
end

.pokemon(id) ⇒ Object



15
16
17
18
# File 'lib/pokeapi/requester.rb', line 15

def pokemon(id)
  data = get("#{POKEMON_ENDPOINT}#{id}")
  Parser::Pokemon.parse(data)
end

.species(id) ⇒ Object



20
21
22
23
# File 'lib/pokeapi/requester.rb', line 20

def species(id)
  data = get("#{POKEMON_SPECIES_ENDPOINT}#{id}")
  Parser::Species.parse(data)
end