Last active
December 7, 2020 16:44
-
-
Save tomascco/712187561eaa2028d6ed15ec1fb10bb9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Pokecli::EntityFormatters | |
class Pokemon | |
Unslugify = ->name { name.tr('-', ' ').capitalize } | |
StatFormatter = ->stat { "#{stat[0].capitalize}: #{stat[1]}" } | |
def self.render(response) | |
new(response).render | |
end | |
def initialize(response) | |
learned_abilities = response[:abilities].map { |ability| ability.dig(:ability, :name) } | |
pokedex_number = response[:id] | |
base_stats = response[:stats].map { |stat| [stat.dig(:stat, :name), stat[:base_stat]] } | |
types = response[:types].map { |type| type.dig(:type, :name) } | |
@params = { | |
learned_abilities: learned_abilities, | |
pokedex_number: pokedex_number, | |
base_stats: base_stats, | |
types: types | |
} | |
end | |
def render | |
<<~POKEMON | |
Pokemon: #{Unslugify.call(@params[:name])} (N° #{@params[:pokedex_number]}) | |
Types: #{@params[:types].map(&:capitalize).join('/')} | |
Abilities: #{@params[:learned_abilities].map(&Unslugify).join(', ')} | |
Base stats: | |
#{@params[:base_stats].map(&StatFormatter).join("\n")} | |
POKEMON | |
end | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Pokecli::EntityFormatters | |
Pokemon = ->response do | |
params = GetPokemonParams[response] | |
<<~POKEMON | |
Pokemon: #{Unslugify.call(params[:name])} (N° #{params[:pokedex_number]}) | |
Types: #{params[:types].map(&:capitalize).join('/')} | |
Abilities: #{params[:learned_abilities].map(&Unslugify).join(', ')} | |
Base stats: | |
#{params[:base_stats].map(&StatFormatter).join("\n")} | |
POKEMON | |
end | |
GetPokemonParams = ->response do | |
learned_abilities = response[:abilities].map { |ability| ability.dig(:ability, :name) } | |
pokedex_number = response[:id] | |
base_stats = response[:stats].map { |stat| [stat.dig(:stat, :name), stat[:base_stat]] } | |
types = response[:types].map { |type| type.dig(:type, :name) } | |
{ | |
learned_abilities: learned_abilities, | |
pokedex_number: pokedex_number, | |
base_stats: base_stats, | |
types: types | |
} | |
end | |
Unslugify = ->name { name.tr('-', ' ').capitalize } | |
StatFormatter = ->stat { "#{stat[0].capitalize}: #{stat[1]}" } | |
private_constant :GetPokemonParams, :Unslugify, :StatFormatter | |
end |
mrbongiolo
commented
Nov 27, 2020
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment