-
Notifications
You must be signed in to change notification settings - Fork 0
Services #12
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Services #12
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
class PokemonsPresenter | ||
def initialize(pokemon) | ||
@pokemon = pokemon | ||
end | ||
|
||
def as_json | ||
{ | ||
name: @pokemon.nome, | ||
url: @pokemon.imagem | ||
} | ||
Comment on lines
+6
to
+10
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Aqui é como queremos mostrar a info, separei num presenter para separar a logica |
||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
class PokemonsApiRepository | ||
POKEMON_API = "https://pokeapi.co/api/v2/pokemon" | ||
|
||
attr_reader :url, :limit | ||
|
||
def initialize(limit = 10000) | ||
@url = POKEMON_API | ||
@limit = limit | ||
end | ||
|
||
def fetch_all_as_json | ||
response = HTTParty.get("#{url}?limit=#{limit}") | ||
if response.code == 200 | ||
response.body | ||
end | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
class PokemonsRepository | ||
require_relative "../presenters/pokemons_presenter" | ||
|
||
def initialize(model = Pokemon) | ||
@model = model | ||
@presenter = ::PokemonsPresenter | ||
end | ||
|
||
def fetch_all_as_json | ||
pokemons = all | ||
{ | ||
count: pokemons.count, | ||
next: nil, | ||
previous: nil, | ||
results: pokemons.map { |pokemon| @presenter.new(pokemon).as_json }, | ||
}.to_json | ||
end | ||
|
||
def all | ||
@model.all | ||
end | ||
|
||
def find_by_name(name) | ||
@model.find_by(nome: name) | ||
end | ||
|
||
def find_by_id(id) | ||
@model.find(id) | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
class PokemonsService | ||
|
||
def initialize(repository = PokemonsRepository.new) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Por padrão, será o PokemonsRepository (que procura na base), o controller está procurando o da API, isso é legal pra gente isolar a logica das barreiras de acesso a banco de dados, logo podemos mudar como acessamos os dados sem mudar a interface |
||
@repository = repository | ||
end | ||
|
||
def all_pokemons | ||
JSON.parse(@repository.fetch_all_as_json) | ||
end | ||
|
||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Isso aqui seria um exemplo, que teriamos 2 repos, um que procuraria na base, outro na api