Use regex to find URLs
This commit is contained in:
@@ -1,5 +1,6 @@
|
|||||||
package gq.kirmanak.mealient.data.share
|
package gq.kirmanak.mealient.data.share
|
||||||
|
|
||||||
|
import androidx.core.util.PatternsCompat
|
||||||
import gq.kirmanak.mealient.logging.Logger
|
import gq.kirmanak.mealient.logging.Logger
|
||||||
import javax.inject.Inject
|
import javax.inject.Inject
|
||||||
import javax.inject.Singleton
|
import javax.inject.Singleton
|
||||||
@@ -12,16 +13,9 @@ class ShareRecipeRepoImpl @Inject constructor(
|
|||||||
|
|
||||||
override suspend fun saveRecipeByURL(url: CharSequence): String {
|
override suspend fun saveRecipeByURL(url: CharSequence): String {
|
||||||
logger.v { "saveRecipeByURL() called with: url = $url" }
|
logger.v { "saveRecipeByURL() called with: url = $url" }
|
||||||
|
val matcher = PatternsCompat.WEB_URL.matcher(url)
|
||||||
val urlStart = url.indexOf("http")
|
require(matcher.find()) { "Can't find URL in the text" }
|
||||||
if (urlStart == -1) throw IllegalArgumentException("URL doesn't start with http")
|
val urlString = matcher.group()
|
||||||
|
|
||||||
val startsWithUrl = url.subSequence(urlStart, url.length)
|
|
||||||
val urlEnd = startsWithUrl.indexOfFirst { it.isWhitespace() }
|
|
||||||
.takeUnless { it == -1 }
|
|
||||||
?: startsWithUrl.length
|
|
||||||
|
|
||||||
val urlString = startsWithUrl.substring(0, urlEnd)
|
|
||||||
val request = ParseRecipeURLInfo(url = urlString, includeTags = true)
|
val request = ParseRecipeURLInfo(url = urlString, includeTags = true)
|
||||||
return parseRecipeDataSource.parseRecipeFromURL(request)
|
return parseRecipeDataSource.parseRecipeFromURL(request)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -52,7 +52,7 @@ class ShareRecipeRepoImplTest : BaseUnitTest() {
|
|||||||
fun `when url has suffix expect saveRecipeByURL removes it`() = runTest {
|
fun `when url has suffix expect saveRecipeByURL removes it`() = runTest {
|
||||||
subject.saveRecipeByURL("https://www.allrecipes.com/recipe/215447/dads-leftover-turkey-pot-pie/ is my favorite recipe")
|
subject.saveRecipeByURL("https://www.allrecipes.com/recipe/215447/dads-leftover-turkey-pot-pie/ is my favorite recipe")
|
||||||
val expected = ParseRecipeURLInfo(
|
val expected = ParseRecipeURLInfo(
|
||||||
url = "https://www.allrecipes.com/recipe/215447/dads-leftover-turkey-pot-pie/",
|
url = "https://www.allrecipes.com/recipe/215447/dads-leftover-turkey-pot-pie",
|
||||||
includeTags = true
|
includeTags = true
|
||||||
)
|
)
|
||||||
coVerify { parseRecipeDataSource.parseRecipeFromURL(eq(expected)) }
|
coVerify { parseRecipeDataSource.parseRecipeFromURL(eq(expected)) }
|
||||||
@@ -62,7 +62,7 @@ class ShareRecipeRepoImplTest : BaseUnitTest() {
|
|||||||
fun `when url has prefix and suffix expect saveRecipeByURL removes them`() = runTest {
|
fun `when url has prefix and suffix expect saveRecipeByURL removes them`() = runTest {
|
||||||
subject.saveRecipeByURL("Actually, https://www.allrecipes.com/recipe/215447/dads-leftover-turkey-pot-pie/ is my favorite recipe")
|
subject.saveRecipeByURL("Actually, https://www.allrecipes.com/recipe/215447/dads-leftover-turkey-pot-pie/ is my favorite recipe")
|
||||||
val expected = ParseRecipeURLInfo(
|
val expected = ParseRecipeURLInfo(
|
||||||
url = "https://www.allrecipes.com/recipe/215447/dads-leftover-turkey-pot-pie/",
|
url = "https://www.allrecipes.com/recipe/215447/dads-leftover-turkey-pot-pie",
|
||||||
includeTags = true
|
includeTags = true
|
||||||
)
|
)
|
||||||
coVerify { parseRecipeDataSource.parseRecipeFromURL(eq(expected)) }
|
coVerify { parseRecipeDataSource.parseRecipeFromURL(eq(expected)) }
|
||||||
|
|||||||
Reference in New Issue
Block a user