Made builder fields private

This commit is contained in:
Erik C. Thauvin 2023-09-25 08:33:14 -07:00
parent ca6ac25013
commit f6f3c82513
4 changed files with 21 additions and 19 deletions

View file

@ -81,9 +81,9 @@ To make it easier to use the library with Java, configuration builders are avail
```java ```java
var config = new Config.Builder() var config = new Config.Builder()
.url("https://www.example.com/") .url("https://www.example.com/")
.shorturl("foobar") .shortUrl("foobar")
.callback("test") .callback("test")
.logstats(true) .logStats(true)
.format(Format.JSON) .format(Format.JSON)
.build(); .build();

View file

@ -12,7 +12,7 @@ public final class IsgdSample {
try { try {
if (arg.contains("is.gd")) { if (arg.contains("is.gd")) {
System.out.println(arg + " <-- " + Isgd.lookup(arg)); System.out.println(arg + " <-- " + Isgd.lookup(arg));
System.out.print(Isgd.lookup(new Config.Builder().shorturl(arg).format(Format.WEB).build())); System.out.print(Isgd.lookup(new Config.Builder().shortUrl(arg).format(Format.WEB).build()));
} else { } else {
System.out.println(arg + " --> " + Isgd.shorten(arg)); System.out.println(arg + " --> " + Isgd.shorten(arg));
} }

View file

@ -44,19 +44,21 @@ class Config private constructor(
) { ) {
/** /**
* Configures the parameters to create/lookup an is.gd shortlink. * Configures the parameters to create/lookup an is.gd shortlink.
*
* See the [is.gd API](https://is.gd/apishorteningreference.php).
*/ */
data class Builder( data class Builder(
var url: String = "", private var url: String = "",
var shorturl: String = "", private var shorturl: String = "",
var callback: String = "", private var callback: String = "",
var logstats: Boolean = false, private var logstats: Boolean = false,
var format: Format = Format.SIMPLE, private var format: Format = Format.SIMPLE,
var isVgd: Boolean = false private var isVgd: Boolean = false
) { ) {
fun url(url: String) = apply { this.url = url } fun url(url: String) = apply { this.url = url }
fun shorturl(shorturl: String) = apply { this.shorturl = shorturl } fun shortUrl(shortUrl: String) = apply { this.shorturl = shortUrl }
fun callback(callback: String) = apply { this.callback = callback } fun callback(callback: String) = apply { this.callback = callback }
fun logstats(logstats: Boolean) = apply { this.logstats = logstats } fun logStats(logStats: Boolean) = apply { this.logstats = logStats }
fun format(format: Format) = apply { this.format = format } fun format(format: Format) = apply { this.format = format }
fun isVgd(isVgd: Boolean) = apply { this.isVgd = isVgd } fun isVgd(isVgd: Boolean) = apply { this.isVgd = isVgd }

View file

@ -84,7 +84,7 @@ class IsgdTest {
assertFailsWith( assertFailsWith(
message = "lookup(config:empty)", message = "lookup(config:empty)",
exceptionClass = IllegalArgumentException::class, exceptionClass = IllegalArgumentException::class,
block = { Isgd.lookup(Config.Builder().shorturl("").build()) } block = { Isgd.lookup(Config.Builder().shortUrl("").build()) }
) )
} }
@ -96,10 +96,10 @@ class IsgdTest {
@Test @Test
fun testLookupDefaultConfig() { fun testLookupDefaultConfig() {
assertEquals(url, Isgd.lookup(Config.Builder().shorturl(shortUrl).build()), "lookup(config)") assertEquals(url, Isgd.lookup(Config.Builder().shortUrl(shortUrl).build()), "lookup(config)")
assertEquals( assertEquals(
url, Isgd.lookup( url, Isgd.lookup(
Config.Builder().shorturl(shortVgdUrl).isVgd(true).build() Config.Builder().shortUrl(shortVgdUrl).isVgd(true).build()
), "lookup(config:isVgd)" ), "lookup(config:isVgd)"
) )
} }
@ -119,12 +119,12 @@ class IsgdTest {
fun testLookupJsonConfig() { fun testLookupJsonConfig() {
assertEquals( assertEquals(
"{ \"url\": \"$url\" }", "{ \"url\": \"$url\" }",
Isgd.lookup(Config.Builder().shorturl(shortUrl).format(Format.JSON).build()), "lookup(config)" Isgd.lookup(Config.Builder().shortUrl(shortUrl).format(Format.JSON).build()), "lookup(config)"
) )
assertEquals( assertEquals(
"test({ \"url\": \"$url\" });", "test({ \"url\": \"$url\" });",
Isgd.lookup(Config.Builder().shorturl(shortUrl).callback("test").format(Format.JSON).build()), Isgd.lookup(Config.Builder().shortUrl(shortUrl).callback("test").format(Format.JSON).build()),
"lookup(config:callback)" "lookup(config:callback)"
) )
} }
@ -141,7 +141,7 @@ class IsgdTest {
fun testLookupXmlConfig() { fun testLookupXmlConfig() {
assertEquals( assertEquals(
"<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?><output><url>$url</url></output>", "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?><output><url>$url</url></output>",
Isgd.lookup(Config.Builder().shorturl(shortUrl).format(Format.XML).build()), Isgd.lookup(Config.Builder().shortUrl(shortUrl).format(Format.XML).build()),
"lookup(config:xml)" "lookup(config:xml)"
) )
} }
@ -172,7 +172,7 @@ class IsgdTest {
assertFailsWith( assertFailsWith(
message = "shorten(config:shorturl)", message = "shorten(config:shorturl)",
exceptionClass = IsgdException::class, exceptionClass = IsgdException::class,
block = { Isgd.shorten(Config.Builder(url).shorturl("test").build()) } block = { Isgd.shorten(Config.Builder(url).shortUrl("test").build()) }
) )
} }
@ -191,7 +191,7 @@ class IsgdTest {
Isgd.shorten(Config.Builder().url(url).isVgd(true).build()), Isgd.shorten(Config.Builder().url(url).isVgd(true).build()),
"shorten(config:isVgd)" "shorten(config:isVgd)"
) )
assertThat(Isgd.shorten(Config.Builder().url(url).logstats(true).build()), "shorten(config:callback)") assertThat(Isgd.shorten(Config.Builder().url(url).logStats(true).build()), "shorten(config:callback)")
.matches("https://is.gd/\\w{6}".toRegex()) .matches("https://is.gd/\\w{6}".toRegex())
} }