Skip to content

Commit

Permalink
Refactor example
Browse files Browse the repository at this point in the history
  • Loading branch information
burnoo committed Sep 1, 2024
1 parent 93ec616 commit ab3df6b
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 18 deletions.
19 changes: 17 additions & 2 deletions example/src/main/kotlin/SimpleExample.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@

import dev.burnoo.ksoup.Kspoon
import model.FakeProfileLite
import dev.burnoo.ksoup.annotation.Selector
import kotlinx.serialization.Serializable
import model.GithubProfile

private const val HTML_CONTENT = """
<h1>burnoo - GitHub</h1>
Expand All @@ -10,13 +13,25 @@ private const val HTML_CONTENT = """
</ul>
"""

@Serializable
data class FakeGitHubProfile(
@Selector("h1", regex = "(.*) - GitHub")
val username: String,
@Selector("img[id=avatar]", attr = "abs:src")
val avatarUrl: String,
@Selector("li")
val interests: List<String>,
@Selector(".pinned-repositories")
val pinnedRepositories: List<GithubProfile.PinnedRepository> = emptyList(),
)

fun simpleExample() {
val ksoup = Kspoon {
parse = { html -> parse(html, baseUri = "https://github.com/") }
coerceInputValues = true
}

val profile = ksoup.parse<FakeProfileLite>(HTML_CONTENT)
val profile = ksoup.parse<FakeGitHubProfile>(HTML_CONTENT)

println("Display name: ${profile.username}")
println("Avatar url: ${profile.avatarUrl}")
Expand Down
16 changes: 0 additions & 16 deletions example/src/main/kotlin/model/FakeProfileLite.kt

This file was deleted.

1 change: 1 addition & 0 deletions example/src/main/kotlin/model/GithubProfile.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ data class GithubProfile(
data class PinnedRepository(
@Selector("span.repo")
val name: String,

@Selector("a.pinned-item-meta", regex = "[0-9]+")
val starNumbers: Int,
)
Expand Down

0 comments on commit ab3df6b

Please sign in to comment.