Changes URL to an actual URL

This commit is contained in:
Julien Lengrand-Lambert
2025-05-20 10:26:39 +02:00
parent f5143140a8
commit ea71d996fc
3 changed files with 21 additions and 10 deletions

View File

@@ -1,5 +1,7 @@
package fr.lengrand.opengraphkt
import java.net.URL
/**
* Enum representing the different types of Open Graph objects.
*/
@@ -75,7 +77,7 @@ data class Data(
// Basic metadata
val title: String?,
val type: String?,
val url: String?,
val url: URL?,
val description: String?,
// Other metadata

View File

@@ -4,6 +4,7 @@ import org.jsoup.Jsoup
import org.jsoup.nodes.Document
import org.jsoup.select.Elements
import java.io.File
import java.net.URI
import java.net.URL
/**
@@ -100,7 +101,8 @@ class Parser {
// Build basic properties
val title = getFirstTagContent(tags, "title")
val type = getFirstTagContent(tags, "type")
val url = getFirstTagContent(tags, "url")
val urlString = getFirstTagContent(tags, "url")
val url = urlString?.let{URI(urlString).toURL()}
val description = getFirstTagContent(tags, "description")
val siteName = getFirstTagContent(tags, "site_name")
val determiner = getFirstTagContent(tags, "determiner")
@@ -363,7 +365,7 @@ class Parser {
val lastName = profileTags.firstOrNull { it.property == "profile:last_name" }?.content
val username = profileTags.firstOrNull { it.property == "profile:username" }?.content
val genderString = profileTags.firstOrNull { it.property == "profile:gender" }?.content
val gender = if(genderString != null) Gender.fromString(genderString) else null
val gender = genderString?.let(Gender::fromString)
return Profile(
firstName = firstName,

View File

@@ -3,10 +3,17 @@ package fr.lengrand.opengraphkt
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.io.TempDir
import java.io.File
import java.net.URL
import kotlin.test.assertEquals
import kotlin.test.assertNotNull
import kotlin.test.assertTrue
// Helper function to compare URL objects with String URLs
private fun assertUrlEquals(expected: String, actual: URL?) {
assertNotNull(actual)
assertEquals(expected, actual.toString())
}
class ParserTest {
private val parser = Parser()
@@ -145,7 +152,7 @@ class ParserTest {
// Verify that all required properties are extracted correctly
assertEquals("The Rock", openGraphData.title)
assertEquals("video.movie", openGraphData.type)
assertEquals("https://example.com/the-rock", openGraphData.url)
assertUrlEquals("https://example.com/the-rock", openGraphData.url)
// Verify that the OpenGraphData object is valid
assertTrue(openGraphData.isValid())
@@ -198,7 +205,7 @@ class ParserTest {
// Verify basic properties
assertEquals("Breaking News", openGraphData.title)
assertEquals("article", openGraphData.type)
assertEquals("https://example.com/news/breaking", openGraphData.url)
assertUrlEquals("https://example.com/news/breaking", openGraphData.url)
assertEquals("Latest breaking news", openGraphData.description)
// Verify article-specific properties
@@ -221,7 +228,7 @@ class ParserTest {
// Verify basic properties
assertEquals("John Doe", openGraphData.title)
assertEquals("profile", openGraphData.type)
assertEquals("https://example.com/profile/johndoe", openGraphData.url)
assertUrlEquals("https://example.com/profile/johndoe", openGraphData.url)
assertEquals("John Doe's profile", openGraphData.description)
// Verify profile-specific properties
@@ -239,7 +246,7 @@ class ParserTest {
// Verify basic properties
assertEquals("The Great Novel", openGraphData.title)
assertEquals("book", openGraphData.type)
assertEquals("https://example.com/books/great-novel", openGraphData.url)
assertUrlEquals("https://example.com/books/great-novel", openGraphData.url)
assertEquals("A great novel", openGraphData.description)
// Verify book-specific properties
@@ -260,7 +267,7 @@ class ParserTest {
// Verify basic properties
assertEquals("Photo Gallery", openGraphData.title)
assertEquals("website", openGraphData.type)
assertEquals("https://example.com/gallery", openGraphData.url)
assertUrlEquals("https://example.com/gallery", openGraphData.url)
assertEquals("A gallery of images", openGraphData.description)
// Verify multiple images
@@ -293,7 +300,7 @@ class ParserTest {
// Verify basic properties
assertEquals("Breaking News", openGraphData.title)
assertEquals("article", openGraphData.type)
assertEquals("https://example.com/news/breaking", openGraphData.url)
assertUrlEquals("https://example.com/news/breaking", openGraphData.url)
assertEquals("Latest breaking news", openGraphData.description)
// Verify article-specific properties
@@ -368,7 +375,7 @@ class ParserTest {
// Verify basic properties
assertEquals("The Matrix", openGraphData.title)
assertEquals("video.movie", openGraphData.type)
assertEquals("https://example.com/movies/the-matrix", openGraphData.url)
assertUrlEquals("https://example.com/movies/the-matrix", openGraphData.url)
assertEquals("A sci-fi action movie", openGraphData.description)
// Verify video.movie-specific properties