Skip to content

Commit 070b927

Browse files
committed
Add getVersion function
1 parent 780905e commit 070b927

File tree

5 files changed

+62
-13
lines changed

5 files changed

+62
-13
lines changed

src/main/kotlin/com/github/azurapi/azurapikotlin/api/Atago.kt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.github.azurapi.azurapikotlin.api
22

33
import com.github.azurapi.azurapikotlin.internal.entities.Ship
4+
import com.github.azurapi.azurapikotlin.internal.entities.Version
45
import com.github.azurapi.azurapikotlin.internal.exceptions.DatabaseException
56
import com.github.azurapi.azurapikotlin.internal.exceptions.ShipNotFoundException
67
import com.github.azurapi.azurapikotlin.internal.exceptions.ApiException
@@ -59,4 +60,13 @@ object Atago {
5960
fun getAllShips(): List<Ship> {
6061
return database.shipsById.values.stream().collect(Collectors.toList())
6162
}
63+
64+
/**
65+
* @since 1.0.0
66+
*
67+
* Get version of the api wrapper
68+
*/
69+
fun getVersion(): Version {
70+
return Version(AtagoInfo.VERSION, database.lastUpdated, database.databaseVersion, database.lastUpdatedDatabase)
71+
}
6272
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.github.azurapi.azurapikotlin.internal.entities
2+
3+
import java.util.*
4+
5+
/**
6+
* Version representation
7+
* @param apiVersion api version
8+
* @param lastUpdatedApi last time database has been reloaded
9+
* @param databaseVersion json version
10+
* @param lastUpdatedDatabase last time json has been refreshed
11+
*/
12+
data class Version (
13+
val apiVersion: String,
14+
val lastUpdatedApi: Date,
15+
val databaseVersion: String,
16+
val lastUpdatedDatabase: Date
17+
)

src/main/kotlin/com/github/azurapi/azurapikotlin/json/Takao.kt

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,39 @@
11
package com.github.azurapi.azurapikotlin.json
22

3-
import com.github.kittinunf.fuel.httpGet
4-
import com.github.kittinunf.fuel.json.responseJson
53
import com.github.azurapi.azurapikotlin.internal.entities.Ship
64
import com.github.azurapi.azurapikotlin.internal.exceptions.DatabaseException
75
import com.github.azurapi.azurapikotlin.utils.ShipParser
6+
import com.github.kittinunf.fuel.httpGet
7+
import com.github.kittinunf.fuel.json.responseJson
88
import info.debatty.java.stringsimilarity.Cosine
9-
import info.debatty.java.stringsimilarity.Levenshtein
10-
import info.debatty.java.stringsimilarity.NormalizedLevenshtein
119
import org.json.JSONObject
10+
import java.util.*
11+
import kotlin.collections.HashMap
1212

1313
/**
14-
* JSON deserializer object
14+
* JSON deserializer object.
15+
* THIS CLASS IS NOT MEANT TO BE USED, USE ATAGO INSTEAD.
1516
* @throws DatabaseException
1617
*/
1718
class Takao {
1819

1920
private lateinit var jsonDatabase: JSONObject
21+
private lateinit var jsonVersion: JSONObject
22+
23+
var lastUpdated: Date
24+
lateinit var lastUpdatedDatabase: Date
25+
lateinit var databaseVersion: String
26+
2027
var shipsById = HashMap<String, Ship>()
2128
var shipsByName = HashMap<String, Ship>()
2229

2330
init {
2431
loadDatabase()
32+
lastUpdated = Date()
2533
}
2634

27-
fun loadJSON(): JSONObject {
28-
val (_, response, result) = TakaoInfo.JSON_SOURCE
35+
private fun loadJSON(url: String): JSONObject {
36+
val (_, response, result) = url
2937
.httpGet()
3038
.responseJson()
3139
return if (response.statusCode == 200) {
@@ -35,14 +43,18 @@ class Takao {
3543
}
3644
}
3745

38-
private fun loadDatabase() {
46+
fun loadDatabase() {
47+
lastUpdated = Date()
3948
try {
40-
jsonDatabase = loadJSON()
49+
jsonDatabase = loadJSON(TakaoInfo.JSON_SOURCE)
50+
jsonVersion = loadJSON(TakaoInfo.JSON_VERSION)
4151
for (shipId in jsonDatabase.keySet()) {
4252
val ship = ShipParser.jsonToShip(jsonDatabase.getJSONObject(shipId), shipId)
4353
shipsById[ship.id.toLowerCase()] = ship
4454
shipsByName[ship.names.en.toLowerCase()] = ship
4555
}
56+
databaseVersion = jsonVersion.getInt("version-number").toString()
57+
lastUpdatedDatabase = Date(jsonVersion.getLong("last-data-refresh-date"))
4658
} catch (e: Exception) {
4759
throw DatabaseException("Could not reload database: (${e.message})")
4860
}

src/main/kotlin/com/github/azurapi/azurapikotlin/json/TakaoInfo.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@ package com.github.azurapi.azurapikotlin.json
44
* JSON deserializer info
55
*/
66
object TakaoInfo {
7-
const val VERSION = "1.0.0"
87
const val JSON_SOURCE = "https://raw.githubusercontent.com/AzurAPI/azurapi-js-setup/master/ships.json"
8+
const val JSON_VERSION = "https://raw.githubusercontent.com/AzurAPI/azurapi-js-setup/master/version-info.json"
99
}

src/test/kotlin/com/github/azurapi/azurapikotlin/api/AtagoTestCase.kt

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
package com.github.azurapi.azurapikotlin.api
22

3-
import info.debatty.java.stringsimilarity.Cosine
4-
import info.debatty.java.stringsimilarity.Levenshtein
53
import io.kotlintest.TestCase
6-
import io.kotlintest.matchers.boolean.shouldBeTrue
4+
import io.kotlintest.matchers.types.shouldNotBeNull
75
import io.kotlintest.shouldBe
86
import io.kotlintest.specs.StringSpec
97

@@ -29,5 +27,17 @@ class AtagoTestCase : StringSpec() {
2927
Atago.getShipByName("Jeanne d'Arc").names.en.shouldBe("Jeanne d'Arc")
3028
Atago.getShipByName("jeanne").names.en.shouldBe("Jeanne d'Arc")
3129
}
30+
31+
"it should return Atago by her id" {
32+
Atago.getShipById("201").names.en.shouldBe("Atago")
33+
}
34+
35+
"it should return a Version object of the api" {
36+
val version = Atago.getVersion()
37+
version.databaseVersion.shouldNotBeNull()
38+
version.lastUpdatedApi.shouldNotBeNull()
39+
version.lastUpdatedDatabase.shouldNotBeNull()
40+
version.apiVersion.shouldNotBeNull()
41+
}
3242
}
3343
}

0 commit comments

Comments
 (0)