Reworked PinConfig with required parameters, etc.

This commit is contained in:
Erik C. Thauvin 2024-05-25 15:34:40 -07:00
parent 0f9606391e
commit 9036a32239
Signed by: erik
GPG key ID: 776702A6A2DA330E
12 changed files with 94 additions and 57 deletions

View file

@ -37,49 +37,86 @@ import java.time.ZonedDateTime
* Provides a builder to add a pin.
*
* Supports of all the [Pinboard API Parameters](https://pinboard.in/api/#posts_add).
*
* @param url The URL of the bookmark.
* @param description The title of the bookmark.
*/
class PinConfig private constructor(
val url: String,
val description: String,
val extended: String,
val tags: Array<out String>,
val dt: ZonedDateTime,
val replace: Boolean,
val shared: Boolean,
val toRead: Boolean
class PinConfig @JvmOverloads constructor(
var url: String,
var description: String,
var extended: String = "",
var tags: Array<out String> = emptyArray(),
var dt: ZonedDateTime = ZonedDateTime.now(),
var replace: Boolean = true,
var shared: Boolean = true,
var toRead: Boolean = false
) {
constructor(builder: Builder) : this(builder.url, builder.description) {
extended = builder.extended
tags = builder.tags
dt = builder.dt
replace = builder.replace
shared = builder.shared
toRead = builder.toRead
}
/**
* Configures the parameters to add a pin.
*
* @param url The URL of the bookmark.
* @param description The title of the bookmark.
*/
data class Builder(
private var url: String = "",
private var description: String = "",
private var extended: String = "",
private var tags: Array<out String> = emptyArray(),
private var dt: ZonedDateTime = ZonedDateTime.now(),
private var replace: Boolean = true,
private var shared: Boolean = true,
private var toRead: Boolean = false
) {
data class Builder(var url: String, var description: String) {
var extended: String = ""
var tags: Array<out String> = emptyArray()
var dt: ZonedDateTime = ZonedDateTime.now()
var replace: Boolean = true
var shared: Boolean = true
var toRead: Boolean = false
/**
* The URL of the bookmark.
*/
fun url(url: String) = apply { this.url = url }
/**
* The title of the bookmark.
*/
fun description(description: String) = apply { this.description = description }
/**
* The description of the bookmark.
*/
fun extended(extended: String) = apply { this.extended = extended }
/**
* A list of up to 100 tags.
*/
fun tags(vararg tag: String) = apply { this.tags = tag }
/**
* The creation time of the bookmark.
*/
fun dt(datetime: ZonedDateTime) = apply { this.dt = datetime }
/**
* Replace any existing bookmark with the specified URL. Default `true`.
*/
fun replace(replace: Boolean) = apply { this.replace = replace }
/**
* Make bookmark public. Default is `true`.
*/
fun shared(shared: Boolean) = apply { this.shared = shared }
/**
* Mark the bookmark as unread. Default is `false`.
*/
fun toRead(toRead: Boolean) = apply { this.toRead = toRead }
fun build() = PinConfig(
url,
description,
extended,
tags,
dt,
replace,
shared,
toRead
)
/**
* Builds a new comment configuration.
*/
fun build() = PinConfig(this)
override fun equals(other: Any?): Boolean {
if (this === other) return true

View file

@ -44,11 +44,13 @@ import kotlin.test.assertFalse
import kotlin.test.assertTrue
class PinboardPosterTest {
private val url = "http://www.example.com/?random=" + (1000..10000).random()
private val url = randomUrl()
private val desc = "This is a test."
private val localProps = Paths.get("local.properties")
private val isCi = "true" == System.getenv("CI")
private fun randomUrl(): String = "http://www.example.com/?random=" + (1000..10000).random()
@Test
fun testAddPin() {
var poster = PinboardPoster("")
@ -83,7 +85,7 @@ class PinboardPosterTest {
assertTrue(poster.validate(), "validate()")
var config = PinConfig.Builder().url(url).description(desc).extended("extra")
var config = PinConfig.Builder(url, desc).extended("extra")
assertTrue(poster.addPin(config.build()), "apiToken: ${Constants.ENV_API_TOKEN}")
@ -99,13 +101,17 @@ class PinboardPosterTest {
assertTrue(e.message!!.contains("item already exists"))
}
config = config.replace(true).toRead(true)
config = config.description("Yet another test.").replace(true).toRead(true)
assertTrue(poster.addPin(config.build()), "toRead(true)")
config = config.dt(ZonedDateTime.now())
assertTrue(poster.addPin(config.build()), "dt(now)")
assertTrue(poster.deletePin(url), "deletePin($url)")
config = config.url(randomUrl())
assertTrue(poster.addPin(config.build()), "add($url)")
assertTrue(poster.deletePin(config.url), "delete($url)")
}
@Test