Update wiki namespace in parsers
This commit is contained in:
parent
311c218863
commit
cec6132823
3 changed files with 21 additions and 12 deletions
|
|
@ -8,16 +8,22 @@ module Granblue
|
||||||
class CharacterParser
|
class CharacterParser
|
||||||
attr_reader :granblue_id
|
attr_reader :granblue_id
|
||||||
|
|
||||||
def initialize(granblue_id: String, debug: false)
|
def initialize(granblue_id: String, debug: false, use_local: false)
|
||||||
@character = Character.find_by(granblue_id: granblue_id)
|
@character = Character.find_by(granblue_id: granblue_id)
|
||||||
@wiki = Granblue::Parsers::Wiki.new
|
@wiki = Granblue::Parsers::Wiki.new
|
||||||
@debug = debug || false
|
@debug = debug || false
|
||||||
|
@use_local = use_local
|
||||||
end
|
end
|
||||||
|
|
||||||
# Fetches using @wiki and then processes the response
|
# Fetches using @wiki and then processes the response
|
||||||
# Returns true if successful, false if not
|
# Returns true if successful, false if not
|
||||||
# Raises an exception if something went wrong
|
# Raises an exception if something went wrong
|
||||||
def fetch(save: false)
|
def fetch(save: false)
|
||||||
|
if @use_local && @character.wiki_raw.present?
|
||||||
|
wikitext = @character.wiki_raw
|
||||||
|
return handle_fetch_success(wikitext, save)
|
||||||
|
end
|
||||||
|
|
||||||
response = fetch_wiki_info
|
response = fetch_wiki_info
|
||||||
return false if response.nil?
|
return false if response.nil?
|
||||||
|
|
||||||
|
|
@ -48,6 +54,9 @@ module Granblue
|
||||||
# Handle the response from the wiki if the response is successful
|
# Handle the response from the wiki if the response is successful
|
||||||
# If the save flag is set, it will persist the data to the database
|
# If the save flag is set, it will persist the data to the database
|
||||||
def handle_fetch_success(response, save)
|
def handle_fetch_success(response, save)
|
||||||
|
@character.wiki_raw = response
|
||||||
|
@character.save!
|
||||||
|
|
||||||
ap "#{@character.granblue_id}: Successfully fetched info for #{@character.wiki_en}" if @debug
|
ap "#{@character.granblue_id}: Successfully fetched info for #{@character.wiki_en}" if @debug
|
||||||
extracted = parse_string(response)
|
extracted = parse_string(response)
|
||||||
info = parse(extracted)
|
info = parse(extracted)
|
||||||
|
|
@ -151,12 +160,12 @@ module Granblue
|
||||||
info[:id] = hash['id']
|
info[:id] = hash['id']
|
||||||
info[:charid] = hash['charid'].scan(/\b\d{4}\b/)
|
info[:charid] = hash['charid'].scan(/\b\d{4}\b/)
|
||||||
|
|
||||||
info[:flb] = GranblueWiki.boolean.fetch(hash['5star'], false)
|
info[:flb] = Granblue::Parsers::Wiki.boolean.fetch(hash['5star'], false)
|
||||||
info[:ulb] = hash['max_evo'].to_i == 6
|
info[:ulb] = hash['max_evo'].to_i == 6
|
||||||
|
|
||||||
info[:rarity] = GranblueWiki.rarities.fetch(hash['rarity'], 0)
|
info[:rarity] = Granblue::Parsers::Wiki.rarities.fetch(hash['rarity'], 0)
|
||||||
info[:element] = GranblueWiki.elements.fetch(hash['element'], 0)
|
info[:element] = Granblue::Parsers::Wiki.elements.fetch(hash['element'], 0)
|
||||||
info[:gender] = GranblueWiki.genders.fetch(hash['gender'], 0)
|
info[:gender] = Granblue::Parsers::Wiki.genders.fetch(hash['gender'], 0)
|
||||||
|
|
||||||
info[:proficiencies] = proficiencies_from_hash(hash['weapon'])
|
info[:proficiencies] = proficiencies_from_hash(hash['weapon'])
|
||||||
info[:races] = races_from_hash(hash['race'])
|
info[:races] = races_from_hash(hash['race'])
|
||||||
|
|
@ -210,14 +219,14 @@ module Granblue
|
||||||
# Converts proficiencies from a string to a hash
|
# Converts proficiencies from a string to a hash
|
||||||
def proficiencies_from_hash(character)
|
def proficiencies_from_hash(character)
|
||||||
character.to_s.split(',').map.with_index do |prof, i|
|
character.to_s.split(',').map.with_index do |prof, i|
|
||||||
{ "proficiency#{i + 1}" => GranblueWiki.proficiencies[prof] }
|
{ "proficiency#{i + 1}" => Granblue::Parsers::Wiki.proficiencies[prof] }
|
||||||
end.reduce({}, :merge)
|
end.reduce({}, :merge)
|
||||||
end
|
end
|
||||||
|
|
||||||
# Converts races from a string to a hash
|
# Converts races from a string to a hash
|
||||||
def races_from_hash(race)
|
def races_from_hash(race)
|
||||||
race.to_s.split(',').map.with_index do |r, i|
|
race.to_s.split(',').map.with_index do |r, i|
|
||||||
{ "race#{i + 1}" => GranblueWiki.races[r] }
|
{ "race#{i + 1}" => Granblue::Parsers::Wiki.races[r] }
|
||||||
end.reduce({}, :merge)
|
end.reduce({}, :merge)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -10,7 +10,7 @@ module Granblue
|
||||||
|
|
||||||
def initialize(granblue_id: String, debug: false)
|
def initialize(granblue_id: String, debug: false)
|
||||||
@summon = Summon.find_by(granblue_id: granblue_id)
|
@summon = Summon.find_by(granblue_id: granblue_id)
|
||||||
@wiki = GranblueWiki.new(debug: debug)
|
@wiki = Granblue::Parsers::Wiki.new
|
||||||
@debug = debug || false
|
@debug = debug || false
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -10,7 +10,7 @@ module Granblue
|
||||||
|
|
||||||
def initialize(granblue_id: String, debug: false)
|
def initialize(granblue_id: String, debug: false)
|
||||||
@weapon = Weapon.find_by(granblue_id: granblue_id)
|
@weapon = Weapon.find_by(granblue_id: granblue_id)
|
||||||
@wiki = GranblueWiki.new(debug: debug)
|
@wiki = Granblue::Parsers::Wiki.new
|
||||||
@debug = debug || false
|
@debug = debug || false
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
@ -278,17 +278,17 @@ module Granblue
|
||||||
|
|
||||||
# Converts rarities from a string to a hash
|
# Converts rarities from a string to a hash
|
||||||
def rarity_from_hash(string)
|
def rarity_from_hash(string)
|
||||||
string ? GranblueWiki.rarities[string.upcase] : nil
|
string ? Granblue::Parsers::Wiki.rarities[string.upcase] : nil
|
||||||
end
|
end
|
||||||
|
|
||||||
# Converts proficiencies from a string to a hash
|
# Converts proficiencies from a string to a hash
|
||||||
def proficiency_from_hash(string)
|
def proficiency_from_hash(string)
|
||||||
GranblueWiki.proficiencies[string]
|
Granblue::Parsers::Wiki.proficiencies[string]
|
||||||
end
|
end
|
||||||
|
|
||||||
# Converts a bullet type from a string to a hash
|
# Converts a bullet type from a string to a hash
|
||||||
def bullet_from_hash(string)
|
def bullet_from_hash(string)
|
||||||
string ? GranblueWiki.bullets[string] : nil
|
string ? Granblue::Parsers::Wiki.bullets[string] : nil
|
||||||
end
|
end
|
||||||
|
|
||||||
# Parses a date string into a Date object
|
# Parses a date string into a Date object
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue