From 2396ba1f7e7c41ff546c111eecc24bffc0c0e248 Mon Sep 17 00:00:00 2001 From: Shinokuni Date: Sat, 25 Dec 2021 17:34:40 +0100 Subject: [PATCH] Add adapter for Fever feeds api call --- .../fever/adapters/FeverFeedsAdapter.kt | 67 +++++++++++++++++++ .../fever/adapters/FeverFeedsAdapterTest.kt | 33 +++++++++ .../test/resources/services/fever/feeds.json | 22 ++++++ 3 files changed, 122 insertions(+) create mode 100644 api/src/main/java/com/readrops/api/services/fever/adapters/FeverFeedsAdapter.kt create mode 100644 api/src/test/java/com/readrops/api/services/fever/adapters/FeverFeedsAdapterTest.kt create mode 100644 api/src/test/resources/services/fever/feeds.json diff --git a/api/src/main/java/com/readrops/api/services/fever/adapters/FeverFeedsAdapter.kt b/api/src/main/java/com/readrops/api/services/fever/adapters/FeverFeedsAdapter.kt new file mode 100644 index 00000000..dd160e38 --- /dev/null +++ b/api/src/main/java/com/readrops/api/services/fever/adapters/FeverFeedsAdapter.kt @@ -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) = "" + + @SuppressLint("CheckResult") + @FromJson + fun fromJson(reader: JsonReader): List = with(reader) { + return try { + val feeds = arrayListOf() + + 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") + } +} \ No newline at end of file diff --git a/api/src/test/java/com/readrops/api/services/fever/adapters/FeverFeedsAdapterTest.kt b/api/src/test/java/com/readrops/api/services/fever/adapters/FeverFeedsAdapterTest.kt new file mode 100644 index 00000000..f193eafe --- /dev/null +++ b/api/src/test/java/com/readrops/api/services/fever/adapters/FeverFeedsAdapterTest.kt @@ -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>(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") + } + } +} \ No newline at end of file diff --git a/api/src/test/resources/services/fever/feeds.json b/api/src/test/resources/services/fever/feeds.json new file mode 100644 index 00000000..e437462b --- /dev/null +++ b/api/src/test/resources/services/fever/feeds.json @@ -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" + } + ] +} \ No newline at end of file