Skip to content

Commit

Permalink
solving problem for crossference
Browse files Browse the repository at this point in the history
  • Loading branch information
AngelFelizR committed Dec 23, 2024
1 parent 5574af5 commit a2d1f42
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 12 deletions.
30 changes: 21 additions & 9 deletions R/get_bible_verse.R
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,9 @@ get_bible_verse <- function(search, version = "RVR1960") {

# Extract and format verse index
verse_index <-
rvest::html_element(verse_elements, "sup") |>
rvest::html_element(verse_elements, xpath = 'sup[@class="versenum"]') |>
rvest::html_text(trim = TRUE) |>
(\(x) c("1", x[-1L]))() |>
(\(x) ifelse(grepl("^\\d+$", x), x, NA_character_))() |>
(\(x) c(ifelse(is.na(x[1L]), "1", x[1L]), x[-1L]))() |>
fill_na() |>
paste0("^**",a = _, "**^")

Expand All @@ -68,13 +67,26 @@ get_bible_verse <- function(search, version = "RVR1960") {
# Extract and clean verse text
verse_text <-
rvest::html_elements(verse_elements,
xpath = ".//text()[not(parent::sup)]") |>
xpath = './/text()[not(ancestor::sup[@class="versenum"]) and not(ancestor::sup[@class="crossreference"])]') |>
rvest::html_text() |>
(\(x) x[!grepl("^[A-Z]$|^\\d+.+$", x)])() |>
tapply(INDEX = verse_index,
FUN = paste,
collapse = " ") |>
unname()
grep(pattern = "^\\d+.$", value = TRUE, invert = TRUE) |>
# Pasting test if starting with space
(\(x) tapply(x,
INDEX = cumsum(!grepl("^\\s", x)),
FUN = paste0,
collapse = "") )()

# Pasting if verses are split in several lines
if(any(table(verse_index) > 1L)){

verse_text <-
tapply(verse_text,
INDEX = verse_index,
FUN = paste0,
collapse = " ") |>
to_sentence()

}

# Removing any verse duplication
verse_index <- unique(verse_index)
Expand Down
5 changes: 5 additions & 0 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,8 @@ fill_na <- function(x) {
x <- x[x_not_missing_i][cumsum(x_not_missing)]
return(x)
}


to_sentence <- function(x) {
gsub("(?<=\\.|^)(\\s*)([a-z])", "\\1\\U\\2", tolower(x), perl = TRUE)
}
43 changes: 40 additions & 3 deletions tests/testthat/test-get_bible_verse.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
test_that("get_bible_verse works correctly", {

# Test case 1: Mock a successful response with a single verse
mock_response_success_single <- function(req) {
if (grepl("search=Juan\\+3:16", req$url)) {
Expand Down Expand Up @@ -27,6 +28,7 @@ test_that("get_bible_verse works correctly", {
expect_match(result, "> \\*__Juan 3:16__: Porque de tal manera amó Dios al mundo, que ha dado a su Hijo unigénito, para que todo aquel que en él cree, no se pierda, mas tenga vida eterna.\\*")
})


# Test case 2: Mock a successful response with multiple verses in a chapter
mock_response_success_multiple <- function(req) {
if (grepl("search=Salmos\\+23", req$url)) {
Expand All @@ -53,11 +55,46 @@ test_that("get_bible_verse works correctly", {
expect_type(result, "character")
expect_length(result, 1)
expect_match(result,
"> *__Salmos 23__: ^**1**^ Jehová es mi pastor; nada me faltará. ^**2**^ En lugares de delicados pastos me hará descansar; Junto a aguas de reposo me pastoreará. ^**3**^ Confortará mi alma; Me guiará por sendas de justicia por amor de su nombre. ^**4**^ Aunque ande en valle de sombra de muerte, No temeré mal alguno, porque tú estarás conmigo; Tu vara y tu cayado me infundirán aliento. ^**5**^ Aderezas mesa delante de mí en presencia de mis angustiadores; Unges mi cabeza con aceite; mi copa está rebosando. ^**6**^ Ciertamente el bien y la misericordia me seguirán todos los días de mi vida, Y en la casa de Jehová moraré por largos días.*",
"> *__Salmos 23__: ^**1**^ Jehová es mi pastor; nada me faltará. ^**2**^ En lugares de delicados pastos me hará descansar; junto a aguas de reposo me pastoreará. ^**3**^ Confortará mi alma; me guiará por sendas de justicia por amor de su nombre. ^**4**^ Aunque ande en valle de sombra de muerte, no temeré mal alguno, porque tú estarás conmigo; tu vara y tu cayado me infundirán aliento. ^**5**^ Aderezas mesa delante de mí en presencia de mis angustiadores; unges mi cabeza con aceite; mi copa está rebosando. ^**6**^ Ciertamente el bien y la misericordia me seguirán todos los días de mi vida, y en la casa de jehová moraré por largos días.",
fixed = TRUE)
})


# Test case 3: Mock a successful response with crossreference
mock_response_crossreference <- function(req) {
if (grepl("search=Juan\\+8:12-16", req$url)) {
httr2::response(
status_code = 200,
headers = "Content-Type: text/html",
body = '
<div class="passage-content passage-class-0"><div class="version-RVR1960 result-text-style-normal text-html">
<h3><span id="es-RVR1960-26395" class="text John-8-12">Jesús, la luz del mundo</span></h3><p><span class="text John-8-12"><sup class="versenum">12&nbsp;</sup>Otra vez Jesús les habló, diciendo: Yo soy la luz del mundo;<sup class="crossreference" data-cr="#ces-RVR1960-26395A" data-link="(<a href=&quot;#ces-RVR1960-26395A&quot; title=&quot;See cross-reference A&quot;>A</a>)">(<a href="#ces-RVR1960-26395A" title="See cross-reference A">A</a>)</sup> el que me sigue, no andará en tinieblas, sino que tendrá la luz de la vida.</span> <span id="es-RVR1960-26396" class="text John-8-13"><sup class="versenum">13&nbsp;</sup>Entonces los fariseos le dijeron: Tú das testimonio acerca de ti mismo; tu testimonio no es verdadero.<sup class="crossreference" data-cr="#ces-RVR1960-26396B" data-link="(<a href=&quot;#ces-RVR1960-26396B&quot; title=&quot;See cross-reference B&quot;>B</a>)">(<a href="#ces-RVR1960-26396B" title="See cross-reference B">B</a>)</sup></span> <span id="es-RVR1960-26397" class="text John-8-14"><sup class="versenum">14&nbsp;</sup>Respondió Jesús y les dijo: Aunque yo doy testimonio acerca de mí mismo, mi testimonio es verdadero, porque sé de dónde he venido y a dónde voy; pero vosotros no sabéis de dónde vengo, ni a dónde voy.</span> <span id="es-RVR1960-26398" class="text John-8-15"><sup class="versenum">15&nbsp;</sup>Vosotros juzgáis según la carne; yo no juzgo a nadie.</span> <span id="es-RVR1960-26399" class="text John-8-16"><sup class="versenum">16&nbsp;</sup>Y si yo juzgo, mi juicio es verdadero; porque no soy yo solo, sino yo y el que me envió, el Padre.</span> </p><a class="full-chap-link" href="/passage/?search=Juan%208&amp;version=RVR1960" title="View Full Chapter">Read full chapter</a>
<div class="crossrefs hidden">
<h4>Cross references</h4><ol><li id="ces-RVR1960-26395A"><a href="#es-RVR1960-26395" title="Go to Juan 8:12">Juan 8:12</a> : <a class="crossref-link" href="/passage/?search=Mateo%205%3A14%2CJuan%209%3A5&amp;version=RVR1960" data-bibleref="Mateo 5:14, Juan 9:5">Mt. 5.14; Jn. 9.5.</a></li>
<li id="ces-RVR1960-26396B"><a href="#es-RVR1960-26396" title="Go to Juan 8:13">Juan 8:13</a> : <a class="crossref-link" href="/passage/?search=Juan%205%3A31&amp;version=RVR1960" data-bibleref="Juan 5:31">Jn. 5.31.</a></li>
</ol></div> <!--end of crossrefs-->
</div>
</div>
'
)
} else {
NULL
}
}

httr2::with_mocked_responses(mock_response_success_multiple, {
result <- get_bible_verse("Juan 8:12-16")
expect_type(result, "character")
expect_length(result, 1)
expect_match(result,
"> *__Juan 8:12-16__: ^**12**^ Otra vez Jesús les habló, diciendo: Yo soy la luz del mundo; el que me sigue, no andará en tinieblas, sino que tendrá la luz de la vida. ^**13**^ Entonces los fariseos le dijeron: Tú das testimonio acerca de ti mismo; tu testimonio no es verdadero. ^**14**^ Respondió Jesús y les dijo: Aunque yo doy testimonio acerca de mí mismo, mi testimonio es verdadero, porque sé de dónde he venido y a dónde voy; pero vosotros no sabéis de dónde vengo, ni a dónde voy. ^**15**^ Vosotros juzgáis según la carne; yo no juzgo a nadie. ^**16**^ Y si yo juzgo, mi juicio es verdadero; porque no soy yo solo, sino yo y el que me envió, el Padre.*",
fixed = TRUE)
})

# Test case 3: Mock a response with no verses found

# Test case 4: Mock a response with no verses found
mock_response_no_verses <- function(req) {
if (grepl("search=this\\+is\\+a\\+non\\+existing\\+book\\+1:1", req$url)) {
httr2::response(
Expand All @@ -80,7 +117,7 @@ test_that("get_bible_verse works correctly", {
expect_null(result)
})

# Test case 4: Mock a response with an error
# Test case 5: Mock a response with an error
mock_response_error <- function(req) {
if (grepl("search=error", req$url)) {
httr2::response(status_code = 500)
Expand Down

0 comments on commit a2d1f42

Please sign in to comment.