RareCandy.Api (rare_candy v0.2.0)

View Source

Interfaces with PokeApi (https://pokeapi.co/) to fetch info about Pokemon.

Liberties were taken to extract json data from PokeApi into a cleaner, more accessible struct format:

iex(1)> RareCandy.Api.get_pokemon_by_id(250)
{:ok,
  %Pokemon{
    abilities: ["pressure", "regenerator"],
    forms: ["ho-oh"],
    height: 38,
    id: 250,
    img: "https://raw.githubusercontent.com/PokeAPI/sprites/master/sprites/pokemon/other/official-artwork/250.png",
    moves: ["gust", "whirlwind", "fly", "double-edge", "roar", "flamethrower",
      "hyper-beam", "strength", "solar-beam", "thunderbolt", "thunder-wave",
      "thunder", "earthquake", "toxic", "psychic", "mimic", "double-team",
      "recover", "light-screen", "reflect", "fire-blast", "swift", "dream-eater",
      "sky-attack", "flash", "rest", "substitute", "nightmare", "snore", "curse",
      "protect", "mud-slap", "zap-cannon", "detect", "sandstorm", "giga-drain",
      "endure", "swagger", "steel-wing", "sleep-talk", "return", "frustration",
      "safeguard", ...],
    name: "ho-oh",
    stats: %{
      "attack" => 130,
      "defense" => 90,
      "hp" => 106,
      "special-attack" => 110,
      "special-defense" => 154,
      "speed" => 90
    },
    types: ["fire", "flying"],
    weight: 1990
    }
  }

Examples

  {_, pkmn} = RareCandy.Api.get_pokemon_by_id(510)
  # {:ok,
  #   %Pokemon{
  #     abilities: ["limber", "unburden", "prankster"],
  #     forms: ["liepard"],
  #     height: 11,
  #     id: 510
  #   ...
  IO.puts(String.capitalize(pkmn.name) <> " was caught!")
  # Liepard was caught!
  # :ok

  RareCandy.Api.find_pokemon("fossrass")
  # {:ok,
  #  %Pokemon{
  #    abilities: ["snow-cloak", "cursed-body"],
  #    forms: ["froslass"],
  #    height: 13,
  #    id: 478,
  #  ...

Summary

Functions

Will return the Pokemon struct that's name is nearest to the string input query.

Fetches data from PokeApi using the desired Pokemon's dex no as an argument.

Types

json()

@type json() :: String.t()

pokemon()

@type pokemon() :: Pokemon.t()

Functions

find_pokemon(query)

@spec find_pokemon(any()) :: {:ok, pokemon()} | {:error, any()}

Will return the Pokemon struct that's name is nearest to the string input query.

Utilizes String.jaro_distance.

get_pokemon_by_id(id)

@spec get_pokemon_by_id(integer()) :: {:ok, pokemon()} | {:error, any()}

Fetches data from PokeApi using the desired Pokemon's dex no as an argument.