Added pin config builder. Closes #10
This commit is contained in:
parent
045ad0f276
commit
fc77b73399
7 changed files with 225 additions and 31 deletions
112
src/main/kotlin/net/thauvin/erik/pinboard/PinConfig.kt
Normal file
112
src/main/kotlin/net/thauvin/erik/pinboard/PinConfig.kt
Normal file
|
@ -0,0 +1,112 @@
|
|||
/*
|
||||
* PinConfig.kt
|
||||
*
|
||||
* Copyright (c) 2017-2023, Erik C. Thauvin (erik@thauvin.net)
|
||||
* All rights reserved.
|
||||
*
|
||||
* 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.pinboard
|
||||
|
||||
import java.time.ZonedDateTime
|
||||
|
||||
/**
|
||||
* Provides a builder to add a pin.
|
||||
*/
|
||||
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
|
||||
) {
|
||||
/**
|
||||
* Configures the parameters to add a pin.
|
||||
*/
|
||||
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
|
||||
) {
|
||||
fun url(url: String) = apply { this.url = url }
|
||||
fun description(description: String) = apply { this.description = description }
|
||||
fun extended(extended: String) = apply { this.extended = extended }
|
||||
fun tags(vararg tag: String) = apply { this.tags = tag }
|
||||
fun dt(datetime: ZonedDateTime) = apply { this.dt = datetime }
|
||||
fun replace(replace: Boolean) = apply { this.replace = replace }
|
||||
fun shared(shared: Boolean) = apply { this.shared = shared }
|
||||
fun toRead(toRead: Boolean) = apply { this.toRead = toRead }
|
||||
|
||||
fun build() = PinConfig(
|
||||
url,
|
||||
description,
|
||||
extended,
|
||||
tags,
|
||||
dt,
|
||||
replace,
|
||||
shared,
|
||||
toRead
|
||||
)
|
||||
|
||||
override fun equals(other: Any?): Boolean {
|
||||
if (this === other) return true
|
||||
if (javaClass != other?.javaClass) return false
|
||||
|
||||
other as Builder
|
||||
|
||||
if (url != other.url) return false
|
||||
if (description != other.description) return false
|
||||
if (extended != other.extended) return false
|
||||
if (!tags.contentEquals(other.tags)) return false
|
||||
if (dt != other.dt) return false
|
||||
if (replace != other.replace) return false
|
||||
if (shared != other.shared) return false
|
||||
if (toRead != other.toRead) return false
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
override fun hashCode(): Int {
|
||||
var result = url.hashCode()
|
||||
result = 31 * result + description.hashCode()
|
||||
result = 31 * result + extended.hashCode()
|
||||
result = 31 * result + tags.contentHashCode()
|
||||
result = 31 * result + dt.hashCode()
|
||||
result = 31 * result + replace.hashCode()
|
||||
result = 31 * result + shared.hashCode()
|
||||
result = 31 * result + toRead.hashCode()
|
||||
return result
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,7 +1,7 @@
|
|||
/*
|
||||
* PinboardPoster.kt
|
||||
*
|
||||
* Copyright (c) 2017-2022, Erik C. Thauvin (erik@thauvin.net)
|
||||
* Copyright (c) 2017-2023, Erik C. Thauvin (erik@thauvin.net)
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
|
@ -44,6 +44,8 @@ import java.net.MalformedURLException
|
|||
import java.net.URL
|
||||
import java.nio.file.Files
|
||||
import java.nio.file.Path
|
||||
import java.time.ZonedDateTime
|
||||
import java.time.format.DateTimeFormatter
|
||||
import java.util.*
|
||||
import java.util.logging.Level
|
||||
import java.util.logging.Logger
|
||||
|
@ -132,6 +134,24 @@ open class PinboardPoster() {
|
|||
}.build()
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a bookmark to Pinboard
|
||||
*
|
||||
* This method supports of all the [Pinboard API Parameters](https://pinboard.in/api/#posts_add).
|
||||
*/
|
||||
fun addPin(config: PinConfig): Boolean {
|
||||
return addPin(
|
||||
url = config.url,
|
||||
description = config.description,
|
||||
extended = config.extended,
|
||||
tags = config.tags,
|
||||
dt = config.dt,
|
||||
replace = config.replace,
|
||||
shared = config.shared,
|
||||
toRead = config.toRead
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a bookmark to Pinboard.
|
||||
*
|
||||
|
@ -153,8 +173,8 @@ open class PinboardPoster() {
|
|||
url: String,
|
||||
description: String,
|
||||
extended: String = "",
|
||||
tags: String = "",
|
||||
dt: String = "",
|
||||
vararg tags: String = emptyArray(),
|
||||
dt: ZonedDateTime = ZonedDateTime.now(),
|
||||
replace: Boolean = true,
|
||||
shared: Boolean = true,
|
||||
toRead: Boolean = false
|
||||
|
@ -169,8 +189,8 @@ open class PinboardPoster() {
|
|||
"url" to url,
|
||||
"description" to description,
|
||||
"extended" to extended,
|
||||
"tags" to tags,
|
||||
"dt" to dt,
|
||||
"tags" to tags.joinToString(","),
|
||||
"dt" to DateTimeFormatter.ISO_INSTANT.format(dt.withNano(0)),
|
||||
"replace" to yesNo(replace),
|
||||
"shared" to yesNo(shared),
|
||||
"toread" to yesNo(toRead)
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
/*
|
||||
* PinboardPosterTest.kt
|
||||
*
|
||||
* Copyright (c) 2017-2022, Erik C. Thauvin (erik@thauvin.net)
|
||||
* Copyright (c) 2017-2023, Erik C. Thauvin (erik@thauvin.net)
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
|
@ -32,20 +32,20 @@
|
|||
|
||||
package net.thauvin.erik.pinboard
|
||||
|
||||
import org.testng.Assert.assertFalse
|
||||
import org.testng.Assert.assertTrue
|
||||
import org.testng.Assert.expectThrows
|
||||
import org.testng.Assert.*
|
||||
import org.testng.annotations.Test
|
||||
import java.io.IOException
|
||||
import java.nio.file.Files
|
||||
import java.nio.file.Paths
|
||||
import java.util.Properties
|
||||
import java.time.ZonedDateTime
|
||||
import java.util.*
|
||||
import java.util.logging.Level
|
||||
|
||||
class PinboardPosterTest {
|
||||
private val url = "http://www.example.com/?random=" + (1000..10000).random()
|
||||
private val desc = "This is a test."
|
||||
private val localProps = Paths.get("local.properties")
|
||||
private val isCi = "true" == System.getenv("CI")
|
||||
|
||||
@Test
|
||||
fun testAddPin() {
|
||||
|
@ -61,10 +61,42 @@ class PinboardPosterTest {
|
|||
// assertFalse(poster.addPin(url, desc), "apiToken: ${poster.apiToken}")
|
||||
|
||||
poster = PinboardPoster(localProps)
|
||||
poster.logger.level = Level.FINE
|
||||
if (!isCi) {
|
||||
poster.logger.level = Level.FINE
|
||||
}
|
||||
assertTrue(poster.addPin(url, desc), "apiToken: ${Constants.ENV_API_TOKEN}")
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testAddPinConfig() {
|
||||
val poster = PinboardPoster(localProps)
|
||||
if (!isCi) {
|
||||
poster.logger.level = Level.FINE
|
||||
}
|
||||
|
||||
var config = PinConfig.Builder().url(url).description(desc).extended("extra")
|
||||
|
||||
assertTrue(poster.addPin(config.build()), "apiToken: ${Constants.ENV_API_TOKEN}")
|
||||
|
||||
config = config.tags("foo", "bar")
|
||||
assertTrue(poster.addPin(config.build()), "tags(foo,bar)")
|
||||
|
||||
config = config.shared(false)
|
||||
assertTrue(poster.addPin(config.build()), "shared(false)")
|
||||
|
||||
try {
|
||||
assertFalse(poster.addPin(config.replace(false).build()))
|
||||
} catch (e: IOException) {
|
||||
assertTrue(e.message!!.contains("item already exists"))
|
||||
}
|
||||
|
||||
config = config.replace(true).toRead(true)
|
||||
assertTrue(poster.addPin(config.build()), "toRead(true)")
|
||||
|
||||
config = config.dt(ZonedDateTime.now())
|
||||
assertTrue(poster.addPin(config.build()), "dt(now)")
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testDeletePin() {
|
||||
val props = if (Files.exists(localProps)) {
|
||||
|
@ -84,8 +116,9 @@ class PinboardPosterTest {
|
|||
assertFalse(poster.deletePin(url), "apiEndPoint: <blank>")
|
||||
|
||||
poster = PinboardPoster(localProps, Constants.ENV_API_TOKEN)
|
||||
poster.logger.level = Level.FINE
|
||||
|
||||
if (!isCi) {
|
||||
poster.logger.level = Level.FINE
|
||||
}
|
||||
poster.apiEndPoint = Constants.API_ENDPOINT
|
||||
assertTrue(poster.deletePin(url), "apiEndPoint: ${Constants.API_ENDPOINT}")
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue