-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature: rake task to seed/update pokemons
alongside with sidekiq
- Loading branch information
1 parent
9bb59a1
commit 08e64ae
Showing
6 changed files
with
76 additions
and
15 deletions.
There are no files selected for viewing
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
class HardWorker | ||
include Sidekiq::Worker | ||
|
||
def perform id | ||
data = Pokeapi::Pokemon.get id | ||
|
||
created_pokemon = Pokemon.find_or_create_by(name: data["name"]) do |p| | ||
p.base_experience = data["base_experience"] | ||
p.height = data["height"] | ||
p.is_default = data["is_default"] | ||
p.order = data["order"] | ||
p.weight = data["weight"] | ||
end | ||
|
||
data["types"].map do |item| | ||
type = Type.find_or_create_by(name: item["type"]["name"]) | ||
created_pokemon.pokemon_types.find_or_create_by(type_id: type.id) do |pt| | ||
pt.slot = item["slot"] | ||
end | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
class PokemonWorker | ||
include Sidekiq::Worker | ||
|
||
def perform(*args) | ||
# Do something | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
require 'sidekiq/web' | ||
|
||
Rails.application.routes.draw do | ||
# For details on the DSL available within this file, see https://guides.rubyonrails.org/routing.html | ||
mount Sidekiq::Web => "/sidekiq" | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
require 'pokeapi' | ||
|
||
namespace :pokemons do | ||
task update: :environment do | ||
pokemons = Pokeapi::Pokemon.get_all | ||
|
||
pokemons.each do |pokemon| | ||
HardWorker.perform_async pokemon["url"].split("/").last | ||
end | ||
end | ||
end |