diff --git a/Gemfile b/Gemfile index 9f9acb1..91bce0e 100644 --- a/Gemfile +++ b/Gemfile @@ -35,7 +35,7 @@ gem 'omniauth-bnet', '~> 2.0.0' # This gem provides a mitigation against CVE-2015-9284 gem 'omniauth-rails_csrf_protection', '~> 0.1.2' # A Ruby wrapper around Blizzard's Game Data and Profile APIs -gem 'rbattlenet', '~> 2.2.5', git: 'https://github.com/Dainii/rbattlenet' +gem 'rbattlenet', '~> 2.2.6', git: 'https://github.com/Dainii/rbattlenet' # A gem that provides Rails integration for the Sentry error logger gem 'sentry-rails', '~> 4.4.0' gem 'sentry-ruby', '~> 4.4.1' diff --git a/Gemfile.lock b/Gemfile.lock index 303098a..d6e248c 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,8 +1,8 @@ GIT remote: https://github.com/Dainii/rbattlenet - revision: 0b430de01052dd66430c50cd391570ba81d6d699 + revision: 52c159e1418350247c2f928f08930e0881bc6827 specs: - rbattlenet (2.2.5) + rbattlenet (2.2.6) require_all typhoeus (~> 1.1) @@ -411,7 +411,7 @@ DEPENDENCIES rails (~> 6.1.3, >= 6.1.3.1) rails-erd rails-i18n (~> 6.0.0) - rbattlenet (~> 2.2.5)! + rbattlenet (~> 2.2.6)! redis (~> 4.2.5) rspec-rails rubocop diff --git a/app/models/wow_spell.rb b/app/models/wow_spell.rb new file mode 100644 index 0000000..2ee6c91 --- /dev/null +++ b/app/models/wow_spell.rb @@ -0,0 +1,7 @@ +class WowSpell < ApplicationRecord + extend Mobility + translates :name, :description + + validates :name, presence: true + validates :spell_id, presence: true, uniqueness: true +end diff --git a/app/workers/wow_spells_worker.rb b/app/workers/wow_spells_worker.rb new file mode 100644 index 0000000..e2ced5a --- /dev/null +++ b/app/workers/wow_spells_worker.rb @@ -0,0 +1,38 @@ +# frozen_string_literal: true + +class WowSpellsWorker < WowSidekiqWorker + def perform(spell_id, batch_size, keep_going_on) + RBattlenet.authenticate(client_id: ENV['BLIZZARD_API_CLIENT_ID'], client_secret: ENV['BLIZZARD_API_CLIENT_SECRET']) + RBattlenet.set_options(locale: 'all') + + params = { + _page: 1, + _pageSize: batch_size, + orderby: 'id', + filters: { id: "[#{spell_id},]" } + } + result = RBattlenet::Wow::Search::Spell.find(params) + + return unless result.status_code == 200 + + result.results&.each do |spell| + wow_spell = WowSpell.find_or_initialize_by(spell_id: spell.data.id) + + wow_spell.media_id = spell.data.media.id if spell.data.media.id + + # Localisation data + locales.each do |locale| + Mobility.with_locale(locale[0]) do + wow_spell.name = spell.data.name[locale[1]] + end + end + + wow_spell.save + end + + # Create a new job for the next batch if keep_going_on is true + return unless keep_going_on && !result.results.count.zero? + + WowSpellsWorker.perform_async(result.results.last.data.id + 1, batch_size, keep_going_on) + end +end diff --git a/db/migrate/20210530121125_create_wow_spells.rb b/db/migrate/20210530121125_create_wow_spells.rb new file mode 100644 index 0000000..dfef40c --- /dev/null +++ b/db/migrate/20210530121125_create_wow_spells.rb @@ -0,0 +1,14 @@ +class CreateWowSpells < ActiveRecord::Migration[6.1] + def change + create_table :wow_spells do |t| + t.integer :spell_id + t.jsonb :name + t.jsonb :description + t.integer :media_id + + t.timestamps + end + + add_index :wow_spells, :spell_id, unique: true + end +end diff --git a/db/schema.rb b/db/schema.rb index 107c5e5..ff5705d 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 2021_05_28_214602) do +ActiveRecord::Schema.define(version: 2021_05_30_121125) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -374,6 +374,16 @@ ActiveRecord::Schema.define(version: 2021_05_28_214602) do t.index ["wow_reputation_tier_id"], name: "index_wow_reputations_on_wow_reputation_tier_id" end + create_table "wow_spells", force: :cascade do |t| + t.integer "spell_id" + t.jsonb "name" + t.jsonb "description" + t.integer "media_id" + t.datetime "created_at", precision: 6, null: false + t.datetime "updated_at", precision: 6, null: false + t.index ["spell_id"], name: "index_wow_spells_on_spell_id", unique: true + end + create_table "wow_standings", force: :cascade do |t| t.bigint "wow_character_id" t.bigint "wow_reputation_id" diff --git a/spec/models/wow_spell_spec.rb b/spec/models/wow_spell_spec.rb new file mode 100644 index 0000000..3bc508a --- /dev/null +++ b/spec/models/wow_spell_spec.rb @@ -0,0 +1,5 @@ +require 'rails_helper' + +RSpec.describe WowSpell, type: :model do + pending "add some examples to (or delete) #{__FILE__}" +end