Class: Oakdex::Pokemon::Import

Inherits:
Object
  • Object
show all
Defined in:
lib/oakdex/pokemon/import.rb

Overview

Imports and validates pokemon

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data) ⇒ Import

Returns a new instance of Import.



14
15
16
# File 'lib/oakdex/pokemon/import.rb', line 14

def initialize(data)
  @data = data.is_a?(Hash) ? data : JSON.parse(data)
end

Class Method Details

.schemaObject



9
10
11
12
# File 'lib/oakdex/pokemon/import.rb', line 9

def self.schema
  schema_path = File.join Oakdex::Pokemon.root, 'lib', 'oakdex', 'pokemon', 'schema.json'
  @schema ||= File.read(File.expand_path(schema_path))
end

Instance Method Details

#import!Object



18
19
20
21
22
23
24
25
26
27
# File 'lib/oakdex/pokemon/import.rb', line 18

def import!
  JSON::Validator.validate!(self.class.schema, @data)
  pok = Oakdex::Pokemon.new(@data['species_id'], attributes)
  apply_growth_events(pok)
  pok
rescue JSON::Schema::ValidationError => e
  raise Oakdex::Pokemon::InvalidPokemon, e.message
rescue Oakdex::Pokedex::NotFound => e
  raise Oakdex::Pokemon::InvalidPokemon, e.message
end