Skip to content

Commit

Permalink
feature: rake task to seed/update pokemons
Browse files Browse the repository at this point in the history
alongside with sidekiq
  • Loading branch information
AdrienGiboire committed Oct 13, 2021
1 parent 9bb59a1 commit 08e64ae
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 15 deletions.
19 changes: 5 additions & 14 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,14 @@ git_source(:github) { |repo| "https://github.com/#{repo}.git" }

ruby '3.0.2'

# Bundle edge Rails instead: gem 'rails', github: 'rails/rails', branch: 'main'
gem 'rails', '~> 6.1.4', '>= 6.1.4.1'
# Use sqlite3 as the database for Active Record
gem 'sqlite3', '~> 1.4'
# Use Puma as the app server
gem 'puma', '~> 5.0'
# Use SCSS for stylesheets
gem 'sass-rails', '>= 6'
# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
gem 'jbuilder', '~> 2.7'
# Use Redis adapter to run Action Cable in production
# gem 'redis', '~> 4.0'
# Use Active Model has_secure_password
# gem 'bcrypt', '~> 3.1.7'

# Use Active Storage variant
# gem 'image_processing', '~> 1.2'

# Reduces boot times through caching; required in config/boot.rb
gem 'bootsnap', '>= 1.4.4', require: false
gem 'active_model_serializers', '~> 0.10.0'
gem 'sidekiq'

group :development, :test do
# Call 'byebug' anywhere in the code to stop execution and get a debugger console
Expand All @@ -46,6 +34,9 @@ group :test do
gem 'selenium-webdriver'
# Easy installation and use of web drivers to run system tests with browsers
gem 'webdrivers'
gem 'rexml' # necessary if using ruby 3+
gem 'webmock'
gem 'vcr'
end

# Windows does not include zoneinfo files, so bundle the tzinfo-data gem
Expand Down
28 changes: 28 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ GEM
erubi (~> 1.4)
rails-dom-testing (~> 2.0)
rails-html-sanitizer (~> 1.1, >= 1.2.0)
active_model_serializers (0.10.12)
actionpack (>= 4.1, < 6.2)
activemodel (>= 4.1, < 6.2)
case_transform (>= 0.2)
jsonapi-renderer (>= 0.1.1.beta1, < 0.3)
activejob (6.1.4.1)
activesupport (= 6.1.4.1)
globalid (>= 0.3.6)
Expand Down Expand Up @@ -75,17 +80,24 @@ GEM
rack-test (>= 0.6.3)
regexp_parser (>= 1.5, < 3.0)
xpath (~> 3.2)
case_transform (0.2)
activesupport
childprocess (3.0.0)
concurrent-ruby (1.1.9)
connection_pool (2.2.5)
crack (0.4.5)
rexml
crass (1.0.6)
erubi (1.10.0)
ffi (1.15.4)
globalid (0.5.2)
activesupport (>= 5.0)
hashdiff (1.0.1)
i18n (1.8.10)
concurrent-ruby (~> 1.0)
jbuilder (2.11.2)
activesupport (>= 5.0.0)
jsonapi-renderer (0.2.2)
listen (3.7.0)
rb-fsevent (~> 0.10, >= 0.10.3)
rb-inotify (~> 0.9, >= 0.9.10)
Expand Down Expand Up @@ -141,7 +153,9 @@ GEM
rb-fsevent (0.11.0)
rb-inotify (0.10.1)
ffi (~> 1.0)
redis (4.4.0)
regexp_parser (2.1.1)
rexml (3.2.5)
rubyzip (2.3.2)
sass-rails (6.0.0)
sassc-rails (~> 2.1, >= 2.1.1)
Expand All @@ -156,6 +170,10 @@ GEM
selenium-webdriver (3.142.7)
childprocess (>= 0.5, < 4.0)
rubyzip (>= 1.2.2)
sidekiq (6.2.2)
connection_pool (>= 2.2.2)
rack (~> 2.0)
redis (>= 4.2.0)
spring (3.0.0)
sprockets (4.0.2)
concurrent-ruby (~> 1.0)
Expand All @@ -169,6 +187,7 @@ GEM
tilt (2.0.10)
tzinfo (2.0.4)
concurrent-ruby (~> 1.0)
vcr (6.0.0)
web-console (4.1.0)
actionview (>= 6.0.0)
activemodel (>= 6.0.0)
Expand All @@ -178,6 +197,10 @@ GEM
nokogiri (~> 1.6)
rubyzip (>= 1.3.0)
selenium-webdriver (>= 3.0, < 4.0)
webmock (3.14.0)
addressable (>= 2.8.0)
crack (>= 0.3.2)
hashdiff (>= 0.4.0, < 2.0.0)
websocket-driver (0.7.5)
websocket-extensions (>= 0.1.0)
websocket-extensions (0.1.5)
Expand All @@ -189,6 +212,7 @@ PLATFORMS
x86_64-linux

DEPENDENCIES
active_model_serializers (~> 0.10.0)
bootsnap (>= 1.4.4)
byebug
capybara (>= 3.26)
Expand All @@ -197,13 +221,17 @@ DEPENDENCIES
puma (~> 5.0)
rack-mini-profiler (~> 2.0)
rails (~> 6.1.4, >= 6.1.4.1)
rexml
sass-rails (>= 6)
selenium-webdriver
sidekiq
spring
sqlite3 (~> 1.4)
tzinfo-data
vcr
web-console (>= 4.1.0)
webdrivers
webmock

RUBY VERSION
ruby 3.0.2p107
Expand Down
22 changes: 22 additions & 0 deletions app/workers/hard_worker.rb
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
7 changes: 7 additions & 0 deletions app/workers/pokemon_worker.rb
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
4 changes: 3 additions & 1 deletion config/routes.rb
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
11 changes: 11 additions & 0 deletions lib/tasks/pokemons/update.rake
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

0 comments on commit 08e64ae

Please sign in to comment.