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

@ -22,9 +22,9 @@ A small library for posting to [Pinboard](https://pinboard.in).
val poster = PinboardPoster("user:TOKEN") val poster = PinboardPoster("user:TOKEN")
poster.addPin("https://www.example.com/foo", "This is a test") poster.addPin("https://example.com/foo", "This is a test")
poster.addPin("https://examples.com", "This is a test", tags = arrayOf("foo", "bar")) poster.addPin("https://example.com", "This is a test", tags = arrayOf("foo", "bar"))
poster.deletePin("https:///www.example.com/bar") poster.deletePin("https://example.com/bar")
``` ```
@ -36,13 +36,11 @@ poster.deletePin("https:///www.example.com/bar")
final PinboardPoster poster = new PinBboardPoster("user:TOKEN"); final PinboardPoster poster = new PinBboardPoster("user:TOKEN");
poster.addPin("https://www.example.com/foo", "This is a test"); poster.addPin("https://example.com/foo", "This is a test");
poster.addPin(new PinConfig.Builder() poster.addPin(new PinConfig.Builder("https://example.com", "This is a test")
.url("https://example.com")
.description("This is a test")
.tags("foo", "bar") .tags("foo", "bar")
.build()); .build());
poster.deletePin("https:///www.example.com/bar"); poster.deletePin("https://example.com/bar");
``` ```
[View Examples](https://github.com/ethauvin/pinboard-poster/blob/master/examples) [View Examples](https://github.com/ethauvin/pinboard-poster/blob/master/examples)

View file

@ -1,10 +1,10 @@
<?xml version='1.0' encoding='UTF-8'?> <?xml version="1.0" ?>
<SmellBaseline> <SmellBaseline>
<ManuallySuppressedIssues/> <ManuallySuppressedIssues></ManuallySuppressedIssues>
<CurrentIssues> <CurrentIssues>
<ID>LongParameterList:PinConfig.kt$PinConfig$( val url: String, val description: String, val extended: String, val tags: Array&lt;out String>, val dt: ZonedDateTime, val replace: Boolean, val shared: Boolean, val toRead: Boolean )</ID> <ID>LongParameterList:PinConfig.kt$PinConfig$( var url: String, var description: String, var extended: String = "", var tags: Array&lt;out String&gt; = emptyArray(), var dt: ZonedDateTime = ZonedDateTime.now(), var replace: Boolean = true, var shared: Boolean = true, var toRead: Boolean = false )</ID>
<ID>LongParameterList:PinboardPoster.kt$PinboardPoster$( url: String, description: String, extended: String = "", vararg tags: String = emptyArray(), dt: ZonedDateTime = ZonedDateTime.now(), replace: Boolean = true, shared: Boolean = true, toRead: Boolean = false )</ID> <ID>LongParameterList:PinboardPoster.kt$PinboardPoster$( url: String, description: String, extended: String = "", vararg tags: String = emptyArray(), dt: ZonedDateTime = ZonedDateTime.now(), replace: Boolean = true, shared: Boolean = true, toRead: Boolean = false )</ID>
<ID>NestedBlockDepth:PinboardPoster.kt$PinboardPoster$private fun executeMethod(method: String, params: Map&lt;String, String>): Boolean</ID> <ID>NestedBlockDepth:PinboardPoster.kt$PinboardPoster$private fun executeMethod(method: String, params: Map&lt;String, String&gt;): Boolean</ID>
<ID>ThrowsCount:PinboardPoster.kt$PinboardPoster$@Throws(IOException::class) internal fun parseMethodResponse(method: String, response: String)</ID> <ID>ThrowsCount:PinboardPoster.kt$PinboardPoster$@Throws(IOException::class) internal fun parseMethodResponse(method: String, response: String)</ID>
<ID>TooManyFunctions:PinConfig.kt$PinConfig$Builder</ID> <ID>TooManyFunctions:PinConfig.kt$PinConfig$Builder</ID>
</CurrentIssues> </CurrentIssues>

View file

@ -1,6 +1,6 @@
bld.downloadExtensionJavadoc=false bld.downloadExtensionJavadoc=false
bld.downloadExtensionSources=true bld.downloadExtensionSources=true
bld.extensions=com.uwyn.rife2:bld-kotlin:0.9.4 bld.extensions=com.uwyn.rife2:bld-kotlin:0.9.7
bld.repositories=MAVEN_LOCAL,MAVEN_CENTRAL,RIFE2_SNAPSHOTS,RIFE2_RELEASES bld.repositories=MAVEN_LOCAL,MAVEN_CENTRAL,RIFE2_SNAPSHOTS,RIFE2_RELEASES
bld.downloadLocation= bld.downloadLocation=
bld.sourceDirectories= bld.sourceDirectories=

View file

@ -31,9 +31,7 @@ public class JavaExample {
if (poster.validate()) { if (poster.validate()) {
// Add Pin // Add Pin
if (poster.addPin(new PinConfig.Builder() if (poster.addPin(new PinConfig.Builder(url, "Testing")
.url(url)
.description("Testing")
.extended("Extra") .extended("Extra")
.tags("test", "java") .tags("test", "java")
.build())) { .build())) {

View file

@ -19,5 +19,5 @@ repositories {
} }
dependencies { dependencies {
implementation 'net.thauvin.erik:pinboard-poster:1.1.1' implementation 'net.thauvin.erik:pinboard-poster:1.1.2-SNAPSHOT'
} }

View file

@ -7,4 +7,4 @@
* in the user guide at https://docs.gradle.org/4.8/userguide/multi_project_builds.html * in the user guide at https://docs.gradle.org/4.8/userguide/multi_project_builds.html
*/ */
rootProject.name = 'samples-java' rootProject.name = 'pinboard-poster-examples-gradle-java'

View file

@ -31,9 +31,7 @@ public class JavaExample {
if (poster.validate()) { if (poster.validate()) {
// Add Pin // Add Pin
if (poster.addPin(new PinConfig.Builder() if (poster.addPin(new PinConfig.Builder(url, "Testing")
.url(url)
.description("Testing")
.extended("Extra") .extended("Extra")
.tags("test", "java") .tags("test", "java")
.build())) { .build())) {

View file

@ -1 +1 @@
pinboard-poster-examples-gradle-kotlin pinboard-post-examples-gradle-kotlin

View file

@ -4,5 +4,5 @@
<component name="PDMPlugin"> <component name="PDMPlugin">
<option name="skipTestSources" value="false" /> <option name="skipTestSources" value="false" />
</component> </component>
<component name="ProjectRootManager" version="2" languageLevel="JDK_11" project-jdk-name="20" project-jdk-type="JavaSDK" /> <component name="ProjectRootManager" version="2" languageLevel="JDK_11" project-jdk-name="17" project-jdk-type="JavaSDK" />
</project> </project>

View file

@ -1 +1 @@
rootProject.name = "samples-kotlin" rootProject.name = "pinboard-post-examples-gradle-kotlin"

View file

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

View file

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