Implemented DSL for deeplinks when creating or updating Bitlinks. Closes #16

This commit is contained in:
Erik C. Thauvin 2024-05-22 20:16:52 -07:00
parent 2efe251b56
commit f1e0b3101c
Signed by: erik
GPG key ID: 776702A6A2DA330E
6 changed files with 457 additions and 0 deletions

View file

@ -31,6 +31,9 @@
package net.thauvin.erik.bitly
import java.time.format.DateTimeFormatter
import java.util.*
/** Provides the constants for this package. */
object Constants {
/** The Bitly API base URL. */
@ -50,4 +53,7 @@ object Constants {
/** True */
const val TRUE = true.toString()
/** ISO Timestamp format **/
val ISO_TIMESTAMP: DateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ssZZ", Locale.US)
}

View file

@ -0,0 +1,86 @@
/*
* CreateDeeplinks.kt
*
* Copyright 2020-2024 Erik C. Thauvin (erik@thauvin.net)
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* Neither the name of this project nor the names of its contributors may be
* used to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package net.thauvin.erik.bitly.deeplinks
import java.util.*
/**
* Configures deeplinks used when creating [Bitlinks][net.thauvin.erik.bitly.Bitlinks].
*
* See the [Bit.ly API](https://dev.bitly.com/api-reference#createFullBitlink) for more information.
*
* @since 2.0
*/
@Suppress("FunctionName", "LocalVariableName")
class CreateDeeplinks {
private val map = mutableMapOf<String, String>()
fun app_id(app_id: String) {
map["app_id"] = app_id
}
fun app_id(): String? = map["app_id"]
fun app_uri_path(app_uri_path: String) {
map["app_uri_path"] = app_uri_path
}
fun app_uri_path(): String? = map["app_uri_path"]
fun install_url(install_url: String) {
map["install_url"] = install_url
}
fun install_url(): String? = map["install_url"]
fun install_type(install_type: InstallType) {
map["install_type"] = install_type.type
}
fun install_type(): InstallType? {
val type = map["install_type"]
if (type != null) {
return InstallType.valueOf(type.uppercase(Locale.getDefault()))
}
return null
}
/**
* Returns `true` if there are defined links.
*/
fun isNotEmpty(): Boolean = map.isNotEmpty()
/**
* Returns the links.
*/
fun links(): Map<String, String> = map
}

View file

@ -0,0 +1,43 @@
/*
* InstallType.kt
*
* Copyright 2020-2024 Erik C. Thauvin (erik@thauvin.net)
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* Neither the name of this project nor the names of its contributors may be
* used to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package net.thauvin.erik.bitly.deeplinks
/**
* Defines the installation types.
*
* @since 2.0
*/
enum class InstallType(val type: String) {
NO_INSTALL("no_install"),
AUTO_INSTALL("auto_install"),
PROMOTE_INSTALL("promote_install")
}

View file

@ -0,0 +1,42 @@
/*
* InstallType.kt
*
* Copyright 2020-2024 Erik C. Thauvin (erik@thauvin.net)
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* Neither the name of this project nor the names of its contributors may be
* used to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package net.thauvin.erik.bitly.deeplinks
/**
* Defines the operating system types.
*
* @since 2.0
*/
enum class Os(val type: String) {
IOS("ios"),
ANDROID("android")
}

View file

@ -0,0 +1,150 @@
/*
* CreateDeeplinks.kt
*
* Copyright 2020-2024 Erik C. Thauvin (erik@thauvin.net)
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* Neither the name of this project nor the names of its contributors may be
* used to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package net.thauvin.erik.bitly.deeplinks
import net.thauvin.erik.bitly.Constants
import java.time.ZonedDateTime
import java.util.*
/**
* Configures deeplinks used when updating [Bitlinks][net.thauvin.erik.bitly.Bitlinks].
*
* See the [Bit.ly API](https://dev.bitly.com/api-reference#updateBitlink) for more information.
*
* @since 2.0
*/
@Suppress("FunctionName", "LocalVariableName")
class UpdateDeeplinks {
private val map = mutableMapOf<String, String>()
fun guid(guid: String) {
map["guid"] = guid
}
fun guid(): String? = map["guid"]
fun bitlink(bitlink: String) {
map["bitlink"] = bitlink
}
fun bitlink(): String? = map["bitlink"]
fun app_uri_path(app_uri_path: String) {
map["app_uri_path"] = app_uri_path
}
fun app_uri_path(): String? = map["app_uri_path"]
fun install_url(install_url: String) {
map["install_url"] = install_url
}
fun install_url(): String? = map["install_url"]
fun app_guid(app_guid: String) {
map["app_guid"] = app_guid
}
fun app_guid(): String? = map["app_guid"]
fun os(os: Os) {
map["os"] = os.type
}
fun os(): Os? {
val os = map["os"]
if (os != null) {
return Os.valueOf(os.uppercase(Locale.getDefault()))
}
return null
}
fun install_type(install_type: InstallType) {
map["install_type"] = install_type.type
}
fun install_type(): InstallType? {
val type = map["install_type"]
if (type != null) {
return InstallType.valueOf(type.uppercase(Locale.getDefault()))
}
return null
}
/**
* ISO timestamp.
*/
fun created(created: String) {
map["created"] = created
}
/**
* ISO timestamp.
*/
fun created(created: ZonedDateTime) {
map["created"] = Constants.ISO_TIMESTAMP.format(created)
}
fun created(): String? = map["created"]
/**
* ISO timestamp.
*/
fun modified(modified: String) {
map["modified"] = modified
}
/**
* ISO timestamp.
*/
fun modified(modified: ZonedDateTime) {
map["modified"] = Constants.ISO_TIMESTAMP.format(modified)
}
fun modified(): String? = map["modified"]
fun brand_guid(brand_guid: String) {
map["brand_guid"] = brand_guid
}
fun brand_guid(): String? = map["brand_guid"]
/**
* Returns `true` if there are defined links.
*/
fun isNotEmpty(): Boolean = map.isNotEmpty()
/**
* Returns the links.
*/
fun links(): Map<String, String> = map
}

View file

@ -0,0 +1,130 @@
/*
* CreateDeeplinksTest.kt
*
* Copyright 2020-2024 Erik C. Thauvin (erik@thauvin.net)
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* Neither the name of this project nor the names of its contributors may be
* used to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package net.thauvin.erik.bitly.deeplinks
import assertk.assertThat
import assertk.assertions.contains
import assertk.assertions.doesNotContain
import assertk.assertions.isEqualTo
import assertk.assertions.isNull
import org.json.JSONObject
import org.junit.Test
import java.time.ZoneId
import java.time.ZonedDateTime
class DeeplinksTest {
@Test
fun `create test`() {
val deeplinks = CreateDeeplinks().apply {
app_uri_path("app_uri_path")
install_type(InstallType.NO_INSTALL)
}
assertThat(deeplinks.install_url()).isNull()
deeplinks.install_url("install_url")
assertThat(deeplinks.app_uri_path()).isEqualTo("app_uri_path")
assertThat(deeplinks.install_type()).isEqualTo(InstallType.NO_INSTALL)
assertThat(JSONObject(deeplinks.links()).toString()).isEqualTo(
"""
{"app_uri_path":"app_uri_path","install_type":"no_install","install_url":"install_url"}
""".trimIndent()
)
deeplinks.install_type(InstallType.PROMOTE_INSTALL)
deeplinks.app_id("app_id")
assertThat(deeplinks.app_id()).isEqualTo("app_id")
assertThat(JSONObject(deeplinks.links()).toString()).apply {
doesNotContain(InstallType.NO_INSTALL.type)
contains(InstallType.PROMOTE_INSTALL.type)
contains("\"app_id\":\"app_id\"")
}
}
@Test
fun `update test`() {
val deeplinks = UpdateDeeplinks().apply {
app_guid("app_guid")
os(Os.IOS)
install_type(InstallType.NO_INSTALL)
guid("guid")
install_url("install_url")
app_uri_path("app_uri_path")
created("created")
modified("modified")
}
assertThat(deeplinks.brand_guid()).isNull()
deeplinks.brand_guid("brand_guid")
assertThat(deeplinks.app_uri_path()).isEqualTo("app_uri_path")
assertThat(deeplinks.install_url()).isEqualTo("install_url")
assertThat(deeplinks.os()).isEqualTo(Os.IOS)
assertThat(deeplinks.install_type()).isEqualTo(InstallType.NO_INSTALL)
assertThat(deeplinks.app_guid()).isEqualTo("app_guid")
assertThat(deeplinks.modified()).isEqualTo("modified")
assertThat(deeplinks.brand_guid()).isEqualTo("brand_guid")
assertThat(JSONObject(deeplinks.links()).toString()).isEqualTo(
"""
{"app_guid":"app_guid","install_url":"install_url","os":"ios","app_uri_path":"app_uri_path","created":"created","brand_guid":"brand_guid","guid":"guid","modified":"modified","install_type":"no_install"}
""".trimIndent()
)
deeplinks.install_type(InstallType.PROMOTE_INSTALL)
deeplinks.os(Os.ANDROID)
deeplinks.bitlink("bitlink")
val zdt = ZonedDateTime.of(1997, 8, 29, 2, 14, 0, 0, ZoneId.of("US/Eastern"))
deeplinks.modified(zdt)
deeplinks.created(zdt)
assertThat(deeplinks.bitlink()).isEqualTo("bitlink")
assertThat(deeplinks.created()).isEqualTo("1997-08-29T02:14:00-0400")
assertThat(deeplinks.modified()).isEqualTo("1997-08-29T02:14:00-0400")
assertThat(JSONObject(deeplinks.links()).toString()).apply {
doesNotContain(InstallType.NO_INSTALL.type)
contains(InstallType.PROMOTE_INSTALL.type)
doesNotContain(Os.IOS.type)
contains("\"os\":\"android\"")
contains("\"bitlink\":\"bitlink\"")
}
}
}