diff --git a/opengraphkt/src/main/kotlin/fr/lengrand/opengraphkt/OpenGraphParser.kt b/opengraphkt/src/main/kotlin/fr/lengrand/opengraphkt/OpenGraphParser.kt index 71e0f4e..18b082b 100644 --- a/opengraphkt/src/main/kotlin/fr/lengrand/opengraphkt/OpenGraphParser.kt +++ b/opengraphkt/src/main/kotlin/fr/lengrand/opengraphkt/OpenGraphParser.kt @@ -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( diff --git a/opengraphkt/src/test/kotlin/fr/lengrand/opengraphkt/OpenGraphParserTest.kt b/opengraphkt/src/test/kotlin/fr/lengrand/opengraphkt/OpenGraphParserTest.kt index 5944470..6860013 100644 --- a/opengraphkt/src/test/kotlin/fr/lengrand/opengraphkt/OpenGraphParserTest.kt +++ b/opengraphkt/src/test/kotlin/fr/lengrand/opengraphkt/OpenGraphParserTest.kt @@ -334,6 +334,33 @@ class OpenGraphParserTest { """.trimIndent() + private val noTypeHtml = """ + + + + Video Movie Example + + + +

The Matrix

+ + + """.trimIndent() + + private val unknownTypeHtml = """ + + + + Video Movie Example + + + + +

The Matrix

+ + + """.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()) + } }