From 8119a56dd4de61c60ffb5f2cdd8be51be3a60ecc Mon Sep 17 00:00:00 2001 From: Wojtek Zieba Date: Wed, 29 Nov 2023 19:11:14 +0100 Subject: [PATCH] refactor: migrate `ParselyMetadata` and `ParselyVideoMetadata` to kotlin --- .../parsely/parselyandroid/ParselyMetadata.kt | 98 ++++++++----------- .../parselyandroid/ParselyVideoMetadata.kt | 69 +++++-------- 2 files changed, 66 insertions(+), 101 deletions(-) diff --git a/parsely/src/main/java/com/parsely/parselyandroid/ParselyMetadata.kt b/parsely/src/main/java/com/parsely/parselyandroid/ParselyMetadata.kt index 031bd14a..7f024881 100644 --- a/parsely/src/main/java/com/parsely/parselyandroid/ParselyMetadata.kt +++ b/parsely/src/main/java/com/parsely/parselyandroid/ParselyMetadata.kt @@ -1,80 +1,60 @@ -package com.parsely.parselyandroid; - -import androidx.annotation.Nullable; - -import java.util.ArrayList; -import java.util.HashMap; -import java.util.Map; +package com.parsely.parselyandroid /** * 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. */ -public class ParselyMetadata { - ArrayList authors, tags; - String link, section, thumbUrl, title; - long publicationDateMilliseconds; - - /** - * 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 publicationDateMilliseconds The date this piece of content was published. - */ - public ParselyMetadata( - @Nullable ArrayList authors, - @Nullable String link, - @Nullable String section, - @Nullable ArrayList tags, - @Nullable String thumbUrl, - @Nullable String title, - long publicationDateMilliseconds - ) { - this.authors = authors; - this.link = link; - this.section = section; - this.tags = tags; - this.thumbUrl = thumbUrl; - this.title = title; - this.publicationDateMilliseconds = publicationDateMilliseconds; - } - +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 publicationDateMilliseconds The date this piece of content was published. + */( + private val authors: ArrayList?, + @JvmField internal val link: String?, + private val section: String?, + private val tags: ArrayList?, + private val thumbUrl: String?, + private val title: String?, + private val publicationDateMilliseconds: Long +) { /** * Turn this object into a Map * * @return a Map object representing the metadata. */ - Map toMap() { - Map output = new HashMap<>(); - if (this.authors != null) { - output.put("authors", this.authors); + open fun toMap(): Map? { + val output: MutableMap = HashMap() + if (authors != null) { + output["authors"] = authors } - if (this.link != null) { - output.put("link", this.link); + if (link != null) { + output["link"] = link } - if (this.section != null) { - output.put("section", this.section); + if (section != null) { + output["section"] = section } - if (this.tags != null) { - output.put("tags", this.tags); + if (tags != null) { + output["tags"] = tags } - if (this.thumbUrl != null) { - output.put("thumb_url", this.thumbUrl); + if (thumbUrl != null) { + output["thumb_url"] = thumbUrl } - if (this.title != null) { - output.put("title", this.title); + if (title != null) { + output["title"] = title } - output.put("pub_date_tmsp", publicationDateMilliseconds / 1000); - return output; + output["pub_date_tmsp"] = publicationDateMilliseconds / 1000 + return output } } - diff --git a/parsely/src/main/java/com/parsely/parselyandroid/ParselyVideoMetadata.kt b/parsely/src/main/java/com/parsely/parselyandroid/ParselyVideoMetadata.kt index 0718ffba..3e02be83 100644 --- a/parsely/src/main/java/com/parsely/parselyandroid/ParselyVideoMetadata.kt +++ b/parsely/src/main/java/com/parsely/parselyandroid/ParselyVideoMetadata.kt @@ -1,53 +1,38 @@ -package com.parsely.parselyandroid; - -import androidx.annotation.NonNull; -import androidx.annotation.Nullable; - -import java.util.ArrayList; -import java.util.Calendar; -import java.util.Map; +package com.parsely.parselyandroid /** * ParselyMetadata for video content. */ -public class ParselyVideoMetadata extends ParselyMetadata { - - int durationSeconds; - - /** - * 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 publicationDateMilliseconds The timestamp in milliseconds this video was published. - * @param durationSeconds Duration of the video in seconds. Required. - */ - public ParselyVideoMetadata( - @Nullable ArrayList authors, - @NonNull String videoId, - @Nullable String section, - @Nullable ArrayList tags, - @Nullable String thumbUrl, - @Nullable String title, - long publicationDateMilliseconds, - int durationSeconds - ) { - super(authors, videoId, section, tags, thumbUrl, title, publicationDateMilliseconds); - this.durationSeconds = durationSeconds; - } - +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 publicationDateMilliseconds The timestamp in milliseconds this video was published. + * @param durationSeconds Duration of the video in seconds. Required. + */( + authors: ArrayList?, + videoId: String, + section: String?, + tags: ArrayList?, + thumbUrl: String?, + title: String?, + publicationDateMilliseconds: Long, + @JvmField internal val durationSeconds: Int +) : ParselyMetadata(authors, videoId, section, tags, thumbUrl, title, publicationDateMilliseconds) { /** * Turn this object into a Map * * @return a Map object representing the metadata. */ - Map toMap() { - Map output = super.toMap(); - output.put("duration", this.durationSeconds); - return output; + override fun toMap(): Map? { + val output = super.toMap()?.toMutableMap() + output?.put("duration", durationSeconds) + return output } }