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/"
TYPES_ENDPOINT =
"type/"

Class Method Summary collapse

Class Method Details

.evolution_chain(id) ⇒ Object



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

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

.get(path) ⇒ Object



36
37
38
39
40
41
42
# File 'lib/pokeapi/requester.rb', line 36

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



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

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

.species(id) ⇒ Object



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

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

.type(id) ⇒ Object



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

def type(id)
  data = get("#{TYPES_ENDPOINT}#{id}")
  Parser::Type.parse(data)
end