Skip to content

Commit

Permalink
Merge pull request #96 from Parsely/issue/migrate_metadata_to_kotlin
Browse files Browse the repository at this point in the history
Migrate `ParselyMetadata` and `ParselyVideoMetadata` to Kotlin. Improve API.
  • Loading branch information
wzieba authored Dec 13, 2023
2 parents ee8614e + 67a755b commit 7504512
Show file tree
Hide file tree
Showing 6 changed files with 175 additions and 141 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package com.parsely.parselyandroid

import java.util.Calendar

/**
* Represents post metadata to be passed to Parsely tracking.
*
*
* This class is used to attach a metadata block to a Parse.ly pageview
* request. Pageview metadata is only required for URLs not accessible over the
* internet (i.e. app-only content) or if the customer is using an "in-pixel" integration.
* Otherwise, metadata will be gathered by Parse.ly's crawling infrastructure.
*/
open class ParselyMetadata
/**
* Create a new ParselyMetadata object.
*
* @param authors The names of the authors of the content. Up to 10 authors are accepted.
* @param link A post's canonical url.
* @param section The category or vertical to which this content belongs.
* @param tags User-defined tags for the content. Up to 20 are allowed.
* @param thumbUrl URL at which the main image for this content is located.
* @param title The title of the content.
* @param pubDate The date this piece of content was published.
*/(
private val authors: List<String>? = null,
@JvmField internal val link: String? = null,
private val section: String? = null,
private val tags: List<String>? = null,
private val thumbUrl: String? = null,
private val title: String? = null,
private val pubDate: Calendar? = null
) {
/**
* Turn this object into a Map
*
* @return a Map object representing the metadata.
*/
open fun toMap(): Map<String, Any?>? {
val output: MutableMap<String, Any?> = HashMap()
if (authors != null) {
output["authors"] = authors
}
if (link != null) {
output["link"] = link
}
if (section != null) {
output["section"] = section
}
if (tags != null) {
output["tags"] = tags
}
if (thumbUrl != null) {
output["thumb_url"] = thumbUrl
}
if (title != null) {
output["title"] = title
}
if (pubDate != null) {
output["pub_date_tmsp"] = pubDate.timeInMillis / 1000
}
return output
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.parsely.parselyandroid

import java.util.Calendar

/**
* ParselyMetadata for video content.
*/
class ParselyVideoMetadata
/**
* Create a new ParselyVideoMetadata object.
*
* @param authors The names of the authors of the video. Up to 10 authors are accepted.
* @param videoId Unique identifier for the video. Required.
* @param section The category or vertical to which this video belongs.
* @param tags User-defined tags for the video. Up to 20 are allowed.
* @param thumbUrl URL at which the main image for this video is located.
* @param title The title of the video.
* @param pubDate The timestamp in milliseconds this video was published.
* @param durationSeconds Duration of the video in seconds. Required.
*/(
authors: List<String>? = null,
videoId: String,
section: String? = null,
tags: List<String>? = null,
thumbUrl: String? = null,
title: String? = null,
pubDate: Calendar? = null,
@JvmField internal val durationSeconds: Int
) : ParselyMetadata(authors, videoId, section, tags, thumbUrl, title, pubDate) {
/**
* Turn this object into a Map
*
* @return a Map object representing the metadata.
*/
override fun toMap(): Map<String, Any?>? {
val output = super.toMap()?.toMutableMap()
output?.put("duration", durationSeconds)
return output
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import org.assertj.core.api.Assertions.assertThat
import org.assertj.core.api.MapAssert
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
import org.robolectric.RobolectricTestRunner

internal class EventsBuilderTest {
private lateinit var sut: EventsBuilder
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package com.parsely.parselyandroid

import java.util.Calendar
import kotlin.time.Duration.Companion.seconds
import org.assertj.core.api.Assertions.assertThat
import org.junit.Test

class ParselyMetadataTest {

@Test
fun `given metadata with complete set of data, when converting to map, then the map is as expected`() {
// given
val sut = ParselyMetadata(
authors,
link,
section,
tags,
thumbUrl,
title,
pubDate
)

// when
val map = sut.toMap()

// then
assertThat(map).isEqualTo(expectedParselyMetadataMap)
}

@Test
fun `given video metadata with complete set of data, when converting to map, then the map is as expected`() {
// given
val duration = 12
val sut = ParselyVideoMetadata(
authors,
link,
section,
tags,
thumbUrl,
title,
pubDate,
duration
)

// when
val map = sut.toMap()

// then
assertThat(map).isEqualTo(expectedParselyMetadataMap + ("duration" to duration))
}

companion object {
val authors = arrayListOf("first author", "second author")
val link = "sample link"
val section = "sample section"
val tags = arrayListOf("first tag", "second tag")
val thumbUrl = "sample thumb url"
val title = "sample title"
val pubDate = Calendar.getInstance().apply { set(2023, 0, 1) }

val expectedParselyMetadataMap = mapOf(
"authors" to authors,
"link" to link,
"section" to section,
"tags" to tags,
"thumb_url" to thumbUrl,
"title" to title,
"pub_date_tmsp" to pubDate.timeInMillis / 1000
)
}
}

0 comments on commit 7504512

Please sign in to comment.