Skip to content

Commit

Permalink
Converted string functions to extensions.
Browse files Browse the repository at this point in the history
  • Loading branch information
ethauvin committed Mar 2, 2020
1 parent c18ec9f commit b8cec20
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 32 deletions.
62 changes: 34 additions & 28 deletions src/main/kotlin/net/thauvin/erik/bitly/Utils.kt
Expand Up @@ -49,22 +49,9 @@ import java.util.logging.Logger
/** Useful functions. */
open class Utils private constructor() {
companion object {
/** The logger instance. **/
/** The logger instance. */
val logger: Logger by lazy { Logger.getLogger(Bitly::class.java.simpleName) }

/**
* Builds the full API endpoint URL using the [Constants.API_BASE_URL].
*
* @param endPointPath The REST request path. (eg. `/shorten', '/user`)
*/
fun buildEndPointUrl(endPointPath: String): String {
return if (endPointPath.startsWith('/')) {
"${Constants.API_BASE_URL}$endPointPath"
} else {
"${Constants.API_BASE_URL}/$endPointPath"
}
}

/**
* Executes an API call.
*
Expand All @@ -74,10 +61,11 @@ open class Utils private constructor() {
* @param method The submission [Method][Methods].
* @return The response (JSON) from the API.
*/
@JvmOverloads
fun call(
accessToken: String,
endPoint: String,
params: Map<String, String>,
params: Map<String, String> = emptyMap(),
method: Methods = Methods.POST
): String {
var response = Constants.EMPTY
Expand Down Expand Up @@ -153,28 +141,46 @@ open class Utils private constructor() {
return Constants.EMPTY
}

private fun validateCall(accessToken: String, endPoint: String): Boolean {
when {
endPoint.isBlank() -> logger.severe("Please specify a valid API endpoint.")
accessToken.isBlank() -> logger.severe("Please specify a valid API access token.")
else -> return true
}
return false
}

/**
* Validates a URL.
*/
fun validateUrl(url: String): Boolean {
if (url.isNotBlank()) {
fun String.isValidUrl(): Boolean {
if (this.isNotBlank()) {
try {
URL(url)
URL(this)
return true
} catch (e: MalformedURLException) {
logger.log(Level.FINE, "Invalid URL: $url", e)
logger.log(Level.FINE, "Invalid URL: $this", e)
}
}
return false
}

/**
* Removes http(s) scheme from string.
*/
fun String.removeHttp(): String {
return this.replaceFirst(Regex("^[Hh][Tt]{2}[Pp][Ss]?://"), "")
}

/**
* Builds the full API endpoint URL using the [Constants.API_BASE_URL].
*/
fun String.toEndPoint(): String {
return if (this.startsWith('/')) {
"${Constants.API_BASE_URL}$this"
} else {
"${Constants.API_BASE_URL}/$this"
}
}

private fun validateCall(accessToken: String, endPoint: String): Boolean {
when {
endPoint.isBlank() -> logger.severe("Please specify a valid API endpoint.")
accessToken.isBlank() -> logger.severe("Please specify a valid API access token.")
else -> return true
}
return false
}
}
}
21 changes: 17 additions & 4 deletions src/test/kotlin/net/thauvin/erik/bitly/BitlyTest.kt
Expand Up @@ -32,6 +32,9 @@

package net.thauvin.erik.bitly

import net.thauvin.erik.bitly.Utils.Companion.removeHttp
import net.thauvin.erik.bitly.Utils.Companion.toEndPoint
import org.json.JSONObject
import org.junit.Before
import java.io.File
import java.util.logging.Level
Expand Down Expand Up @@ -72,7 +75,7 @@ class BitlyTest {
val test = Bitly().apply { accessToken = "12345679" }
assertEquals(
"{\"message\":\"FORBIDDEN\"}",
test.bitlinks().shorten("https://erik.thauvin.net/blog", isJson = true)
test.bitlinks().shorten("https://erik.thauvin.net/blog", toJson = true)
)
}

Expand All @@ -89,12 +92,22 @@ class BitlyTest {

@Test
fun `as json`() {
assertTrue(bitly.bitlinks().shorten(longUrl, isJson = true).startsWith("{\"created_at\":"))
assertTrue(bitly.bitlinks().shorten(longUrl, toJson = true).startsWith("{\"created_at\":"))
}

@Test
fun `get user`() {
assertTrue(bitly.call(Utils.buildEndPointUrl("user"), emptyMap(), Methods.GET).contains("\"login\":"))
assertTrue(bitly.call("/user".toEndPoint(), method = Methods.GET).contains("\"login\":"))
}

@Test
fun `created by`() {
assertEquals(
"ethauvin",
JSONObject(
bitly.call("/bitlinks/${shortUrl.removeHttp()}".toEndPoint(), method = Methods.GET)
).getString("created_by")
)
}

@Test
Expand All @@ -109,6 +122,6 @@ class BitlyTest {

@Test
fun `clicks summary`() {
assertNotEquals(Constants.EMPTY, bitly.bitlinks().Clicks().summary(shortUrl))
assertNotEquals(Constants.EMPTY, bitly.bitlinks().clicks(shortUrl))
}
}

0 comments on commit b8cec20

Please sign in to comment.