Run RuboCop on everything

yolo
This commit is contained in:
Justin Edmund 2022-12-21 00:22:47 -08:00
parent 1fe1c1bb36
commit c1716c1e4f
45 changed files with 1251 additions and 1132 deletions

View file

@ -1,4 +1,7 @@
module Api::V1 # frozen_string_literal: true
module Api
module V1
class ApiController < ActionController::API class ApiController < ActionController::API
##### Doorkeeper ##### Doorkeeper
include Doorkeeper::Rails::Helpers include Doorkeeper::Rails::Helpers
@ -34,18 +37,18 @@ module Api::V1
def current_user def current_user
@current_user ||= User.find(doorkeeper_token.resource_owner_id) if doorkeeper_token @current_user ||= User.find(doorkeeper_token.resource_owner_id) if doorkeeper_token
return @current_user @current_user
end end
# Set the response content-type # Set the response content-type
def set_content_type(content_type) def set_content_type(content_type)
response.headers["Content-Type"] = content_type response.headers['Content-Type'] = content_type
end end
# Set the default response content-type to application/javascript # Set the default response content-type to application/javascript
# with a UTF-8 charset # with a UTF-8 charset
def set_default_content_type def set_default_content_type
set_content_type("application/javascript; charset=utf-8") set_content_type('application/javascript; charset=utf-8')
end end
### Error response methods ### Error response methods
@ -63,7 +66,7 @@ module Api::V1
end end
def render_not_found_response def render_not_found_response
response = { errors: [{ message: "Record could not be found.", code: "not_found" }] } response = { errors: [{ message: 'Record could not be found.', code: 'not_found' }] }
render 'not_found', status: :not_found render 'not_found', status: :not_found
end end
@ -77,4 +80,5 @@ module Api::V1
raise UnauthorizedError unless current_user raise UnauthorizedError unless current_user
end end
end end
end
end end

View file

@ -1,26 +1,25 @@
class Api::V1::FavoritesController < Api::V1::ApiController # frozen_string_literal: true
module Api
module V1
class FavoritesController < Api::V1::ApiController
before_action :set_party, only: ['create'] before_action :set_party, only: ['create']
def create def create
party_id = favorite_params[:party_id] party_id = favorite_params[:party_id]
party = Party.find(party_id) party = Party.find(party_id)
if !current_user raise Api::V1::UnauthorizedError unless current_user
raise Api::V1::UnauthorizedError raise Api::V1::SameFavoriteUserError if party.user && current_user.id == party.user.id
elsif party.user && current_user.id == party.user.id raise Api::V1::FavoriteAlreadyExistsError if Favorite.where(user_id: current_user.id,
raise Api::V1::SameFavoriteUserError party_id: party_id).length.positive?
elsif Favorite.where(user_id: current_user.id, party_id: party_id).length > 0
raise Api::V1::FavoriteAlreadyExistsError
else
object = {
user_id: current_user.id,
party_id: favorite_params[:party_id]
}
@favorite = Favorite.new(object) @favorite = Favorite.new({
user_id: current_user.id,
party_id: party_id
})
render :show, status: :created if @favorite.save! render :show, status: :created if @favorite.save!
end end
end
def destroy def destroy
raise Api::V1::UnauthorizedError unless current_user raise Api::V1::UnauthorizedError unless current_user
@ -32,10 +31,12 @@ class Api::V1::FavoritesController < Api::V1::ApiController
private private
def set_party def set_party
@party = Party.where("id = ?", params[:party_id]).first @party = Party.where('id = ?', params[:party_id]).first
end end
def favorite_params def favorite_params
params.require(:favorite).permit(:party_id) params.require(:favorite).permit(:party_id)
end end
end
end
end end

View file

@ -1,26 +1,26 @@
class Api::V1::GridCharactersController < Api::V1::ApiController # frozen_string_literal: true
module Api
module V1
class GridCharactersController < Api::V1::ApiController
def create def create
party = Party.find(character_params[:party_id]) party = Party.find(character_params[:party_id])
incoming_character = Character.find(character_params[:character_id]) incoming_character = Character.find(character_params[:character_id])
if current_user render_unauthorized_response if current_user && (party.user != current_user)
if party.user != current_user
render_unauthorized_response
end
end
current_characters = party.characters.map { |c| current_characters = party.characters.map do |c|
Character.find(c.character.id).character_id Character.find(c.character.id).character_id
}.flatten end.flatten
# Check all character ids on incoming character against current characters # Check all character ids on incoming character against current characters
conflict_ids = (current_characters & incoming_character.character_id) conflict_ids = (current_characters & incoming_character.character_id)
if conflict_ids.length > 0 if conflict_ids.length.positive?
# Find conflicting character ids in party characters # Find conflicting character ids in party characters
conflict_characters = party.characters.filter { |c| conflict_characters = party.characters.filter do |c|
c if (conflict_ids & c.character.character_id).length > 0 c if (conflict_ids & c.character.character_id).length.positive?
}.flatten end.flatten
# Render a template with the conflicting and incoming characters, # Render a template with the conflicting and incoming characters,
# as well as the selected position, so the user can be presented with # as well as the selected position, so the user can be presented with
@ -40,7 +40,8 @@ class Api::V1::GridCharactersController < Api::V1::ApiController
# Otherwise, create a new grid character # Otherwise, create a new grid character
else else
@character = GridCharacter.create!(character_params.merge(party_id: party.id, character_id: incoming_character.id)) @character = GridCharacter.create!(character_params.merge(party_id: party.id,
character_id: incoming_character.id))
end end
render :show, status: :created if @character.save! render :show, status: :created if @character.save!
@ -57,9 +58,9 @@ class Api::V1::GridCharactersController < Api::V1::ApiController
# Destroy the character at the desired position if it exists # Destroy the character at the desired position if it exists
existing_character = GridCharacter.where(party: party.id, position: resolve_params[:position]).first existing_character = GridCharacter.where(party: party.id, position: resolve_params[:position]).first
GridCharacter.destroy(existing_character.id) unless !existing_character GridCharacter.destroy(existing_character.id) if existing_character
if (incoming.special) if incoming.special
uncap_level = 3 uncap_level = 3
uncap_level = 5 if incoming.ulb uncap_level = 5 if incoming.ulb
uncap_level = 4 if incoming.flb uncap_level = 4 if incoming.flb
@ -69,34 +70,33 @@ class Api::V1::GridCharactersController < Api::V1::ApiController
uncap_level = 5 if incoming.flb uncap_level = 5 if incoming.flb
end end
@character = GridCharacter.create!(party_id: party.id, character_id: incoming.id, position: resolve_params[:position], uncap_level: uncap_level) @character = GridCharacter.create!(party_id: party.id, character_id: incoming.id,
position: resolve_params[:position], uncap_level: uncap_level)
render :show, status: :created if @character.save! render :show, status: :created if @character.save!
end end
def update_uncap_level def update_uncap_level
@character = GridCharacter.find(character_params[:id]) @character = GridCharacter.find(character_params[:id])
if current_user render_unauthorized_response if current_user && (@character.party.user != current_user)
if @character.party.user != current_user
render_unauthorized_response
end
end
@character.uncap_level = character_params[:uncap_level] @character.uncap_level = character_params[:uncap_level]
render :show, status: :ok if @character.save! render :show, status: :ok if @character.save!
end end
def destroy def destroy; end
end
private private
# Specify whitelisted properties that can be modified. # Specify whitelisted properties that can be modified.
def character_params def character_params
params.require(:character).permit(:id, :party_id, :character_id, :position, :uncap_level, :conflicting, :incoming) params.require(:character).permit(:id, :party_id, :character_id, :position, :uncap_level, :conflicting,
:incoming)
end end
def resolve_params def resolve_params
params.require(:resolve).permit(:position, :incoming, :conflicting => []) params.require(:resolve).permit(:position, :incoming, conflicting: [])
end
end
end end
end end

View file

@ -1,18 +1,18 @@
class Api::V1::GridSummonsController < Api::V1::ApiController # frozen_string_literal: true
module Api
module V1
class GridSummonsController < Api::V1::ApiController
def create def create
party = Party.find(summon_params[:party_id]) party = Party.find(summon_params[:party_id])
canonical_summon = Summon.find(summon_params[:summon_id]) canonical_summon = Summon.find(summon_params[:summon_id])
if current_user render_unauthorized_response if current_user && (party.user != current_user)
if party.user != current_user
render_unauthorized_response
end
end
if grid_summon = GridSummon.where( if (grid_summon = GridSummon.where(
party_id: party.id, party_id: party.id,
position: summon_params[:position] position: summon_params[:position]
).first ).first)
GridSummon.destroy(grid_summon.id) GridSummon.destroy(grid_summon.id)
end end
@ -23,18 +23,13 @@ class Api::V1::GridSummonsController < Api::V1::ApiController
def update_uncap_level def update_uncap_level
@summon = GridSummon.find(summon_params[:id]) @summon = GridSummon.find(summon_params[:id])
if current_user render_unauthorized_response if current_user && (@summon.party.user != current_user)
if @summon.party.user != current_user
render_unauthorized_response
end
end
@summon.uncap_level = summon_params[:uncap_level] @summon.uncap_level = summon_params[:uncap_level]
render :show, status: :ok if @summon.save! render :show, status: :ok if @summon.save!
end end
def destroy def destroy; end
end
private private
@ -42,4 +37,6 @@ class Api::V1::GridSummonsController < Api::V1::ApiController
def summon_params def summon_params
params.require(:summon).permit(:id, :party_id, :summon_id, :position, :main, :friend, :uncap_level) params.require(:summon).permit(:id, :party_id, :summon_id, :position, :main, :friend, :uncap_level)
end end
end
end
end end

View file

@ -1,15 +1,15 @@
class Api::V1::GridWeaponsController < Api::V1::ApiController # frozen_string_literal: true
before_action :set, except: ['create', 'update_uncap_level', 'destroy']
module Api
module V1
class GridWeaponsController < Api::V1::ApiController
before_action :set, except: %w[create update_uncap_level destroy]
def create def create
party = Party.find(weapon_params[:party_id]) party = Party.find(weapon_params[:party_id])
canonical_weapon = Weapon.find(weapon_params[:weapon_id]) canonical_weapon = Weapon.find(weapon_params[:weapon_id])
if current_user render_unauthorized_response if current_user && (party.user != current_user)
if party.user != current_user
render_unauthorized_response
end
end
if grid_weapon = GridWeapon.where( if grid_weapon = GridWeapon.where(
party_id: party.id, party_id: party.id,
@ -20,7 +20,7 @@ class Api::V1::GridWeaponsController < Api::V1::ApiController
@weapon = GridWeapon.create!(weapon_params.merge(party_id: party.id, weapon_id: canonical_weapon.id)) @weapon = GridWeapon.create!(weapon_params.merge(party_id: party.id, weapon_id: canonical_weapon.id))
if (@weapon.position == -1) if @weapon.position == -1
party.element = @weapon.weapon.element party.element = @weapon.weapon.element
party.save! party.save!
end end
@ -29,11 +29,9 @@ class Api::V1::GridWeaponsController < Api::V1::ApiController
end end
def update def update
if current_user render_unauthorized_response if current_user && (@weapon.party.user != current_user)
if @weapon.party.user != current_user
render_unauthorized_response ap weapon_params
end
end
# TODO: Server-side validation of weapon mods # TODO: Server-side validation of weapon mods
# We don't want someone modifying the JSON and adding # We don't want someone modifying the JSON and adding
@ -47,11 +45,7 @@ class Api::V1::GridWeaponsController < Api::V1::ApiController
def update_uncap_level def update_uncap_level
@weapon = GridWeapon.find(weapon_params[:id]) @weapon = GridWeapon.find(weapon_params[:id])
if current_user render_unauthorized_response if current_user && (party.user != current_user)
if party.user != current_user
render_unauthorized_response
end
end
@weapon.uncap_level = weapon_params[:uncap_level] @weapon.uncap_level = weapon_params[:uncap_level]
render :show, status: :ok if @weapon.save! render :show, status: :ok if @weapon.save!
@ -60,7 +54,7 @@ class Api::V1::GridWeaponsController < Api::V1::ApiController
private private
def set def set
@weapon = GridWeapon.where("id = ?", params[:id]).first @weapon = GridWeapon.where('id = ?', params[:id]).first
end end
# Specify whitelisted properties that can be modified. # Specify whitelisted properties that can be modified.
@ -72,4 +66,6 @@ class Api::V1::GridWeaponsController < Api::V1::ApiController
:ax_modifier1, :ax_modifier2, :ax_strength1, :ax_strength2 :ax_modifier1, :ax_modifier2, :ax_strength1, :ax_strength2
) )
end end
end
end
end end

View file

@ -1,6 +1,10 @@
class Api::V1::JobSkillsController < Api::V1::ApiController # frozen_string_literal: true
module Api
module V1
class JobSkillsController < Api::V1::ApiController
def all def all
@skills = JobSkill.all() @skills = JobSkill.all
render :all, status: :ok render :all, status: :ok
end end
@ -10,4 +14,6 @@ class Api::V1::JobSkillsController < Api::V1::ApiController
@skills = JobSkill.where(job: job).or(JobSkill.where(sub: true)) @skills = JobSkill.where(job: job).or(JobSkill.where(sub: true))
render :all, status: :ok render :all, status: :ok
end end
end
end
end end

View file

@ -1,8 +1,12 @@
class Api::V1::JobsController < Api::V1::ApiController # frozen_string_literal: true
module Api
module V1
class JobsController < Api::V1::ApiController
before_action :set, only: %w[update_job update_job_skills] before_action :set, only: %w[update_job update_job_skills]
def all def all
@jobs = Job.all() @jobs = Job.all
render :all, status: :ok render :all, status: :ok
end end
@ -45,7 +49,8 @@ class Api::V1::JobsController < Api::V1::ApiController
new_skill_ids = new_skill_keys.map { |key| job_params[key] } new_skill_ids = new_skill_keys.map { |key| job_params[key] }
new_skill_ids.map do |id| new_skill_ids.map do |id|
skill = JobSkill.find(id) skill = JobSkill.find(id)
raise Api::V1::IncompatibleSkillError.new(job: @party.job, skill: skill) if mismatched_skill(@party.job, skill) raise Api::V1::IncompatibleSkillError.new(job: @party.job, skill: skill) if mismatched_skill(@party.job,
skill)
end end
positions = extract_positions_from_keys(new_skill_keys) positions = extract_positions_from_keys(new_skill_keys)
@ -157,4 +162,6 @@ class Api::V1::JobsController < Api::V1::ApiController
:skill3_id :skill3_id
) )
end end
end
end
end end

View file

@ -1,4 +1,8 @@
class Api::V1::PartiesController < Api::V1::ApiController # frozen_string_literal: true
module Api
module V1
class PartiesController < Api::V1::ApiController
before_action :set_from_slug, before_action :set_from_slug,
except: %w[create destroy update index favorites] except: %w[create destroy update index favorites]
before_action :set, only: %w[update destroy] before_action :set, only: %w[update destroy]
@ -38,20 +42,22 @@ class Api::V1::PartiesController < Api::V1::ApiController
@per_page = 15 @per_page = 15
now = DateTime.current now = DateTime.current
unless request.params['recency'].blank?
start_time = start_time =
( (
now - request.params["recency"].to_i.seconds now - request.params['recency'].to_i.seconds
).to_datetime.beginning_of_day unless request.params["recency"].blank? ).to_datetime.beginning_of_day
end
conditions = {} conditions = {}
conditions[:element] = request.params["element"] unless request.params[ conditions[:element] = request.params['element'] unless request.params[
"element" 'element'
].blank? ].blank?
conditions[:raid] = request.params["raid"] unless request.params[ conditions[:raid] = request.params['raid'] unless request.params[
"raid" 'raid'
].blank? ].blank?
conditions[:created_at] = start_time..now unless request.params[ conditions[:created_at] = start_time..now unless request.params[
"recency" 'recency'
].blank? ].blank?
conditions[:weapons_count] = 5..13 conditions[:weapons_count] = 5..13
@ -75,20 +81,22 @@ class Api::V1::PartiesController < Api::V1::ApiController
@per_page = 15 @per_page = 15
now = DateTime.current now = DateTime.current
unless request.params['recency'].blank?
start_time = start_time =
( (
now - params["recency"].to_i.seconds now - params['recency'].to_i.seconds
).to_datetime.beginning_of_day unless request.params["recency"].blank? ).to_datetime.beginning_of_day
end
conditions = {} conditions = {}
conditions[:element] = request.params["element"] unless request.params[ conditions[:element] = request.params['element'] unless request.params[
"element" 'element'
].blank? ].blank?
conditions[:raid] = request.params["raid"] unless request.params[ conditions[:raid] = request.params['raid'] unless request.params[
"raid" 'raid'
].blank? ].blank?
conditions[:created_at] = start_time..now unless request.params[ conditions[:created_at] = start_time..now unless request.params[
"recency" 'recency'
].blank? ].blank?
conditions[:favorites] = { user_id: current_user.id } conditions[:favorites] = { user_id: current_user.id }
@ -96,7 +104,7 @@ class Api::V1::PartiesController < Api::V1::ApiController
Party Party
.joins(:favorites) .joins(:favorites)
.where(conditions) .where(conditions)
.order("favorites.created_at DESC") .order('favorites.created_at DESC')
.paginate(page: request.params[:page], per_page: @per_page) .paginate(page: request.params[:page], per_page: @per_page)
.each { |party| party.favorited = party.is_favorited(current_user) } .each { |party| party.favorited = party.is_favorited(current_user) }
@count = Party.joins(:favorites).where(conditions).count @count = Party.joins(:favorites).where(conditions).count
@ -130,19 +138,18 @@ class Api::V1::PartiesController < Api::V1::ApiController
private private
def random_string def random_string
numChars = 6 num_chars = 6
o = [("a".."z"), ("A".."Z"), (0..9)].map(&:to_a).flatten o = [('a'..'z'), ('A'..'Z'), (0..9)].map(&:to_a).flatten
return (0...numChars).map { o[rand(o.length)] }.join (0...num_chars).map { o[rand(o.length)] }.join
end end
def set_from_slug def set_from_slug
@party = Party.where("shortcode = ?", params[:id]).first @party = Party.where('shortcode = ?', params[:id]).first
@party.favorited = @party.favorited = current_user && @party ? @party.is_favorited(current_user) : false
current_user && @party ? @party.is_favorited(current_user) : false
end end
def set def set
@party = Party.where("id = ?", params[:id]).first @party = Party.where('id = ?', params[:id]).first
end end
def party_params def party_params
@ -159,4 +166,6 @@ class Api::V1::PartiesController < Api::V1::ApiController
:skill3_id :skill3_id
) )
end end
end
end
end end

View file

@ -1,6 +1,12 @@
class Api::V1::RaidsController < Api::V1::ApiController # frozen_string_literal: true
module Api
module V1
class RaidsController < Api::V1::ApiController
def all def all
@raids = Raid.all() @raids = Raid.all
render :all, status: :ok render :all, status: :ok
end end
end
end
end end

View file

@ -1,4 +1,8 @@
class Api::V1::SearchController < Api::V1::ApiController # frozen_string_literal: true
module Api
module V1
class SearchController < Api::V1::ApiController
def characters def characters
filters = search_params[:filters] filters = search_params[:filters]
locale = search_params[:locale] || 'en' locale = search_params[:locale] || 'en'
@ -7,8 +11,14 @@ class Api::V1::SearchController < Api::V1::ApiController
if filters if filters
conditions[:rarity] = filters['rarity'] unless filters['rarity'].blank? || filters['rarity'].empty? conditions[:rarity] = filters['rarity'] unless filters['rarity'].blank? || filters['rarity'].empty?
conditions[:element] = filters['element'] unless filters['element'].blank? || filters['element'].empty? conditions[:element] = filters['element'] unless filters['element'].blank? || filters['element'].empty?
conditions[:proficiency1] = filters['proficiency1'] unless filters['proficiency1'].blank? || filters['proficiency1'].empty? unless filters['proficiency1'].blank? || filters['proficiency1'].empty?
conditions[:proficiency2] = filters['proficiency2'] unless filters['proficiency2'].blank? || filters['proficiency2'].empty? conditions[:proficiency1] =
filters['proficiency1']
end
unless filters['proficiency2'].blank? || filters['proficiency2'].empty?
conditions[:proficiency2] =
filters['proficiency2']
end
# conditions[:series] = filters['series'] unless filters['series'].blank? || filters['series'].empty? # conditions[:series] = filters['series'] unless filters['series'].blank? || filters['series'].empty?
end end
@ -34,7 +44,10 @@ class Api::V1::SearchController < Api::V1::ApiController
if filters if filters
conditions[:rarity] = filters['rarity'] unless filters['rarity'].blank? || filters['rarity'].empty? conditions[:rarity] = filters['rarity'] unless filters['rarity'].blank? || filters['rarity'].empty?
conditions[:element] = filters['element'] unless filters['element'].blank? || filters['element'].empty? conditions[:element] = filters['element'] unless filters['element'].blank? || filters['element'].empty?
conditions[:proficiency] = filters['proficiency1'] unless filters['proficiency1'].blank? || filters['proficiency1'].empty? unless filters['proficiency1'].blank? || filters['proficiency1'].empty?
conditions[:proficiency] =
filters['proficiency1']
end
conditions[:series] = filters['series'] unless filters['series'].blank? || filters['series'].empty? conditions[:series] = filters['series'] unless filters['series'].blank? || filters['series'].empty?
end end
@ -136,4 +149,6 @@ class Api::V1::SearchController < Api::V1::ApiController
def search_params def search_params
params.require(:search).permit! params.require(:search).permit!
end end
end
end
end end

View file

@ -1,8 +1,12 @@
class Api::V1::UsersController < Api::V1::ApiController # frozen_string_literal: true
module Api
module V1
class UsersController < Api::V1::ApiController
class ForbiddenError < StandardError; end class ForbiddenError < StandardError; end
before_action :set, except: ['create', 'check_email', 'check_username'] before_action :set, except: %w[create check_email check_username]
before_action :set_by_id, only: ['info', 'update'] before_action :set_by_id, only: %w[info update]
def create def create
@user = User.new(user_params) @user = User.new(user_params)
@ -14,7 +18,8 @@ class Api::V1::UsersController < Api::V1::ApiController
scopes: 'public' scopes: 'public'
).token ).token
if @user.save! return unless @user.save!
@presenter = { @presenter = {
user_id: @user.id, user_id: @user.id,
username: @user.username, username: @user.username,
@ -23,8 +28,6 @@ class Api::V1::UsersController < Api::V1::ApiController
render :create, status: :created render :create, status: :created
end end
end
def update def update
render :info, status: :ok if @user.update(user_params) render :info, status: :ok if @user.update(user_params)
@ -39,7 +42,9 @@ class Api::V1::UsersController < Api::V1::ApiController
@per_page = 15 @per_page = 15
now = DateTime.current now = DateTime.current
start_time = (now - params['recency'].to_i.seconds).to_datetime.beginning_of_day unless request.params['recency'].blank? unless request.params['recency'].blank?
start_time = (now - params['recency'].to_i.seconds).to_datetime.beginning_of_day
end
conditions = {} conditions = {}
conditions[:element] = request.params['element'] unless request.params['element'].blank? conditions[:element] = request.params['element'] unless request.params['element'].blank?
@ -51,9 +56,9 @@ class Api::V1::UsersController < Api::V1::ApiController
.where(conditions) .where(conditions)
.order(created_at: :desc) .order(created_at: :desc)
.paginate(page: request.params[:page], per_page: @per_page) .paginate(page: request.params[:page], per_page: @per_page)
.each { |party| .each do |party|
party.favorited = (current_user) ? party.is_favorited(current_user) : false party.favorited = current_user ? party.is_favorited(current_user) : false
} end
@count = Party.where(conditions).count @count = Party.where(conditions).count
else else
render_not_found_response render_not_found_response
@ -61,37 +66,36 @@ class Api::V1::UsersController < Api::V1::ApiController
end end
def check_email def check_email
if params[:email].present? @available = if params[:email].present?
@available = User.where("email = ?", params[:email]).count == 0 User.where('email = ?', params[:email]).count.zero?
else else
@available = false false
end end
render :available render :available
end end
def check_username def check_username
if params[:username].present? @available = if params[:username].present?
@available = User.where("username = ?", params[:username]).count == 0 User.where('username = ?', params[:username]).count.zero?
else else
@available = false false
end end
render :available render :available
end end
def destroy def destroy; end
end
private private
# Specify whitelisted properties that can be modified. # Specify whitelisted properties that can be modified.
def set def set
@user = User.where("username = ?", params[:id]).first @user = User.where('username = ?', params[:id]).first
end end
def set_by_id def set_by_id
@user = User.where("id = ?", params[:id]).first @user = User.where('id = ?', params[:id]).first
end end
def user_params def user_params
@ -100,4 +104,6 @@ class Api::V1::UsersController < Api::V1::ApiController
:granblue_id, :picture, :element, :language, :gender, :private :granblue_id, :picture, :element, :language, :gender, :private
) )
end end
end
end
end end

View file

@ -1,4 +1,8 @@
class Api::V1::WeaponKeysController < Api::V1::ApiController # frozen_string_literal: true
module Api
module V1
class WeaponKeysController < Api::V1::ApiController
def all def all
conditions = {} conditions = {}
conditions[:series] = request.params['series'] conditions[:series] = request.params['series']
@ -8,4 +12,6 @@ class Api::V1::WeaponKeysController < Api::V1::ApiController
@keys = WeaponKey.where(conditions) @keys = WeaponKey.where(conditions)
render :all, status: :ok render :all, status: :ok
end end
end
end
end end

View file

@ -1,3 +1,5 @@
# frozen_string_literal: true
class ApplicationController < ActionController::API class ApplicationController < ActionController::API
# Not usually required for Rails 5 in API mode, but # Not usually required for Rails 5 in API mode, but
# necessary here because we're using RABL. # necessary here because we're using RABL.

View file

@ -1,3 +1,5 @@
# frozen_string_literal: true
class TokensController < Doorkeeper::TokensController class TokensController < Doorkeeper::TokensController
# Overriding create action # Overriding create action
# POST /oauth/token # POST /oauth/token
@ -7,7 +9,11 @@ class TokensController < Doorkeeper::TokensController
if response.status == :ok if response.status == :ok
# User the resource_owner_id from token to identify the user # User the resource_owner_id from token to identify the user
user = User.find(response.token.resource_owner_id) rescue nil user = begin
User.find(response.token.resource_owner_id)
rescue StandardError
nil
end
unless user.nil? unless user.nil?
### If you want to render user with template ### If you want to render user with template
@ -25,12 +31,10 @@ class TokensController < Doorkeeper::TokensController
end end
end end
self.headers.merge! response.headers headers.merge! response.headers
self.response_body = body.to_json self.response_body = body.to_json
self.status = response.status self.status = response.status
rescue Doorkeeper::Errors::DoorkeeperError => e rescue Doorkeeper::Errors::DoorkeeperError => e
handle_token_exception e handle_token_exception e
end end
end end

View file

@ -1,22 +0,0 @@
module Api::V1
class FavoriteAlreadyExistsError < GranblueError
def http_status
422
end
def code
"favorite_already_exists"
end
def message
"This user has favorited this party already"
end
def to_hash
{
message: message,
code: code
}
end
end
end

View file

@ -1,29 +0,0 @@
module Api::V1
class IncompatibleSkillError < GranblueError
def initialize(data)
@data = data
end
def http_status
422
end
def code
'incompatible_skill'
end
def message
'The selected skill cannot be added to the current job'
end
def to_hash
ap @data
{
message: message,
code: code,
job: @data[:job],
skill: @data[:skill]
}
end
end
end

View file

@ -1,22 +0,0 @@
module Api::V1
class NoJobProvidedError < GranblueError
def http_status
422
end
def code
"no_job_provided"
end
def message
"A job ID must be provided"
end
def to_hash
{
message: message,
code: code
}
end
end
end

View file

@ -1,22 +0,0 @@
module Api::V1
class NoJobSkillProvidedError < GranblueError
def http_status
422
end
def code
"no_job_skill_provided"
end
def message
"A job skill ID must be provided"
end
def to_hash
{
message: message,
code: code
}
end
end
end

View file

@ -1,22 +0,0 @@
module Api::V1
class UnauthorizedError < StandardError
def http_status
401
end
def code
"unauthorized"
end
def message
"User is not allowed to modify that resource"
end
def to_hash
{
message: message,
code: code
}
end
end
end

View file

@ -0,0 +1,26 @@
# frozen_string_literal: true
module Api
module V1
class FavoriteAlreadyExistsError < GranblueError
def http_status
422
end
def code
'favorite_already_exists'
end
def message
'This user has favorited this party already'
end
def to_hash
{
message: message,
code: code
}
end
end
end
end

View file

@ -0,0 +1,33 @@
# frozen_string_literal: true
module Api
module V1
class IncompatibleSkillError < GranblueError
def initialize(data)
@data = data
end
def http_status
422
end
def code
'incompatible_skill'
end
def message
'The selected skill cannot be added to the current job'
end
def to_hash
ap @data
{
message: message,
code: code,
job: @data[:job],
skill: @data[:skill]
}
end
end
end
end

View file

@ -0,0 +1,26 @@
# frozen_string_literal: true
module Api
module V1
class NoJobProvidedError < GranblueError
def http_status
422
end
def code
'no_job_provided'
end
def message
'A job ID must be provided'
end
def to_hash
{
message: message,
code: code
}
end
end
end
end

View file

@ -0,0 +1,26 @@
# frozen_string_literal: true
module Api
module V1
class NoJobSkillProvidedError < GranblueError
def http_status
422
end
def code
'no_job_skill_provided'
end
def message
'A job skill ID must be provided'
end
def to_hash
{
message: message,
code: code
}
end
end
end
end

View file

@ -1,3 +1,5 @@
# frozen_string_literal: true
module Api module Api
module V1 module V1
class SameFavoriteUserError < GranblueError class SameFavoriteUserError < GranblueError

View file

@ -0,0 +1,26 @@
# frozen_string_literal: true
module Api
module V1
class UnauthorizedError < StandardError
def http_status
401
end
def code
'unauthorized'
end
def message
'User is not allowed to modify that resource'
end
def to_hash
{
message: message,
code: code
}
end
end
end
end

View file

@ -1,3 +1,5 @@
# frozen_string_literal: true
class ValidationErrorSerializer class ValidationErrorSerializer
def initialize(record, field, details) def initialize(record, field, details)
@record = record @record = record

View file

@ -1,3 +1,4 @@
# frozen_string_literal: true
class ValidationErrorsSerializer class ValidationErrorsSerializer
attr_reader :record attr_reader :record

View file

@ -1,3 +1,5 @@
# frozen_string_literal: true
class ApplicationRecord < ActiveRecord::Base class ApplicationRecord < ActiveRecord::Base
self.abstract_class = true self.abstract_class = true
end end

View file

@ -1,3 +1,5 @@
# frozen_string_literal: true
class Character < ApplicationRecord class Character < ApplicationRecord
include PgSearch::Model include PgSearch::Model
@ -14,7 +16,7 @@ class Character < ApplicationRecord
using: { using: {
tsearch: { tsearch: {
prefix: true, prefix: true,
dictionary: "simple" dictionary: 'simple'
} }
} }
@ -90,5 +92,4 @@ class Character < ApplicationRecord
# Female: 2, # Female: 2,
# "Male/Female": 3 # "Male/Female": 3
# } # }
end end

View file

@ -1,8 +1,10 @@
# frozen_string_literal: true
class Favorite < ApplicationRecord class Favorite < ApplicationRecord
belongs_to :user belongs_to :user
belongs_to :party belongs_to :party
def party def party
Party.find(self.party_id) Party.find(party_id)
end end
end end

View file

@ -1,7 +1,9 @@
# frozen_string_literal: true
class GridCharacter < ApplicationRecord class GridCharacter < ApplicationRecord
belongs_to :party belongs_to :party
def character def character
Character.find(self.character_id) Character.find(character_id)
end end
end end

View file

@ -1,7 +1,9 @@
# frozen_string_literal: true
class GridSummon < ApplicationRecord class GridSummon < ApplicationRecord
belongs_to :party belongs_to :party
def summon def summon
Summon.find(self.summon_id) Summon.find(summon_id)
end end
end end

View file

@ -1,3 +1,5 @@
# frozen_string_literal: true
class GridWeapon < ApplicationRecord class GridWeapon < ApplicationRecord
belongs_to :party, belongs_to :party,
counter_cache: :weapons_count counter_cache: :weapons_count
@ -7,14 +9,14 @@ class GridWeapon < ApplicationRecord
belongs_to :weapon_key3, class_name: 'WeaponKey', foreign_key: :weapon_key3_id, optional: true belongs_to :weapon_key3, class_name: 'WeaponKey', foreign_key: :weapon_key3_id, optional: true
def weapon def weapon
Weapon.find(self.weapon_id) Weapon.find(weapon_id)
end end
def weapon_keys def weapon_keys
weapon_keys = [] weapon_keys = []
weapon_keys.push(self.weapon_key1) if self.weapon_key1 != nil weapon_keys.push(weapon_key1) unless weapon_key1.nil?
weapon_keys.push(self.weapon_key2) if self.weapon_key2 != nil weapon_keys.push(weapon_key2) unless weapon_key2.nil?
weapon_keys.push(self.weapon_key3) if self.weapon_key3 != nil weapon_keys.push(weapon_key3) unless weapon_key3.nil?
weapon_keys weapon_keys
end end

View file

@ -1,3 +1,5 @@
# frozen_string_literal: true
class Job < ApplicationRecord class Job < ApplicationRecord
belongs_to :party belongs_to :party

View file

@ -1,3 +1,5 @@
# frozen_string_literal: true
class JobSkill < ApplicationRecord class JobSkill < ApplicationRecord
alias eql? == alias eql? ==
@ -10,8 +12,8 @@ class JobSkill < ApplicationRecord
using: { using: {
tsearch: { tsearch: {
prefix: true, prefix: true,
dictionary: "simple", dictionary: 'simple'
}, }
} }
pg_search_scope :jp_search, pg_search_scope :jp_search,
@ -19,15 +21,15 @@ class JobSkill < ApplicationRecord
using: { using: {
tsearch: { tsearch: {
prefix: true, prefix: true,
dictionary: "simple", dictionary: 'simple'
}, }
} }
def display_resource(skill) def display_resource(skill)
skill.name_en skill.name_en
end end
def ==(o) def ==(other)
self.class == o.class && id == o.id self.class == other.class && id == other.id
end end
end end

View file

@ -1,3 +1,5 @@
# frozen_string_literal: true
class Party < ApplicationRecord class Party < ApplicationRecord
##### ActiveRecord Associations ##### ActiveRecord Associations
belongs_to :user, optional: true belongs_to :user, optional: true
@ -5,38 +7,38 @@ class Party < ApplicationRecord
belongs_to :job, optional: true belongs_to :job, optional: true
belongs_to :skill0, belongs_to :skill0,
foreign_key: "skill0_id", foreign_key: 'skill0_id',
class_name: "JobSkill", class_name: 'JobSkill',
optional: true optional: true
belongs_to :skill1, belongs_to :skill1,
foreign_key: "skill1_id", foreign_key: 'skill1_id',
class_name: "JobSkill", class_name: 'JobSkill',
optional: true optional: true
belongs_to :skill2, belongs_to :skill2,
foreign_key: "skill2_id", foreign_key: 'skill2_id',
class_name: "JobSkill", class_name: 'JobSkill',
optional: true optional: true
belongs_to :skill3, belongs_to :skill3,
foreign_key: "skill3_id", foreign_key: 'skill3_id',
class_name: "JobSkill", class_name: 'JobSkill',
optional: true optional: true
has_many :characters, has_many :characters,
foreign_key: "party_id", foreign_key: 'party_id',
class_name: "GridCharacter", class_name: 'GridCharacter',
dependent: :destroy dependent: :destroy
has_many :weapons, has_many :weapons,
foreign_key: "party_id", foreign_key: 'party_id',
class_name: "GridWeapon", class_name: 'GridWeapon',
dependent: :destroy dependent: :destroy
has_many :summons, has_many :summons,
foreign_key: "party_id", foreign_key: 'party_id',
class_name: "GridSummon", class_name: 'GridSummon',
dependent: :destroy dependent: :destroy
has_many :favorites has_many :favorites
@ -55,18 +57,14 @@ class Party < ApplicationRecord
def skills_are_unique def skills_are_unique
skills = [skill0, skill1, skill2, skill3].compact skills = [skill0, skill1, skill2, skill3].compact
if skills.uniq.length != skills.length return unless skills.uniq.length != skills.length
errors.add(:skill1, "must be unique") if skill0 == skill1
if skill0 == skill2 || skill1 == skill2 errors.add(:skill1, 'must be unique') if skill0 == skill1
errors.add(:skill2, "must be unique")
end
if skill0 == skill3 || skill1 == skill3 || skill2 == skill3 errors.add(:skill2, 'must be unique') if skill0 == skill2 || skill1 == skill2
errors.add(:skill3, "must be unique")
end
errors.add(:job_skills, "must be unique") errors.add(:skill3, 'must be unique') if skill0 == skill3 || skill1 == skill3 || skill2 == skill3
end
errors.add(:job_skills, 'must be unique')
end end
end end

View file

@ -1,2 +1,4 @@
# frozen_string_literal: true
class Raid < ApplicationRecord class Raid < ApplicationRecord
end end

View file

@ -1,3 +1,5 @@
# frozen_string_literal: true
class Summon < ApplicationRecord class Summon < ApplicationRecord
include PgSearch::Model include PgSearch::Model
@ -14,7 +16,7 @@ class Summon < ApplicationRecord
using: { using: {
tsearch: { tsearch: {
prefix: true, prefix: true,
dictionary: "simple" dictionary: 'simple'
} }
} }

View file

@ -1,3 +1,5 @@
# frozen_string_literal: true
class User < ApplicationRecord class User < ApplicationRecord
before_save { self.email = email.downcase } before_save { self.email = email.downcase }
@ -34,10 +36,10 @@ class User < ApplicationRecord
on: :update, on: :update,
if: :password_digest_changed? if: :password_digest_changed?
##### ActiveModel Security ##### ActiveModel Security
has_secure_password has_secure_password
def favorite_parties def favorite_parties
self.favorites.map { |favorite| favorite.party } favorites.map(&:party)
end end
end end

View file

@ -1,3 +1,5 @@
# frozen_string_literal: true
class Weapon < ApplicationRecord class Weapon < ApplicationRecord
include PgSearch::Model include PgSearch::Model
@ -14,7 +16,7 @@ class Weapon < ApplicationRecord
using: { using: {
tsearch: { tsearch: {
prefix: true, prefix: true,
dictionary: "simple" dictionary: 'simple'
} }
} }

View file

@ -1,2 +1,4 @@
# frozen_string_literal: true
class WeaponKey < ApplicationRecord class WeaponKey < ApplicationRecord
end end

View file

@ -1,4 +1,9 @@
Rails.application.configure do Rails.application.configure do
config.after_initialize do
ActiveRecord::Base.logger = Rails.logger.clone
ActiveRecord::Base.logger.level = Logger::INFO
end
# Settings specified here will take precedence over those in config/application.rb. # Settings specified here will take precedence over those in config/application.rb.
config.hosts << "grid-api.ngrok.io" config.hosts << "grid-api.ngrok.io"

View file

@ -0,0 +1,7 @@
require 'rails_helper'
RSpec.describe "JobSkills", type: :request do
describe "GET /index" do
pending "add some examples (or delete) #{__FILE__}"
end
end