Skip to content

Commit

Permalink
Merge pull request #236 from hossain-khan/cleanup-code
Browse files Browse the repository at this point in the history
Cleanup code and refactoring
  • Loading branch information
hossain-khan authored Dec 30, 2023
2 parents 3947677 + 0d2b95e commit be94834
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 13 deletions.
5 changes: 5 additions & 0 deletions src/main/kotlin/dev/hossain/githubstats/AppConstants.kt
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,9 @@ object AppConstants {
* Label for PR analysis progress bar.
*/
const val PROGRESS_LABEL = "Progress"

/**
* GitHub token settings URL to direct user to create a new token.
*/
const val GITHUB_TOKEN_SETTINGS_URL = "https://github.com/settings/tokens"
}
33 changes: 32 additions & 1 deletion src/main/kotlin/dev/hossain/githubstats/util/ErrorProcessor.kt
Original file line number Diff line number Diff line change
@@ -1,12 +1,25 @@
package dev.hossain.githubstats.util

import dev.hossain.githubstats.AppConstants
import dev.hossain.githubstats.AppConstants.BUILD_CONFIG
import dev.hossain.githubstats.BuildConfig
import okhttp3.ResponseBody
import retrofit2.HttpException
import retrofit2.Response

class ErrorProcessor {
companion object {
/**
* Error message when token is invalid.
*
* Sample error message.
* ```json
* {"message":"Bad credentials","documentation_url":"https://docs.github.com/rest"}
* ```
*/
private const val TOKEN_ERROR_MESSAGE = "Bad credentials"
}

/**
* Provides exception with detailed message to debug the error.
*/
Expand All @@ -28,14 +41,32 @@ class ErrorProcessor {
val message: String = exception.message ?: "HTTP Error ${exception.code()}"

if (error != null) {
return "$message - ${error.string()}\n$debugGuide"
val errorContent = error.string()
return "$message - ${errorContent}${getTokenErrorGuide(errorContent)}\n$debugGuide"
}
return "${message}\n$debugGuide"
} else {
return "${exception.message}\n$debugGuide"
}
}

private fun getTokenErrorGuide(errorMessage: String): String {
println("Error message: $errorMessage")
return if (errorMessage.contains(TOKEN_ERROR_MESSAGE)) {
"""
------------------------------------------------------------------------------------------------
⚠️ NOTE: Your token likely have expired.
You can create a new token from GitHub settings page and provide it in `[${AppConstants.LOCAL_PROPERTIES_FILE}]`.
See: ${AppConstants.GITHUB_TOKEN_SETTINGS_URL}
------------------------------------------------------------------------------------------------
""".trimIndent()
} else {
""
}
}

private val httpResponseDebugGuide: String = """
------------------------------------------------------------------------------------------------
Expand Down
15 changes: 15 additions & 0 deletions src/main/kotlin/dev/hossain/time/UserCity.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package dev.hossain.time

/**
* User cities for time zone defined in [UserTimeZone].
*/
object UserCity {
const val ATLANTA = "Atlanta"
const val CHICAGO = "Chicago"
const val DETROIT = "Detroit"
const val NEW_YORK = "New York"
const val PHOENIX = "Phoenix"
const val SAN_FRANCISCO = "San Francisco"
const val TORONTO = "Toronto"
const val VANCOUVER = "Vancouver"
}
28 changes: 18 additions & 10 deletions src/main/kotlin/dev/hossain/time/Zone.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
package dev.hossain.time

import dev.hossain.time.UserCity.ATLANTA
import dev.hossain.time.UserCity.CHICAGO
import dev.hossain.time.UserCity.DETROIT
import dev.hossain.time.UserCity.NEW_YORK
import dev.hossain.time.UserCity.PHOENIX
import dev.hossain.time.UserCity.SAN_FRANCISCO
import dev.hossain.time.UserCity.TORONTO
import dev.hossain.time.UserCity.VANCOUVER
import java.time.ZoneId

/**
Expand All @@ -12,19 +20,19 @@ object Zone {
* REF: https://mkyong.com/java8/java-display-all-zoneid-and-its-utc-offset/
*/
val cities = mapOf(
"Atlanta" to ZoneId.of("America/New_York"),
"Chicago" to ZoneId.of("America/Chicago"),
"Detroit" to ZoneId.of("America/Detroit"),
"New York" to ZoneId.of("America/New_York"),
"Phoenix" to ZoneId.of("America/Phoenix"),
"San Francisco" to ZoneId.of("America/Los_Angeles"),
"Toronto" to ZoneId.of("America/Toronto"),
"Vancouver" to ZoneId.of("America/Vancouver")
ATLANTA to ZoneId.of("America/New_York"),
CHICAGO to ZoneId.of("America/Chicago"),
DETROIT to ZoneId.of("America/Detroit"),
NEW_YORK to ZoneId.of("America/New_York"),
PHOENIX to ZoneId.of("America/Phoenix"),
SAN_FRANCISCO to ZoneId.of("America/Los_Angeles"),
TORONTO to ZoneId.of("America/Toronto"),
VANCOUVER to ZoneId.of("America/Vancouver")
)

/**
* Provides [ZoneId] based on known [cityName] defined in [cities].
* Provides [ZoneId] based on known [cityName] defined in [UserCity].
* @throws NullPointerException if [cityName] is not in [cities]
*/
fun city(cityName: String): ZoneId = cities[cityName]!!
fun city(cityName: String): ZoneId = requireNotNull(cities[cityName]) { "Please add $cityName to Zone.cities map first." }
}
20 changes: 18 additions & 2 deletions src/main/kotlin/dev/hossain/time/ZonedDateTimeExtension.kt
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,12 @@ internal fun ZonedDateTime.isWithinWorkingHour(): Boolean {
}

/**
* TODO - doc and example please.
* Checks if the current ZonedDateTime is before the start of the working hour.
* The working hour is considered to start at 9 AM.
*
* @return Boolean value indicating whether the current time is before the start of the working hour.
* If the next working hour is the same as the current time, it returns false.
* If the previous working hour is on the previous day, it means the current time is before 9 AM, so it returns true.
*/
internal fun ZonedDateTime.isBeforeWorkingHour(): Boolean {
val nextWorkingHourOrSame = this.nextWorkingHourOrSame()
Expand All @@ -146,6 +151,14 @@ internal fun ZonedDateTime.isBeforeWorkingHour(): Boolean {
return prevWorkingHour.isSameDay(this).not()
}

/**
* Checks if the current ZonedDateTime is after the end of the working hour.
* The working hour is considered to end at 5 PM.
*
* @return Boolean value indicating whether the current time is after the end of the working hour.
* If the next working hour is the same as the current time, it returns false.
* If the next working hour is on the next day, it means the current time is after 5 PM, so it returns true.
*/
internal fun ZonedDateTime.isAfterWorkingHour(): Boolean {
val nextWorkingHourOrSame = this.nextWorkingHourOrSame()
if (nextWorkingHourOrSame == this) {
Expand All @@ -157,7 +170,10 @@ internal fun ZonedDateTime.isAfterWorkingHour(): Boolean {
}

/**
* Formats the [ZonedDateTime] with formatter to more human-readable time.
* Formats the ZonedDateTime to a more human-readable format.
*
* @return A string representing the formatted date and time.
* The format used is the full localized date-time format for the US locale.
*/
internal fun ZonedDateTime.format(): String {
val formatter = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.FULL)
Expand Down

0 comments on commit be94834

Please sign in to comment.