Skip to content

Commit

Permalink
Turn Podcastindex Chapters type property into a MediaType (MIME type)
Browse files Browse the repository at this point in the history
  • Loading branch information
mpgirro committed Feb 28, 2021
1 parent a31d483 commit ac73ac2
Show file tree
Hide file tree
Showing 14 changed files with 32 additions and 20 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package dev.stalla.builder.episode

import com.google.common.net.MediaType
import dev.stalla.builder.Builder
import dev.stalla.model.podcastindex.Chapters
import dev.stalla.util.whenNotNull
Expand All @@ -11,7 +12,7 @@ public interface EpisodePodcastindexChaptersBuilder : Builder<Chapters> {
public fun url(url: String): EpisodePodcastindexChaptersBuilder

/** Set the type value. */
public fun type(type: String): EpisodePodcastindexChaptersBuilder
public fun type(type: MediaType): EpisodePodcastindexChaptersBuilder

override fun applyFrom(prototype: Chapters?): EpisodePodcastindexChaptersBuilder =
whenNotNull(prototype) { chapters ->
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package dev.stalla.builder.validating.episode

import com.google.common.net.MediaType
import dev.stalla.builder.episode.EpisodePodcastindexChaptersBuilder
import dev.stalla.model.podcastindex.Chapters
import dev.stalla.util.InternalApi
Expand All @@ -8,11 +9,11 @@ import dev.stalla.util.InternalApi
internal class ValidatingEpisodePodcastindexChaptersBuilder : EpisodePodcastindexChaptersBuilder {

private lateinit var urlValue: String
private lateinit var typeValue: String
private lateinit var typeValue: MediaType

override fun url(url: String): EpisodePodcastindexChaptersBuilder = apply { this.urlValue = url }

override fun type(type: String): EpisodePodcastindexChaptersBuilder = apply { this.typeValue = type }
override fun type(type: MediaType): EpisodePodcastindexChaptersBuilder = apply { this.typeValue = type }

override val hasEnoughDataToBuild: Boolean
get() = ::urlValue.isInitialized && ::typeValue.isInitialized
Expand Down
3 changes: 2 additions & 1 deletion src/main/kotlin/dev/stalla/model/podcastindex/Chapters.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package dev.stalla.model.podcastindex

import com.google.common.net.MediaType
import dev.stalla.builder.episode.EpisodePodcastindexChaptersBuilder
import dev.stalla.builder.validating.episode.ValidatingEpisodePodcastindexChaptersBuilder
import dev.stalla.model.BuilderFactory
Expand All @@ -13,7 +14,7 @@ import dev.stalla.model.BuilderFactory
*/
public data class Chapters(
val url: String,
val type: String
val type: MediaType
) {

/** Provides a builder for the [Chapters] class. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import dev.stalla.builder.podcast.PodcastPodcastindexFundingBuilder
import dev.stalla.builder.podcast.PodcastPodcastindexLockedBuilder
import dev.stalla.builder.podcast.ProvidingPodcastBuilder
import dev.stalla.dom.getAttributeByName
import dev.stalla.dom.getAttributeValueByName
import dev.stalla.dom.parseAsMediaTypeOrNull
import dev.stalla.dom.textAsBooleanOrNull
import dev.stalla.model.StyledDuration
import dev.stalla.model.podcastindex.TranscriptType
Expand Down Expand Up @@ -95,8 +97,8 @@ internal object PodcastindexParser : NamespaceParser() {
private fun Node.toChaptersBuilder(
chaptersBuilder: EpisodePodcastindexChaptersBuilder
): EpisodePodcastindexChaptersBuilder? {
val url = getAttributeByName("url")?.value.trimmedOrNullIfBlank()
val type = getAttributeByName("type")?.value.trimmedOrNullIfBlank()
val url = getAttributeValueByName("url").trimmedOrNullIfBlank()
val type = getAttributeValueByName("type").parseAsMediaTypeOrNull()

if (url == null || type == null) return null
return chaptersBuilder.url(url)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ internal object PodcastindexWriter : NamespaceWriter() {
if (podcastNs.chapters != null && podcastNs.chapters.canBeWritten()) {
appendElement("chapters", namespace) {
setAttribute("url", podcastNs.chapters.url.trim())
setAttribute("type", podcastNs.chapters.type.trim())
setAttribute("type", podcastNs.chapters.type.toString())
}
}

Expand All @@ -75,5 +75,5 @@ internal object PodcastindexWriter : NamespaceWriter() {
}
}

private fun Chapters.canBeWritten() = url.isNotBlank() && type.isNotBlank()
private fun Chapters.canBeWritten() = url.isNotBlank()
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package dev.stalla.model;

import com.google.common.net.MediaType;
import dev.stalla.builder.episode.*;
import dev.stalla.model.bitlove.Bitlove;
import dev.stalla.model.content.Content;
Expand Down Expand Up @@ -140,7 +141,7 @@ public void testEpisodePodcastTranscriptBuilderFactory() {
public void testEpisodePodcastChapterBuilderFactory() {
EpisodePodcastindexChaptersBuilder episodePodcastindexChaptersBuilder = Chapters.builder()
.url("url")
.type("type");
.type(MediaType.JSON_UTF_8);

assertNotNull(episodePodcastindexChaptersBuilder.build());
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
package dev.stalla.builder.fake.episode

import com.google.common.net.MediaType
import dev.stalla.builder.episode.EpisodePodcastindexChaptersBuilder
import dev.stalla.builder.fake.FakeBuilder
import dev.stalla.model.podcastindex.Chapters

internal class FakeEpisodePodcastindexChaptersBuilder : FakeBuilder<Chapters>(), EpisodePodcastindexChaptersBuilder {

var url: String? = null
var type: String? = null
var type: MediaType? = null

override fun url(url: String): EpisodePodcastindexChaptersBuilder = apply { this.url = url }

override fun type(type: String): EpisodePodcastindexChaptersBuilder = apply { this.type = type }
override fun type(type: MediaType): EpisodePodcastindexChaptersBuilder = apply { this.type = type }

override fun equals(other: Any?): Boolean {
if (this === other) return true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import assertk.assertions.isNotNull
import assertk.assertions.isNull
import assertk.assertions.isTrue
import assertk.assertions.prop
import com.google.common.net.MediaType
import dev.stalla.builder.episode.EpisodeBuilder
import dev.stalla.builder.validating.ValidatingAtomPersonBuilder
import dev.stalla.builder.validating.ValidatingRssCategoryBuilder
Expand Down Expand Up @@ -50,7 +51,7 @@ internal class ValidatingEpisodeBuilderTest {

private val expectedPodcastChaptersBuilder = ValidatingEpisodePodcastindexChaptersBuilder()
.url("https://example.com/episode/chapters.json")
.type("application/json+chapters")
.type(MediaType.JSON_UTF_8.withoutParameters())

@Test
internal fun `should not build a Episode when the mandatory fields are missing`() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import assertk.assertions.isNotNull
import assertk.assertions.isNull
import assertk.assertions.isTrue
import assertk.assertions.prop
import com.google.common.net.MediaType
import dev.stalla.builder.episode.EpisodePodcastindexBuilder
import dev.stalla.model.StyledDuration
import dev.stalla.model.episode.anEpisodePodcastindex
Expand All @@ -22,7 +23,7 @@ internal class ValidatingEpisodePodcastindexBuilderTest {

private val expectedChaptersBuilder = ValidatingEpisodePodcastindexChaptersBuilder()
.url("https://example.com/episode/chapters.json")
.type("application/json+chapters")
.type(MediaType.JSON_UTF_8.withoutParameters())

private val firstExpectedSoundbiteBuilder = ValidatingEpisodePodcastindexSoundbiteBuilder()
.startTime(StyledDuration.secondsAndFraction(1))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import assertk.assertions.isNotNull
import assertk.assertions.isNull
import assertk.assertions.isTrue
import assertk.assertions.prop
import com.google.common.net.MediaType
import dev.stalla.builder.episode.EpisodePodcastindexChaptersBuilder
import dev.stalla.model.episode.anEpisodePodcastindexChapters
import dev.stalla.model.podcastindex.Chapters
Expand All @@ -30,7 +31,7 @@ internal class ValidatingEpisodePodcastindexChaptersBuilderTest {
@Test
internal fun `should not build an Episode Podcastindex Chapters with when the url field is missing`() {
val chaptersBuilder = ValidatingEpisodePodcastindexChaptersBuilder()
.type("text/json+chapters")
.type(MediaType.JSON_UTF_8.withoutParameters())

assertAll {
assertThat(chaptersBuilder).prop(EpisodePodcastindexChaptersBuilder::hasEnoughDataToBuild).isFalse()
Expand All @@ -55,14 +56,14 @@ internal class ValidatingEpisodePodcastindexChaptersBuilderTest {
internal fun `should build an Episode Podcastindex Chapters with with all the added entries to its fields`() {
val chaptersBuilder = ValidatingEpisodePodcastindexChaptersBuilder()
.url("https://example.com/episode/chapters.json")
.type("text/json+chapters")
.type(MediaType.JSON_UTF_8)

assertAll {
assertThat(chaptersBuilder).prop(EpisodePodcastindexChaptersBuilder::hasEnoughDataToBuild).isTrue()

assertThat(chaptersBuilder.build()).isNotNull().all {
prop(Chapters::url).isEqualTo("https://example.com/episode/chapters.json")
prop(Chapters::type).isEqualTo("text/json+chapters")
prop(Chapters::type).isEqualTo(MediaType.JSON_UTF_8)
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/test/kotlin/dev/stalla/model/episode/EpisodeFixtures.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package dev.stalla.model.episode

import com.google.common.net.MediaType
import dev.stalla.dateTime
import dev.stalla.model.Episode
import dev.stalla.model.HrefOnlyImage
Expand Down Expand Up @@ -148,5 +149,5 @@ internal fun anEpisodePodcastindexSoundbite(

internal fun anEpisodePodcastindexChapters(
url: String = "episode podcastindex chapters url",
type: String = "episode podcastindex chapters type"
type: MediaType = MediaType.JSON_UTF_8.withoutParameters()
) = Chapters(url, type)
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import assertk.assertions.isEqualTo
import assertk.assertions.isNotNull
import assertk.assertions.isNull
import assertk.assertions.prop
import com.google.common.net.MediaType
import dev.stalla.builder.fake.episode.FakeEpisodeBuilder
import dev.stalla.builder.fake.episode.FakeEpisodePodcastindexBuilder
import dev.stalla.builder.fake.episode.FakeEpisodePodcastindexChaptersBuilder
Expand Down Expand Up @@ -39,7 +40,7 @@ internal class PodcastindexParserTest : NamespaceParserTest() {

private val expectedChaptersBuilder = FakeEpisodePodcastindexChaptersBuilder()
.url("episode podcastindex chapters url")
.type("episode podcastindex chapters type")
.type(MediaType.JSON_UTF_8.withoutParameters())

private val expectedSoundbiteBuilder = FakeEpisodePodcastindexSoundbiteBuilder()
.startTime(StyledDuration.secondsAndFraction(33, 833_000_000))
Expand Down
2 changes: 1 addition & 1 deletion src/test/resources/xml/item.xml
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
<psc:chapter start="00:01:03.856" title="episode podlove simplechapter title 2" href="episode podlove simplechapter href 2" image="episode podlove simplechapter image 2"/>
<psc:chapter start="00:02:12.641" title="episode podlove simplechapter title 3" href="episode podlove simplechapter href 3" image="episode podlove simplechapter image 3"/>
</psc:chapters>
<podcast:chapters url="episode podcastindex chapters url" type="episode podcastindex chapters type"/>
<podcast:chapters url="episode podcastindex chapters url" type="application/json"/>
<podcast:soundbite startTime="33.833" duration="60.0">episode podcastindex soundbite title</podcast:soundbite>
<podcast:transcript url="episode podcastindex transcript url" type="text/plain" language="it-IT" rel="episode podcastindex transcript rel"/>
</item>
2 changes: 1 addition & 1 deletion src/test/resources/xml/writer-fixtures.xml
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@
<googleplay:block>yes</googleplay:block>
<podcast:transcript url="episode podcastindex transcript url" type="application/srt" language="it-IT" rel="captions"/>
<podcast:soundbite startTime="1" duration="15.123">episode podcastindex soundbite title</podcast:soundbite>
<podcast:chapters url="episode podcastindex chapters url" type="episode podcastindex chapters type"/>
<podcast:chapters url="episode podcastindex chapters url" type="application/json"/>
</item>
</channel>
</rss>

0 comments on commit ac73ac2

Please sign in to comment.