Added ShortenConfig && LookupConfig
This commit is contained in:
parent
63645f7cf4
commit
e20934fc08
15 changed files with 138 additions and 171 deletions
|
@ -75,7 +75,7 @@ class Isgd private constructor() {
|
|||
*/
|
||||
@JvmStatic
|
||||
@Throws(IsgdException::class)
|
||||
fun lookup(config: Config): String {
|
||||
fun lookup(config: LookupConfig): String {
|
||||
return lookup(
|
||||
config.shorturl,
|
||||
config.callback,
|
||||
|
@ -89,9 +89,10 @@ class Isgd private constructor() {
|
|||
*
|
||||
* See the [is.gd API](https://is.gd/apilookupreference.php).
|
||||
*
|
||||
* @param The shorturl parameter is the shortened is.gd URL that you want to look up. You can either submit the
|
||||
* full address (e.g. https://is.gd/example) or only the unique part (e.g. example). The address you submit
|
||||
* should be properly formed; the API lookup function is not guaranteed to handle malformed URLs the same way as when you visit them manually.
|
||||
* @param shorturl The shorturl parameter is the shortened is.gd URL that you want to look up. You can either
|
||||
* submit the full address (e.g. `https://is.gd/example`) or only the unique part (e.g. `example`). The address
|
||||
* you submit should be properly formed; the API lookup function is not guaranteed to handle malformed URLs the
|
||||
* same way as when you visit them manually.
|
||||
* @param callback The callback parameter is used to specify a callback function to wrap the returned data in
|
||||
* when using JSON format. This can be useful when working with cross domain data. Even when using JSON format
|
||||
* this parameter is optional.
|
||||
|
@ -128,7 +129,7 @@ class Isgd private constructor() {
|
|||
*/
|
||||
@JvmStatic
|
||||
@Throws(IsgdException::class)
|
||||
fun shorten(config: Config): String {
|
||||
fun shorten(config: ShortenConfig): String {
|
||||
return shorten(
|
||||
config.url,
|
||||
config.shorturl,
|
||||
|
|
84
src/main/kotlin/net/thauvin/erik/isgd/LookupConfig.kt
Normal file
84
src/main/kotlin/net/thauvin/erik/isgd/LookupConfig.kt
Normal file
|
@ -0,0 +1,84 @@
|
|||
/*
|
||||
* LookupConfig.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.isgd
|
||||
|
||||
/**
|
||||
* Provides a builder to lookup an is.gd shortlink.
|
||||
*/
|
||||
class LookupConfig private constructor(builder: Builder) {
|
||||
val shorturl: String = builder.shorturl
|
||||
val callback: String = builder.callback
|
||||
val format: Format = builder.format
|
||||
val isVgd: Boolean = builder.isVgd
|
||||
|
||||
/**
|
||||
* Configures the parameters to lookup an is.gd shortlink.
|
||||
*
|
||||
* See the [is.gd Lookup]() API.
|
||||
*/
|
||||
data class Builder(var shorturl: String) {
|
||||
var callback: String = ""
|
||||
var format: Format = Format.SIMPLE
|
||||
var isVgd: Boolean = false
|
||||
|
||||
/**
|
||||
* The shorturl parameter is the shortened is.gd URL that you want to look up. You can either submit the full
|
||||
* address (e.g. `https://is.gd/example`) or only the unique part (e.g. `example`). The address you submit
|
||||
* should be properly formed; the API lookup function is not guaranteed to handle malformed URLs the same way
|
||||
* as when you visit them manually.
|
||||
*/
|
||||
fun shorturl(shorturl: String): Builder = apply { this.shorturl = shorturl }
|
||||
|
||||
/**
|
||||
* The callback parameter is used to specify a callback function to wrap the returned data in
|
||||
* when using JSON format. This can be useful when working with cross domain data. Even when using JSON format
|
||||
* this parameter is optional.
|
||||
*/
|
||||
fun callback(callback: String): Builder = apply { this.callback = callback }
|
||||
|
||||
/**
|
||||
* The format parameter determines what format is.gd uses to send output back to you (e.g. to
|
||||
* tell you what your new shortened URL is or if an error has occurred).
|
||||
*/
|
||||
fun format(format: Format): Builder = apply { this.format = format }
|
||||
|
||||
/**
|
||||
* Shorten using the `v.gd` domain.
|
||||
*/
|
||||
fun isVgd(isVgd: Boolean): Builder = apply { this.isVgd = isVgd }
|
||||
|
||||
/**
|
||||
* Builds a new configuration.
|
||||
*/
|
||||
fun build() = LookupConfig(this)
|
||||
}
|
||||
}
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Config.kt
|
||||
* ShortenConfig.kt
|
||||
*
|
||||
* Copyright 2020-2024 Erik C. Thauvin (erik@thauvin.net)
|
||||
*
|
||||
|
@ -32,9 +32,9 @@
|
|||
package net.thauvin.erik.isgd
|
||||
|
||||
/**
|
||||
* Provides a builder to create/lookup an is.gd shortlink.
|
||||
* Provides a builder to create an is.gd shortlink.
|
||||
*/
|
||||
class Config private constructor(builder: Builder) {
|
||||
class ShortenConfig private constructor(builder: Builder) {
|
||||
val url: String = builder.url
|
||||
val shorturl: String = builder.shorturl
|
||||
val callback: String = builder.callback
|
||||
|
@ -43,16 +43,15 @@ class Config private constructor(builder: Builder) {
|
|||
val isVgd: Boolean = builder.isVgd
|
||||
|
||||
/**
|
||||
* Configures the parameters to create/lookup an is.gd shortlink.
|
||||
* Configures the parameters to create an is.gd shortlink.
|
||||
*
|
||||
* See the [is.gd Shortening](https://is.gd/apishorteningreference.php) or
|
||||
* [is.gd Lookup](https://is.gd/apilookupreference.php) APIs.
|
||||
* See the [is.gd Shortening](https://is.gd/apishorteningreference.php) API.
|
||||
*/
|
||||
data class Builder(var url: String) {
|
||||
var shorturl: String = "",
|
||||
var callback: String = "",
|
||||
var logstats: Boolean = false,
|
||||
var format: Format = Format.SIMPLE,
|
||||
var shorturl: String = ""
|
||||
var callback: String = ""
|
||||
var logstats: Boolean = false
|
||||
var format: Format = Format.SIMPLE
|
||||
var isVgd: Boolean = false
|
||||
|
||||
/**
|
||||
|
@ -67,7 +66,7 @@ class Config private constructor(builder: Builder) {
|
|||
* short URL might already be taken (this is very often the case with common words) so if you're using this
|
||||
* option be prepared to respond to an error and get an alternative choice from your app's user.
|
||||
*/
|
||||
fun shortUrl(shortUrl: String): Builder = apply { this.shorturl = shortUrl }
|
||||
fun shorturl(shorturl: String): Builder = apply { this.shorturl = shorturl }
|
||||
|
||||
/**
|
||||
* The callback parameter is used to specify a callback function to wrap the returned data in
|
||||
|
@ -85,7 +84,7 @@ class Config private constructor(builder: Builder) {
|
|||
* parameter out of your API call if you don't require statistics on usage. See the
|
||||
* [usage limits page](https://is.gd/usagelimits.php) for more information on this.
|
||||
*/
|
||||
fun logStats(logStats: Boolean): Builder = apply { this.logstats = logStats }
|
||||
fun logstats(logstats: Boolean): Builder = apply { this.logstats = logstats }
|
||||
|
||||
/**
|
||||
* The format parameter determines what format is.gd uses to send output back to you (e.g. to
|
||||
|
@ -101,6 +100,6 @@ class Config private constructor(builder: Builder) {
|
|||
/**
|
||||
* Builds a new configuration.
|
||||
*/
|
||||
fun build() = Config(this)
|
||||
fun build() = ShortenConfig(this)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -66,7 +66,7 @@ class IsgdTest {
|
|||
assertFailsWith(
|
||||
message = "shorten(config:duplicate)",
|
||||
exceptionClass = IsgdException::class,
|
||||
block = { Isgd.shorten(Config.Builder().url(shortUrl).build()) }
|
||||
block = { Isgd.shorten(ShortenConfig.Builder(shortUrl).build()) }
|
||||
)
|
||||
}
|
||||
|
||||
|
@ -84,7 +84,7 @@ class IsgdTest {
|
|||
assertFailsWith(
|
||||
message = "lookup(config:empty)",
|
||||
exceptionClass = IllegalArgumentException::class,
|
||||
block = { Isgd.lookup(Config.Builder().shortUrl("").build()) }
|
||||
block = { Isgd.lookup(LookupConfig.Builder("").build()) }
|
||||
)
|
||||
}
|
||||
|
||||
|
@ -96,10 +96,10 @@ class IsgdTest {
|
|||
|
||||
@Test
|
||||
fun testLookupDefaultConfig() {
|
||||
assertEquals(url, Isgd.lookup(Config.Builder().shortUrl(shortUrl).build()), "lookup(config)")
|
||||
assertEquals(url, Isgd.lookup(LookupConfig.Builder(shortUrl).build()), "lookup(config)")
|
||||
assertEquals(
|
||||
url, Isgd.lookup(
|
||||
Config.Builder().shortUrl(shortVgdUrl).isVgd(true).build()
|
||||
LookupConfig.Builder(shortVgdUrl).isVgd(true).build()
|
||||
), "lookup(config:isVgd)"
|
||||
)
|
||||
}
|
||||
|
@ -119,12 +119,12 @@ class IsgdTest {
|
|||
fun testLookupJsonConfig() {
|
||||
assertEquals(
|
||||
"{ \"url\": \"$url\" }",
|
||||
Isgd.lookup(Config.Builder().shortUrl(shortUrl).format(Format.JSON).build()), "lookup(config)"
|
||||
Isgd.lookup(LookupConfig.Builder(shortUrl).format(Format.JSON).build()), "lookup(config)"
|
||||
)
|
||||
|
||||
assertEquals(
|
||||
"test({ \"url\": \"$url\" });",
|
||||
Isgd.lookup(Config.Builder().shortUrl(shortUrl).callback("test").format(Format.JSON).build()),
|
||||
Isgd.lookup(LookupConfig.Builder(shortUrl).callback("test").format(Format.JSON).build()),
|
||||
"lookup(config:callback)"
|
||||
)
|
||||
}
|
||||
|
@ -141,7 +141,7 @@ class IsgdTest {
|
|||
fun testLookupXmlConfig() {
|
||||
assertEquals(
|
||||
"<?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(LookupConfig.Builder(shortUrl).format(Format.XML).build()),
|
||||
"lookup(config:xml)"
|
||||
)
|
||||
}
|
||||
|
@ -166,13 +166,13 @@ class IsgdTest {
|
|||
assertFailsWith(
|
||||
message = "shorten(config:empty)",
|
||||
exceptionClass = IllegalArgumentException::class,
|
||||
block = { Isgd.shorten(Config.Builder().url("").build()) }
|
||||
block = { Isgd.shorten(ShortenConfig.Builder("").build()) }
|
||||
)
|
||||
|
||||
assertFailsWith(
|
||||
message = "shorten(config:shorturl)",
|
||||
exceptionClass = IsgdException::class,
|
||||
block = { Isgd.shorten(Config.Builder(url).shortUrl("test").build()) }
|
||||
block = { Isgd.shorten(ShortenConfig.Builder(url).shorturl("test").build()) }
|
||||
)
|
||||
}
|
||||
|
||||
|
@ -185,13 +185,13 @@ class IsgdTest {
|
|||
|
||||
@Test
|
||||
fun testShortenDefaultConfig() {
|
||||
assertEquals(shortUrl, Isgd.shorten(Config.Builder().url(url).build()), "shorten(config:url)")
|
||||
assertEquals(shortUrl, Isgd.shorten(ShortenConfig.Builder(url).build()), "shorten(config:url)")
|
||||
assertEquals(
|
||||
shortVgdUrl,
|
||||
Isgd.shorten(Config.Builder().url(url).isVgd(true).build()),
|
||||
Isgd.shorten(ShortenConfig.Builder(url).isVgd(true).build()),
|
||||
"shorten(config:isVgd)"
|
||||
)
|
||||
assertThat(Isgd.shorten(Config.Builder().url(url).logStats(true).build()), "shorten(config:callback)")
|
||||
assertThat(Isgd.shorten(ShortenConfig.Builder(url).logstats(true).build()), "shorten(config:callback)")
|
||||
.matches("https://is.gd/\\w{6}".toRegex())
|
||||
}
|
||||
|
||||
|
@ -209,11 +209,11 @@ class IsgdTest {
|
|||
fun testShortenJsonConfig() {
|
||||
assertEquals(
|
||||
"{ \"shorturl\": \"$shortUrl\" }",
|
||||
Isgd.shorten(Config.Builder().url(url).format(Format.JSON).build()), "shorten(config:json)"
|
||||
Isgd.shorten(ShortenConfig.Builder(url).format(Format.JSON).build()), "shorten(config:json)"
|
||||
)
|
||||
assertEquals(
|
||||
"test({ \"shorturl\": \"$shortUrl\" });",
|
||||
Isgd.shorten(Config.Builder().url(url).callback("test").format(Format.JSON).build()),
|
||||
Isgd.shorten(ShortenConfig.Builder(url).callback("test").format(Format.JSON).build()),
|
||||
"shorten(config:callback,json)"
|
||||
)
|
||||
}
|
||||
|
@ -232,7 +232,7 @@ class IsgdTest {
|
|||
assertEquals(
|
||||
"<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>" +
|
||||
"<output><shorturl>$shortUrl</shorturl></output>",
|
||||
Isgd.shorten(Config.Builder().url(url).format(Format.XML).build()),
|
||||
Isgd.shorten(ShortenConfig.Builder(url).format(Format.XML).build()),
|
||||
"shorten(config:xml)"
|
||||
)
|
||||
}
|
||||
|
@ -244,7 +244,7 @@ class IsgdTest {
|
|||
|
||||
@Test
|
||||
fun testShortenWebConfig() {
|
||||
assertThat(Isgd.shorten(Config.Builder().url(url).format(Format.WEB).build()), "shorten(config:web)")
|
||||
assertThat(Isgd.shorten(ShortenConfig.Builder(url).format(Format.WEB).build()), "shorten(config:web)")
|
||||
.contains(shortUrl)
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue