oakdex-pokedex 0.3.0 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,97 @@
1
+ {
2
+ "$schema": "http://json-schema.org/draft-04/schema#",
3
+ "type": "object",
4
+ "title": "Type",
5
+ "definitions": {
6
+ "translations": {
7
+ "type": "object",
8
+ "properties": {
9
+ "cz": {
10
+ "type": "string"
11
+ },
12
+ "dk": {
13
+ "type": "string"
14
+ },
15
+ "fr": {
16
+ "type": "string"
17
+ },
18
+ "de": {
19
+ "type": "string"
20
+ },
21
+ "gr": {
22
+ "type": "string"
23
+ },
24
+ "it": {
25
+ "type": "string"
26
+ },
27
+ "pl": {
28
+ "type": "string"
29
+ },
30
+ "tr": {
31
+ "type": "string"
32
+ },
33
+ "en": {
34
+ "type": "string"
35
+ },
36
+ "jp": {
37
+ "type": "string"
38
+ },
39
+ "es": {
40
+ "type": "string"
41
+ }
42
+ },
43
+ "required": [
44
+ "en",
45
+ "de"
46
+ ],
47
+ "additionalProperties": false
48
+ }
49
+ },
50
+ "properties": {
51
+ "names": {
52
+ "$ref": "#/definitions/translations"
53
+ },
54
+ "color": {
55
+ "description": "Hex color",
56
+ "type": "string",
57
+ "pattern": "^#[A-Fa-f0-9]{6}$"
58
+ },
59
+ "effectivness": {
60
+ "type": "object",
61
+ "patternProperties": {
62
+ "Normal|Fighting|Flying|Poison|Ground|Rock|Bug|Ghost|Steel|Fire|Water|Grass|Electric|Psychic|Ice|Dragon|Dark|Fairy": {
63
+ "type": "number",
64
+ "minimum": 0,
65
+ "maximum": 2
66
+ }
67
+ },
68
+ "required": [
69
+ "Normal",
70
+ "Fighting",
71
+ "Flying",
72
+ "Poison",
73
+ "Ground",
74
+ "Rock",
75
+ "Bug",
76
+ "Ghost",
77
+ "Steel",
78
+ "Fire",
79
+ "Water",
80
+ "Grass",
81
+ "Electric",
82
+ "Psychic",
83
+ "Ice",
84
+ "Dragon",
85
+ "Dark",
86
+ "Fairy"
87
+ ],
88
+ "additionalProperties": false
89
+ }
90
+ },
91
+ "required": [
92
+ "names",
93
+ "effectivness",
94
+ "color"
95
+ ],
96
+ "additionalProperties": false
97
+ }
@@ -1,4 +1,5 @@
1
1
  require 'json'
2
+ require 'json-schema'
2
3
  require 'oakdex/pokedex/not_found'
3
4
  require 'oakdex/pokedex/type'
4
5
  require 'oakdex/pokedex/nature'
@@ -21,6 +21,12 @@ module Oakdex
21
21
  @all ||= map_json_data(json_folder, self)
22
22
  end
23
23
 
24
+ def add_to_all(custom_entries)
25
+ @all = @all.merge(custom_entries.map do |data|
26
+ [data['names']['en'], new(data)]
27
+ end.to_h)
28
+ end
29
+
24
30
  def find(name)
25
31
  all[name]
26
32
  end
@@ -30,6 +36,10 @@ module Oakdex
30
36
  (raise NotFound, "#{name} (#{json_folder}) could not be found")
31
37
  end
32
38
 
39
+ def reset!
40
+ @all = nil
41
+ end
42
+
33
43
  def where(conditions = {})
34
44
  all.values.select do |entry|
35
45
  conditions.all? do |name, value|
@@ -0,0 +1,7 @@
1
+ module Oakdex
2
+ module Pokedex
3
+ # Exception if custom pokemon is invalid
4
+ class InvalidPokemonError < StandardError
5
+ end
6
+ end
7
+ end
@@ -1,4 +1,5 @@
1
1
  require 'oakdex/pokedex/base'
2
+ require 'oakdex/pokedex/pokemon_importer'
2
3
 
3
4
  module Oakdex
4
5
  module Pokedex
@@ -28,12 +29,21 @@ module Oakdex
28
29
  end
29
30
 
30
31
  class << self
32
+ def import!(custom_pokemon)
33
+ PokemonImporter.new(custom_pokemon).import!
34
+ end
35
+
31
36
  def all_by_id
32
37
  @all_by_id ||= Hash[all.map do |_k, pokemon|
33
38
  [pokemon.national_id, pokemon]
34
39
  end]
35
40
  end
36
41
 
42
+ def add_to_all(custom_entries)
43
+ @all_by_id = nil
44
+ super(custom_entries)
45
+ end
46
+
37
47
  def find(name)
38
48
  all[name] || all_by_id[name]
39
49
  end
@@ -0,0 +1,57 @@
1
+ require 'oakdex/pokedex/invalid_pokemon_error'
2
+
3
+ module Oakdex
4
+ module Pokedex
5
+ # Handles Pokemon Import
6
+ class PokemonImporter
7
+ def initialize(custom_pokemon)
8
+ pokemon_list = if custom_pokemon.is_a?(Array)
9
+ custom_pokemon
10
+ else
11
+ JSON.parse(custom_pokemon)
12
+ end
13
+ @pokemon = pokemon_list.map do |p|
14
+ p.is_a?(Hash) ? p : JSON.parse(p)
15
+ end
16
+ end
17
+
18
+ def import!
19
+ validate!
20
+ Oakdex::Pokedex::Pokemon.add_to_all(@pokemon)
21
+ end
22
+
23
+ private
24
+
25
+ def validate!
26
+ @pokemon.each do |p|
27
+ begin
28
+ JSON::Validator.validate!(schema, p)
29
+ rescue JSON::Schema::ValidationError => e
30
+ pname = pokemon_name(p) || 'Unknown'
31
+ raise InvalidPokemonError, "Invalid #{pname}: #{e.message}"
32
+ end
33
+ end
34
+ end
35
+
36
+ def schema
37
+ @schema ||= JSON.parse(File.read(schema_path)).tap do |schema|
38
+ schema['properties']['national_id']['minimum'] = 10_001
39
+ schema['properties']['national_id'].delete('maximum')
40
+ schema['definitions']['pokemon']['enum'] = [nil] + available_pokemon
41
+ end
42
+ end
43
+
44
+ def available_pokemon
45
+ Pokemon.all.keys + @pokemon.map { |p| pokemon_name(p) }.compact
46
+ end
47
+
48
+ def pokemon_name(data)
49
+ data.dig('names', 'en') ? data['names']['en'] : nil
50
+ end
51
+
52
+ def schema_path
53
+ "#{Oakdex::Pokedex.data_dir}/schemas/pokemon.json"
54
+ end
55
+ end
56
+ end
57
+ end
@@ -1,5 +1,5 @@
1
1
  module Oakdex
2
2
  module Pokedex
3
- VERSION = '0.3.0'
3
+ VERSION = '0.4.0'
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,15 +1,29 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: oakdex-pokedex
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jalyna Schroeder
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-01-06 00:00:00.000000000 Z
12
- dependencies: []
11
+ date: 2019-01-20 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: json-schema
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
13
27
  description: Pokedex for Gen 1-7, in JSON and accessible through Ruby.
14
28
  email: jalyna.schroeder@gmail.com
15
29
  executables: []
@@ -24,6 +38,14 @@ files:
24
38
  - data/nature.json
25
39
  - data/pokemon.json
26
40
  - data/region.json
41
+ - data/schemas/ability.json
42
+ - data/schemas/egg_group.json
43
+ - data/schemas/generation.json
44
+ - data/schemas/move.json
45
+ - data/schemas/nature.json
46
+ - data/schemas/pokemon.json
47
+ - data/schemas/region.json
48
+ - data/schemas/type.json
27
49
  - data/type.json
28
50
  - lib/oakdex.rb
29
51
  - lib/oakdex/pokedex.rb
@@ -31,10 +53,12 @@ files:
31
53
  - lib/oakdex/pokedex/base.rb
32
54
  - lib/oakdex/pokedex/egg_group.rb
33
55
  - lib/oakdex/pokedex/generation.rb
56
+ - lib/oakdex/pokedex/invalid_pokemon_error.rb
34
57
  - lib/oakdex/pokedex/move.rb
35
58
  - lib/oakdex/pokedex/nature.rb
36
59
  - lib/oakdex/pokedex/not_found.rb
37
60
  - lib/oakdex/pokedex/pokemon.rb
61
+ - lib/oakdex/pokedex/pokemon_importer.rb
38
62
  - lib/oakdex/pokedex/region.rb
39
63
  - lib/oakdex/pokedex/type.rb
40
64
  - lib/oakdex/pokedex/version.rb
@@ -58,7 +82,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
58
82
  - !ruby/object:Gem::Version
59
83
  version: '0'
60
84
  requirements: []
61
- rubygems_version: 3.0.1
85
+ rubyforge_project:
86
+ rubygems_version: 2.5.2
62
87
  signing_key:
63
88
  specification_version: 4
64
89
  summary: Comprehensive Pokedex for Gen 1-7