PokeAPI

Crystal wrapper library for PokéAPI.

Installation

  1. Add the dependency to your shard.yml:

    dependencies:
      pokeapi:
        github: henrikac/pokeapi
  2. Run shards install

Usage

Get a list of resources

To get a list of resources from any of the endpoints specified below use

.resource(endpoint : String, limit : UInt32 = 20, offset : UInt32 = 0)

require "pokeapi"

# returns a list of the first 20 pokémons
pokemons = PokeAPI.resource("pokemon")

# returns a list of 8 pokémons starting from #90 (if possible)
pokemons = PokeAPI.resource("pokemon", limit: 8, offset: 90)

Get a single item

Each of the endpoints has a corresponding method that can be used to get data from that specific endpoint, e.g.

/pokemon/{id or name}/
require "pokeapi"

pokemon_id = PokeAPI.pokemon(25) # => returns pokémon with id == 25
pokemon_name = PokeAPI.pokemon("eevee") # => returns pokémon with name eevee
/growth-rate/{id or name}/
require "pokeapi"

growth_rate = PokeAPI.growth_rate(2) # => returns growth-rate with id == 2

Cache

Data retrieved from PokéAPI is automaticly cached in-memory. However, the cache can be turned on/off manually if needed.

require "pokeapi"

PokeAPI::CACHE.enable
PokeAPI::CACHE.disable

It is also possible to clear the cache if needed.

require "pokeapi"

PokeAPI::CACHE.clear

The default time data is cached is set to 30 minutes.

require "pokeapi"

PokeAPI::CACHE.set_cache_time(45.mintues)

Errors

If no resource was found a PokeAPI::NoResourceError is raised.

require "pokeapi"

begin
  berry = PokeAPI.berry("weird-unknown-berry")
rescue ex : PokeAPI::NoResourceError
  puts ex.status_code # => 404
  puts ex.status_message # => Not Found
  puts ex.message # => 404 - Not Found
end

Endpoints

Berries

Contests

Encounters

Evolution

Games

Items

Locations

Machines

Moves

Pokémon

Contributing

  1. Fork it (https://github.com/henrikac/pokeapi/fork)
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create a new Pull Request

Contributors