mirror of https://github.com/readrops/Readrops.git
Add adapter for Fever feeds api call
This commit is contained in:
parent
9189be9d93
commit
2396ba1f7e
|
@ -0,0 +1,67 @@
|
|||
package com.readrops.api.services.fever.adapters
|
||||
|
||||
import android.annotation.SuppressLint
|
||||
import com.readrops.api.utils.exceptions.ParseException
|
||||
import com.readrops.api.utils.extensions.nextNonEmptyString
|
||||
import com.readrops.api.utils.extensions.nextNullableString
|
||||
import com.readrops.api.utils.extensions.skipField
|
||||
import com.readrops.api.utils.extensions.skipToEnd
|
||||
import com.readrops.db.entities.Feed
|
||||
import com.squareup.moshi.FromJson
|
||||
import com.squareup.moshi.JsonReader
|
||||
import com.squareup.moshi.ToJson
|
||||
|
||||
class FeverFeedsAdapter {
|
||||
|
||||
@ToJson
|
||||
fun toJson(feeds: List<Feed>) = ""
|
||||
|
||||
@SuppressLint("CheckResult")
|
||||
@FromJson
|
||||
fun fromJson(reader: JsonReader): List<Feed> = with(reader) {
|
||||
return try {
|
||||
val feeds = arrayListOf<Feed>()
|
||||
|
||||
beginObject()
|
||||
|
||||
// skip basic fields (api_version, auth, last_refreshed...)
|
||||
repeat(3) {
|
||||
skipField()
|
||||
}
|
||||
|
||||
nextName() // beginning of feeds array
|
||||
beginArray()
|
||||
|
||||
while (hasNext()) {
|
||||
beginObject()
|
||||
|
||||
val feed = Feed()
|
||||
while (hasNext()) {
|
||||
with(feed) {
|
||||
when(selectName(NAMES)) {
|
||||
0 -> remoteId = nextInt().toString()
|
||||
1 -> name = nextNonEmptyString()
|
||||
2 -> url = nextNonEmptyString()
|
||||
3 -> siteUrl = nextNullableString()
|
||||
else -> skipValue()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
feeds += feed
|
||||
endObject()
|
||||
}
|
||||
|
||||
endArray()
|
||||
skipToEnd()
|
||||
|
||||
feeds
|
||||
} catch (e: Exception) {
|
||||
throw ParseException(e.message)
|
||||
}
|
||||
}
|
||||
|
||||
companion object {
|
||||
val NAMES: JsonReader.Options = JsonReader.Options.of("id", "title", "url", "site_url")
|
||||
}
|
||||
}
|
|
@ -0,0 +1,33 @@
|
|||
package com.readrops.api.services.fever.adapters
|
||||
|
||||
import com.readrops.api.TestUtils
|
||||
import com.readrops.db.entities.Feed
|
||||
import com.squareup.moshi.Moshi
|
||||
import com.squareup.moshi.Types
|
||||
import okio.Buffer
|
||||
import org.junit.Test
|
||||
import kotlin.test.assertEquals
|
||||
|
||||
class FeverFeedsAdapterTest {
|
||||
|
||||
val adapter = Moshi.Builder()
|
||||
.add(FeverFeedsAdapter())
|
||||
.build()
|
||||
.adapter<List<Feed>>(Types.newParameterizedType(List::class.java, Feed::class.java))!!
|
||||
|
||||
@Test
|
||||
fun validFeedsTest() {
|
||||
val stream = TestUtils.loadResource("services/fever/feeds.json")
|
||||
|
||||
val feeds = adapter.fromJson(Buffer().readFrom(stream))!!
|
||||
|
||||
assertEquals(feeds.size, 1)
|
||||
|
||||
with(feeds[0]) {
|
||||
assertEquals(name, "xda-developers")
|
||||
assertEquals(url, "https://www.xda-developers.com/feed/")
|
||||
assertEquals(siteUrl, "https://www.xda-developers.com/")
|
||||
assertEquals(remoteId, "32")
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,22 @@
|
|||
{
|
||||
"api_version": 3,
|
||||
"auth": 1,
|
||||
"last_refreshed_on_time": 1640284745,
|
||||
"feeds": [
|
||||
{
|
||||
"id": 32,
|
||||
"favicon_id": 32,
|
||||
"title": "xda-developers",
|
||||
"url": "https:\/\/www.xda-developers.com\/feed\/",
|
||||
"site_url": "https:\/\/www.xda-developers.com\/",
|
||||
"is_spark": 0,
|
||||
"last_updated_on_time": 1640364024
|
||||
}
|
||||
],
|
||||
"feeds_groups": [
|
||||
{
|
||||
"group_id": 3,
|
||||
"feed_ids": "5,4"
|
||||
}
|
||||
]
|
||||
}
|
Loading…
Reference in New Issue