diff --git a/README.md b/README.md index 6eaed1c..ed90877 100644 --- a/README.md +++ b/README.md @@ -1,10 +1,16 @@ -[![License (3-Clause BSD)](https://img.shields.io/badge/license-BSD%203--Clause-blue.svg?style=flat-square)](https://opensource.org/licenses/BSD-3-Clause) [![Nexus Snapshot](https://img.shields.io/nexus/s/net.thauvin.erik/jokeapi?server=https%3A%2F%2Foss.sonatype.org%2F)](https://oss.sonatype.org/content/repositories/snapshots/net/thauvin/erik/jokeapi/) +[![License (3-Clause BSD)](https://img.shields.io/badge/license-BSD%203--Clause-blue.svg?style=flat-square)](https://opensource.org/licenses/BSD-3-Clause) +[![Kotlin](https://img.shields.io/badge/kotlin-1.8.0-blue)](https://kotlinlang.org/) +[![Nexus Snapshot](https://img.shields.io/nexus/s/net.thauvin.erik/jokeapi?label=snapshot&server=https%3A%2F%2Foss.sonatype.org%2F)](https://oss.sonatype.org/content/repositories/snapshots/net/thauvin/erik/jokeapi/) +[![Release](https://img.shields.io/github/release/ethauvin/jokeapi.svg)](https://github.com/ethauvin/jokeapi/releases/latest) +[![Maven Central](https://maven-badges.herokuapp.com/maven-central/net.thauvin.erik/jokeapi/badge.svg?color=blue)](https://maven-badges.herokuapp.com/maven-central/net.thauvin.erik/jokeapi) -[![Quality Gate Status](https://sonarcloud.io/api/project_badges/measure?project=ethauvin_jokeapi&metric=alert_status)](https://sonarcloud.io/dashboard?id=ethauvin_jokeapi) [![GitHub CI](https://github.com/ethauvin/jokeapi/actions/workflows/gradle.yml/badge.svg)](https://github.com/ethauvin/jokeapi/actions/workflows/gradle.yml) [![CircleCI](https://circleci.com/gh/ethauvin/jokeapi/tree/master.svg?style=shield)](https://circleci.com/gh/ethauvin/jokeapi/tree/master) +[![Quality Gate Status](https://sonarcloud.io/api/project_badges/measure?project=ethauvin_jokeapi&metric=alert_status)](https://sonarcloud.io/dashboard?id=ethauvin_jokeapi) +[![GitHub CI](https://github.com/ethauvin/jokeapi/actions/workflows/gradle.yml/badge.svg)](https://github.com/ethauvin/jokeapi/actions/workflows/gradle.yml) +[![CircleCI](https://circleci.com/gh/ethauvin/jokeapi/tree/master.svg?style=shield)](https://circleci.com/gh/ethauvin/jokeapi/tree/master) -# JokeAPI for Kotlin/Java +# JokeAPI for Kotlin, Java and Android -A simple Kotlin/Java library to retrieve jokes from [Sv443's JokeAPI](https://v2.jokeapi.dev/). +A simple library to retrieve jokes from [Sv443's JokeAPI](https://v2.jokeapi.dev/). ## Examples (TL;DR) @@ -17,7 +23,7 @@ val pun = getJoke(categories = setOf(Category.PUN)) ``` The parameters match the [joke endpoint](https://v2.jokeapi.dev/#joke-endpoint). -A `Joke` class instance is returned: +A `Joke` class instance is returned, matching the [response](https://v2.jokeapi.dev/joke/Any?type=single): ```kotlin data class Joke( @@ -45,7 +51,7 @@ frenchJokes.forEach { - View more [examples](https://github.com/ethauvin/jokeapi/blob/master/src/test/kotlin/net/thauvin/erik/jokeapi/GetJokesTest.kt)... -If an error occurs, a `JokeException` is thrown: +If an error occurs, a `JokeException` is thrown, matching the [JokeAPI errors](https://sv443.net/jokeapi/v2/#errors): ```kotlin class JokeException( @@ -88,7 +94,7 @@ To use with [Gradle](https://gradle.org/), include the following dependency in y ```gradle repositories { mavenCentral() - maven { url = uri("https://oss.sonatype.org/content/repositories/snapshots") } + maven { url = uri("https://oss.sonatype.org/content/repositories/snapshots") } // only needed for SNAPSHOT } dependencies { diff --git a/detekt-baseline.xml b/detekt-baseline.xml index 8ca446d..4e59bad 100644 --- a/detekt-baseline.xml +++ b/detekt-baseline.xml @@ -2,7 +2,6 @@ - ComplexMethod:JokeApi.kt$fun getRawJokes( categories: Set<Category> = setOf(Category.ANY), lang: Language = Language.EN, blacklistFlags: Set<Flag> = emptySet(), type: Type = Type.ALL, format: Format = Format.JSON, contains: String = "", idRange: IdRange = IdRange(), amount: Int = 1, safe: Boolean = false, auth: String = "" ): String LongParameterList:JokeApi.kt$( amount: Int, categories: Set<Category> = setOf(Category.ANY), lang: Language = Language.EN, blacklistFlags: Set<Flag> = emptySet(), type: Type = Type.ALL, contains: String = "", idRange: IdRange = IdRange(), safe: Boolean = false, auth: String = "", splitNewLine: Boolean = false ) LongParameterList:JokeApi.kt$( categories: Set<Category> = setOf(Category.ANY), lang: Language = Language.EN, blacklistFlags: Set<Flag> = emptySet(), type: Type = Type.ALL, contains: String = "", idRange: IdRange = IdRange(), safe: Boolean = false, auth: String = "", splitNewLine: Boolean = false ) LongParameterList:JokeApi.kt$( categories: Set<Category> = setOf(Category.ANY), lang: Language = Language.EN, blacklistFlags: Set<Flag> = emptySet(), type: Type = Type.ALL, format: Format = Format.JSON, contains: String = "", idRange: IdRange = IdRange(), amount: Int = 1, safe: Boolean = false, auth: String = "" ) @@ -19,6 +18,5 @@ MagicNumber:util.kt$500 MagicNumber:util.kt$523 TooManyFunctions:JokeConfig.kt$JokeConfig$Builder - UnusedPrivateMember:JokeConfig.kt$JokeConfig.Builder$search: String diff --git a/src/main/kotlin/net/thauvin/erik/jokeapi/JokeApi.kt b/src/main/kotlin/net/thauvin/erik/jokeapi/JokeApi.kt index 849acbb..d2f46d6 100644 --- a/src/main/kotlin/net/thauvin/erik/jokeapi/JokeApi.kt +++ b/src/main/kotlin/net/thauvin/erik/jokeapi/JokeApi.kt @@ -311,18 +311,16 @@ fun getRawJokes( if (idRange.start >= 0) { if (idRange.end == -1 || idRange.start == idRange.end) { params[Parameter.RANGE] = idRange.start.toString() - } else if (idRange.end > idRange.start) { - params[Parameter.RANGE] = "${idRange.start}-${idRange.end}" } else { - throw IllegalArgumentException("Invalid ID Range: ${idRange.start}, ${idRange.end}") + require(idRange.end > idRange.start) { "Invalid ID Range: ${idRange.start}, ${idRange.end}" } + params[Parameter.RANGE] = "${idRange.start}-${idRange.end}" } } // Amount + require(amount > 0) { "Invalid Amount: $amount" } if (amount > 1) { params[Parameter.AMOUNT] = amount.toString() - } else if (amount <= 0) { - throw IllegalArgumentException("Invalid Amount: $amount") } // Safe diff --git a/src/main/kotlin/net/thauvin/erik/jokeapi/JokeConfig.kt b/src/main/kotlin/net/thauvin/erik/jokeapi/JokeConfig.kt index cf1a7d8..0e96350 100644 --- a/src/main/kotlin/net/thauvin/erik/jokeapi/JokeConfig.kt +++ b/src/main/kotlin/net/thauvin/erik/jokeapi/JokeConfig.kt @@ -77,7 +77,7 @@ class JokeConfig private constructor( var safe: Boolean = false, var auth: String = "", var splitNewLine: Boolean = false - ) { + ) { fun categories(categories: Set) = apply { this.categories = categories } fun lang(language: Language) = apply { lang = language } fun blacklistFlags(flags: Set) = apply { blacklistFlags = flags }