Skip to content

Commit 6d7c612

Browse files
committed
+ Pokemon Species API
1 parent 569929a commit 6d7c612

File tree

2 files changed

+215
-0
lines changed

2 files changed

+215
-0
lines changed

Example/Tests/Tests.swift

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -609,4 +609,22 @@ class Tests: XCTestCase {
609609
XCTAssertNil(err, "Something went wrong")
610610
}
611611
}
612+
613+
func testFetchPokemonSpecies() {
614+
let asyncExpectation = expectationWithDescription("Fetch Pokemon Species")
615+
PokemonKit.fetchPokemonSpecies("1")
616+
.then{ response -> Void in
617+
XCTAssertNotNil(response);
618+
print(response);
619+
asyncExpectation.fulfill();
620+
}.error{ err in
621+
XCTFail("Should not failed with \(err)")
622+
asyncExpectation.fulfill();
623+
624+
}
625+
626+
self.waitForExpectationsWithTimeout(30) { (err) -> Void in
627+
XCTAssertNil(err, "Something went wrong")
628+
}
629+
}
612630
}

Pod/Classes/PokemonKit.swift

Lines changed: 197 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,171 @@ let baseURL: String = "http://pokeapi.co/api/v2"
1919

2020
// MARK: Classes
2121

22+
/*
23+
entry_number The index number within the Pokédex integer
24+
name The Pokédex the referenced Pokémon species can be found in NamedAPIResource (Pokedex)
25+
*/
26+
public class PKMPokemonSpeciesDexEntry: Mappable {
27+
public var entryNumber: Int?
28+
public var name: PKMNamedAPIResource?
29+
30+
required public init?(_ map: Map) {
31+
32+
}
33+
34+
public func mapping(map: Map) {
35+
entryNumber <- map["entry_number"]
36+
name <- map["name"]
37+
}
38+
}
39+
40+
/*
41+
base_score The base score given to the player when the referenced Pokémon is caught during a pal park run integer
42+
rate The base rate for encountering the referenced Pokémon in this pal park area integer
43+
area The pal park area where this encounter happens NamedAPIResource (PalParkArea)
44+
*/
45+
public class PKMPalParkEncounterArea: Mappable {
46+
public var baseScore: Int?
47+
public var rate: Int?
48+
public var area: PKMNamedAPIResource?
49+
50+
required public init?(_ map: Map) {
51+
52+
}
53+
54+
public func mapping(map: Map) {
55+
baseScore <- map["base_score"]
56+
rate <- map["rate"]
57+
area <- map["area"]
58+
}
59+
}
60+
61+
/*
62+
flavor_text The localized flavor text for an API resource in a specific language string
63+
language The language this name is in NamedAPIResource (Language)
64+
version The version this flavor text entry is used in NamedAPIResource (Version)
65+
*/
66+
public class PKMPokemonSpeciesFlavorText: Mappable {
67+
public var flavorText: String?
68+
public var language: PKMNamedAPIResource?
69+
public var version: PKMNamedAPIResource?
70+
71+
required public init?(_ map: Map) {
72+
73+
}
74+
75+
public func mapping(map: Map) {
76+
flavorText <- map["flavor_text"]
77+
language <- map["language"]
78+
version <- map["version"]
79+
}
80+
}
81+
82+
/*
83+
genus The localized genus for the referenced pokemon species string
84+
language The language this genus is in NamedAPIResource (Language)
85+
*/
86+
public class PKMGenus: Mappable {
87+
public var genus: String?
88+
public var language: PKMNamedAPIResource?
89+
90+
required public init?(_ map: Map) {
91+
92+
}
93+
94+
public func mapping(map: Map) {
95+
genus <- map["genus"]
96+
language <- map["language"]
97+
}
98+
}
99+
100+
101+
/*
102+
id The identifier for this Pokémon species resource integer
103+
name The name for this Pokémon species resource string
104+
order The order in which species should be sorted. Based on National Dex order, except families are grouped together and sorted by stage. integer
105+
gender_rate The chance of this Pokémon being female, in eighths; or -1 for genderless integer
106+
capture_rate The base capture rate; up to 255. The higher the number, the easier the catch. integer
107+
base_happiness The happiness when caught by a normal Pokéball; up to 255. The higher the number, the happier the Pokémon. integer
108+
is_baby Whether or not this is a baby Pokémon boolean
109+
hatch_counter Initial hatch counter: one must walk 255 × (hatch_counter + 1) steps before this Pokémon's egg hatches, unless utilizing bonuses like Flame Body's integer
110+
has_gender_differences Whether or not this Pokémon can have different genders boolean
111+
forms_switchable Whether or not this Pokémon has multiple forms and can switch between them boolean
112+
growth_rate The rate at which this Pokémon species gains levels NamedAPIResource (GrowthRate)
113+
pokedex_numbers A list of pokedexes and the indexes reserved within them for this Pokémon species list PokemonSpeciesDexEntry
114+
egg_groups A list of egg groups this Pokémon species is a member of list NamedAPIResource (EggGroup)
115+
color The color of this Pokémon for gimmicky Pokédex search list NamedAPIResource (PokemonColor)
116+
shape The shape of this Pokémon for gimmicky Pokédex search list NamedAPIResource (PokemonShape)
117+
evolves_from_species The Pokémon species that evolves into this pokemon_species NamedAPIResource (PokemonSpecies)
118+
evolution_chain The evolution chain this Pokémon species is a member of APIResource (EvolutionChain)
119+
habitat The habitat this Pokémon species can be encountered in NamedAPIResource (PokemonHabitat)
120+
generation The generation this Pokémon species was introduced in NamedAPIResource (Generation)
121+
names The name of this Pokémon species listed in different languages list Name
122+
pal_park_encounters A list of encounters that can be had with this Pokémon species in pal park list PalParkEncounterArea
123+
flavor_text_entries The flavor text of this flavor text listed in different languages list PokemonSpeciesFlavorText
124+
form_descriptions Descriptions of different forms Pokémon take on within the Pokémon species list Description
125+
genera The genus of this Pokémon species listed in multiple languages Genus
126+
varieties A list of the Pokémon that exist within this Pokémon species list NamedAPIResource (Pokemon)
127+
*/
128+
public class PKMPokemonSpecies: Mappable {
129+
public var id: Int?
130+
public var name: String?
131+
public var order: Int?
132+
public var genderRate: Int?
133+
public var captureRate: Int?
134+
public var baseHappiness: Int?
135+
public var isBaby: Bool?
136+
public var hatchCounter: Int?
137+
public var hasGenderDifferences: Bool?
138+
public var formsSwitchable: Bool?
139+
public var growthRate: PKMNamedAPIResource?
140+
public var pokedexNumbers: [PKMPokemonSpeciesDexEntry]?
141+
public var eggGroups: [PKMNamedAPIResource]?
142+
public var color: PKMNamedAPIResource?
143+
public var shape: PKMNamedAPIResource?
144+
public var evolutionChain: PKMAPIResource?
145+
public var habitat: PKMNamedAPIResource?
146+
public var generation: PKMNamedAPIResource?
147+
public var names: [PKMName]?
148+
public var palParkEncounters: [PKMPalParkEncounterArea]?
149+
public var flavorTextEntries: [PKMPokemonSpeciesFlavorText]?
150+
public var formDescriptions: [PKMDescription]?
151+
public var genera: [PKMGenus]?
152+
public var varieties: [PKMNamedAPIResource]?
153+
154+
required public init?(_ map: Map) {
155+
156+
}
157+
158+
public func mapping(map: Map) {
159+
id <- map["id"]
160+
name <- map["name"]
161+
order <- map["order"]
162+
genderRate <- map["gender_rate"]
163+
captureRate <- map["capture_rate"]
164+
baseHappiness <- map["base_happiness"]
165+
isBaby <- map["is_baby"]
166+
hatchCounter <- map["hatch_counter"]
167+
hasGenderDifferences <- map["has_gender_differences"]
168+
formsSwitchable <- map["forms_switchable"]
169+
growthRate <- map["growth_rate"]
170+
pokedexNumbers <- map["pokedex_numbers"]
171+
eggGroups <- map["egg_groups"]
172+
color <- map["color"]
173+
shape <- map["shape"]
174+
evolutionChain <- map["evolution_chain"]
175+
habitat <- map["habitat"]
176+
generation <- map["generation"]
177+
names <- map["names"]
178+
palParkEncounters <- map["pal_park_encounters"]
179+
flavorTextEntries <- map["flavor_text_entries"]
180+
formDescriptions <- map["form_descriptions"]
181+
genera <- map["genera"]
182+
varieties <- map["varieties"]
183+
}
184+
}
185+
186+
22187
/*
23188
awesome_name The localized "scientific" name for an API resource in a specific language string
24189
language The language this "scientific" name is in NamedAPIResource (Language)
@@ -3541,6 +3706,38 @@ public func fetchPokemonShape(pokemonShapeId: String) -> Promise<PKMPokemonShape
35413706
reject(response.result.error!)
35423707
}
35433708

3709+
}
3710+
}
3711+
}
3712+
3713+
//PokemonSpecies
3714+
3715+
public func fetchPokemonSpecies() -> Promise<PKMPagedObject> {
3716+
return Promise { fulfill, reject in
3717+
let URL = baseURL + "/pokemon-species"
3718+
3719+
Alamofire.request(.GET, URL).responseObject() { (response: Response<PKMPagedObject, NSError>) in
3720+
if (response.result.isSuccess) {
3721+
fulfill(response.result.value!)
3722+
}else{
3723+
reject(response.result.error!)
3724+
}
3725+
}
3726+
}
3727+
}
3728+
3729+
public func fetchPokemonSpecies(pokemonSpeciesId: String) -> Promise<PKMPokemonSpecies>{
3730+
return Promise { fulfill, reject in
3731+
let URL = baseURL + "/pokemon-species/" + pokemonSpeciesId
3732+
3733+
Alamofire.request(.GET, URL).responseObject() { (response: Response<PKMPokemonSpecies, NSError>) in
3734+
3735+
if (response.result.isSuccess) {
3736+
fulfill(response.result.value!)
3737+
}else{
3738+
reject(response.result.error!)
3739+
}
3740+
35443741
}
35453742
}
35463743
}

0 commit comments

Comments
 (0)