mirror of
https://github.com/jlengrand/OpenGraphKt.git
synced 2026-03-10 08:31:23 +00:00
Adds Enum to easily recognize type
This commit is contained in:
@@ -6,6 +6,52 @@ import org.jsoup.select.Elements
|
||||
import java.io.File
|
||||
import java.net.URL
|
||||
|
||||
/**
|
||||
* Enum representing the different types of Open Graph objects.
|
||||
*/
|
||||
enum class OpenGraphType {
|
||||
ARTICLE,
|
||||
PROFILE,
|
||||
BOOK,
|
||||
MUSIC_SONG,
|
||||
MUSIC_ALBUM,
|
||||
MUSIC_PLAYLIST,
|
||||
MUSIC_RADIO_STATION,
|
||||
VIDEO_MOVIE,
|
||||
VIDEO_TV_SHOW,
|
||||
VIDEO_OTHER,
|
||||
VIDEO_EPISODE,
|
||||
WEBSITE,
|
||||
UNKNOWN;
|
||||
|
||||
companion object {
|
||||
/**
|
||||
* Converts a string type to the corresponding enum value.
|
||||
*
|
||||
* @param type The string representation of the type
|
||||
* @return The corresponding OpenGraphType enum value, or UNKNOWN if not recognized
|
||||
*/
|
||||
fun fromString(type: String?): OpenGraphType {
|
||||
return when (type) {
|
||||
"article" -> ARTICLE
|
||||
"profile" -> PROFILE
|
||||
"book" -> BOOK
|
||||
"music.song" -> MUSIC_SONG
|
||||
"music.album" -> MUSIC_ALBUM
|
||||
"music.playlist" -> MUSIC_PLAYLIST
|
||||
"music.radio_station" -> MUSIC_RADIO_STATION
|
||||
"video.movie" -> VIDEO_MOVIE
|
||||
"video.tv_show" -> VIDEO_TV_SHOW
|
||||
"video.other" -> VIDEO_OTHER
|
||||
"video.episode" -> VIDEO_EPISODE
|
||||
"website" -> WEBSITE
|
||||
null -> WEBSITE // No type tag defaults to Website
|
||||
else -> UNKNOWN // Another type we are not aware of
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
data class OpenGraphTag(
|
||||
val property: String,
|
||||
val content: String,
|
||||
@@ -62,6 +108,15 @@ data class OpenGraphData(
|
||||
fun isValid(): Boolean {
|
||||
return title != null && type != null && images.isNotEmpty() && url != null
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the type of this Open Graph data as an enum value.
|
||||
*
|
||||
* @return The OpenGraphType enum value corresponding to the type string, or UNKNOWN if the type is not recognized
|
||||
*/
|
||||
fun getType(): OpenGraphType {
|
||||
return OpenGraphType.fromString(type)
|
||||
}
|
||||
}
|
||||
|
||||
data class OpenGraphImage(
|
||||
|
||||
@@ -334,6 +334,33 @@ class OpenGraphParserTest {
|
||||
</html>
|
||||
""".trimIndent()
|
||||
|
||||
private val noTypeHtml = """
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<title>Video Movie Example</title>
|
||||
<meta property="og:title" content="The Matrix" />
|
||||
</head>
|
||||
<body>
|
||||
<h1>The Matrix</h1>
|
||||
</body>
|
||||
</html>
|
||||
""".trimIndent()
|
||||
|
||||
private val unknownTypeHtml = """
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<title>Video Movie Example</title>
|
||||
<meta property="og:title" content="The Matrix" />
|
||||
<meta property="og:type" content="test" />
|
||||
</head>
|
||||
<body>
|
||||
<h1>The Matrix</h1>
|
||||
</body>
|
||||
</html>
|
||||
""".trimIndent()
|
||||
|
||||
@Test
|
||||
fun `test parse with video movie-specific tags`() {
|
||||
val openGraphData = parser.parse(videoMovieHtml)
|
||||
@@ -361,4 +388,35 @@ class OpenGraphParserTest {
|
||||
assertTrue(openGraphData.videoMovie.tags.contains("sci-fi"))
|
||||
assertTrue(openGraphData.videoMovie.tags.contains("action"))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `test getType method returns correct enum values`() {
|
||||
// Test video.movie type
|
||||
val videoMovieData = parser.parse(videoMovieHtml)
|
||||
assertEquals(OpenGraphType.VIDEO_MOVIE, videoMovieData.getType())
|
||||
|
||||
// Test article type
|
||||
val articleData = parser.parse(articleHtml)
|
||||
assertEquals(OpenGraphType.ARTICLE, articleData.getType())
|
||||
|
||||
// Test profile type
|
||||
val profileData = parser.parse(profileHtml)
|
||||
assertEquals(OpenGraphType.PROFILE, profileData.getType())
|
||||
|
||||
// Test book type
|
||||
val bookData = parser.parse(bookHtml)
|
||||
assertEquals(OpenGraphType.BOOK, bookData.getType())
|
||||
|
||||
// Test website type (should return UNKNOWN as it's not in our enum)
|
||||
val websiteData = parser.parse(multipleImagesHtml)
|
||||
assertEquals(OpenGraphType.WEBSITE, websiteData.getType())
|
||||
|
||||
// Test no type defaults to Website
|
||||
val noTypeData = parser.parse(noTypeHtml)
|
||||
assertEquals(OpenGraphType.WEBSITE, noTypeData.getType())
|
||||
|
||||
// Test unrecognized type is Unknown
|
||||
val unkwownData = parser.parse(unknownTypeHtml)
|
||||
assertEquals(OpenGraphType.UNKNOWN, unkwownData.getType())
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user