-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #96 from Parsely/issue/migrate_metadata_to_kotlin
Migrate `ParselyMetadata` and `ParselyVideoMetadata` to Kotlin. Improve API.
- Loading branch information
Showing
6 changed files
with
175 additions
and
141 deletions.
There are no files selected for viewing
83 changes: 0 additions & 83 deletions
83
parsely/src/main/java/com/parsely/parselyandroid/ParselyMetadata.java
This file was deleted.
Oops, something went wrong.
64 changes: 64 additions & 0 deletions
64
parsely/src/main/java/com/parsely/parselyandroid/ParselyMetadata.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
56 changes: 0 additions & 56 deletions
56
parsely/src/main/java/com/parsely/parselyandroid/ParselyVideoMetadata.java
This file was deleted.
Oops, something went wrong.
40 changes: 40 additions & 0 deletions
40
parsely/src/main/java/com/parsely/parselyandroid/ParselyVideoMetadata.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 71 additions & 0 deletions
71
parsely/src/test/java/com/parsely/parselyandroid/ParselyMetadataTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
) | ||
} | ||
} |