From 06ea6b91a2d788902b51e63b248e676f5795ce26 Mon Sep 17 00:00:00 2001 From: "Erik C. Thauvin" Date: Thu, 27 Feb 2020 00:42:09 -0800 Subject: [PATCH] Cleanup. --- src/main/kotlin/net/thauvin/erik/bitly/Bitlinks.kt | 10 +++++----- src/main/kotlin/net/thauvin/erik/bitly/Bitly.kt | 11 ++++++++--- src/test/kotlin/net/thauvin/erik/bitly/BitlyTest.kt | 11 ++++++----- 3 files changed, 19 insertions(+), 13 deletions(-) diff --git a/src/main/kotlin/net/thauvin/erik/bitly/Bitlinks.kt b/src/main/kotlin/net/thauvin/erik/bitly/Bitlinks.kt index 0bf3d37..0231a15 100644 --- a/src/main/kotlin/net/thauvin/erik/bitly/Bitlinks.kt +++ b/src/main/kotlin/net/thauvin/erik/bitly/Bitlinks.kt @@ -48,8 +48,8 @@ class Bitlinks(private val accessToken: String) { * See the [Bit.ly API](https://dev.bitly.com/v4/#operation/expandBitlink) for more information. * * @param bitlink_id The bitlink ID. - * @param isJson Returns the full JSON API response if `true` - * @return THe long URL or JSON API response. + * @param isJson Returns the full JSON response if `true` + * @return The long URL or JSON response, or on error, an empty string/JSON object. */ @JvmOverloads fun expand(bitlink_id: String, isJson: Boolean = false): String { @@ -81,9 +81,9 @@ class Bitlinks(private val accessToken: String) { * * @param long_url The long URL. * @param group_guid The group UID. - * @param domain The domain for the short URL, defaults to `bit.ly`. - * @param isJson Returns the full JSON API response if `true` - * @return THe short URL or JSON API response. + * @param domain The domain for the short URL. + * @param isJson Returns the full JSON response if `true` + * @return The short URL or JSON response, or on error, the [long_url] or an empty JSON object. */ @JvmOverloads fun shorten( diff --git a/src/main/kotlin/net/thauvin/erik/bitly/Bitly.kt b/src/main/kotlin/net/thauvin/erik/bitly/Bitly.kt index b97961a..b43dc84 100644 --- a/src/main/kotlin/net/thauvin/erik/bitly/Bitly.kt +++ b/src/main/kotlin/net/thauvin/erik/bitly/Bitly.kt @@ -50,11 +50,16 @@ enum class Methods { * @constructor Creates new instance. */ open class Bitly() { - /** The API access token. **/ - var accessToken: String = System.getenv(Constants.ENV_ACCESS_TOKEN) ?: Constants.EMPTY + /** The API access token. + * + * See [Generic Access Token](https://bitly.is/accesstoken) or + * [Authentication](https://dev.bitly.com/v4/#section/Authentication). + **/ + var accessToken: String = System.getenv(Constants.ENV_ACCESS_TOKEN) + ?: (System.getProperty(Constants.ENV_ACCESS_TOKEN) ?: Constants.EMPTY) /** - * Creates a new instance using an [API Access Token][accessToken]. + * Creates a new instance using an [API Access Token][Bitly.accessToken]. * * @param accessToken The API access token. */ diff --git a/src/test/kotlin/net/thauvin/erik/bitly/BitlyTest.kt b/src/test/kotlin/net/thauvin/erik/bitly/BitlyTest.kt index 1e9dd7b..b903619 100644 --- a/src/test/kotlin/net/thauvin/erik/bitly/BitlyTest.kt +++ b/src/test/kotlin/net/thauvin/erik/bitly/BitlyTest.kt @@ -47,7 +47,8 @@ class BitlyTest { Bitly() } } - private val blog = "https://erik.thauvin.net/blog" + private val longUrl = "https://erik.thauvin.net/blog" + private val shortUrl = "http://bit.ly/380ojFd" @Before fun before() { @@ -62,7 +63,7 @@ class BitlyTest { if (System.getenv("CI") == "true") { test.accessToken = Constants.EMPTY } - assertEquals(Constants.EMPTY, test.bitlinks().shorten(blog)) + assertEquals(longUrl, test.bitlinks().shorten(longUrl)) } @Test @@ -78,13 +79,13 @@ class BitlyTest { @Test fun `shorten = expand`() { - val shortUrl = bitly.bitlinks().shorten(blog, domain = "bit.ly") - assertEquals(blog, bitly.bitlinks().expand(shortUrl)) + val shortUrl = bitly.bitlinks().shorten(longUrl, domain = "bit.ly") + assertEquals(longUrl, bitly.bitlinks().expand(shortUrl)) } @Test fun `as json`() { - assertTrue(bitly.bitlinks().shorten(blog, isJson = true).startsWith("{\"created_at\":")) + assertTrue(bitly.bitlinks().shorten(longUrl, isJson = true).startsWith("{\"created_at\":")) } @Test