Skip to content

Commit

Permalink
Merge pull request #126 from ropensci/cl_fix
Browse files Browse the repository at this point in the history
fix ebirdchecklist for multiple media types
  • Loading branch information
slager authored Mar 24, 2024
2 parents f9301b7 + b3ebaa8 commit 81137ee
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 6 deletions.
14 changes: 12 additions & 2 deletions R/ebirdchecklist.R
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@
#' @return "howManyStr" number of individuals reported for each taxon
#' @return "exoticCategory" exotic species categories for each taxon
#' @return "obsComments" observation comments for each taxon
#' @return "photoCounts" number of photos for each taxon
#' @return "audioCounts" number of audio files for each taxon
#' @return "videoCounts" number of video files for each taxon
#' @return "auxCode" breding code for each taxon

#' @export
Expand Down Expand Up @@ -103,11 +106,18 @@ ebirdchecklist <- function(subId, sleep = 0, key = NULL, other = FALSE, ...) {
obs_df$subnational1Code <- NULL
obs_df$obsDt <- NULL
obs_df$projId <- NULL
# mediaCounts appears to just be a nested integer vector (?)
obs_df$mediaCounts <- Reduce(c, obs_df$mediaCounts)
# 'comments' column has name duplicated with checklist comments
names(obs_df)[names(obs_df) == 'comments'] <- 'obsComments'

# deal with mediaCounts
if (! is.null(obs_df$mediaCounts)){
mediaCounts <- obs_df$mediaCounts
idx <- match(names(mediaCounts), c('P','A','V'))
names(mediaCounts) <- c('photoCounts', 'audioCounts', 'videoCounts')[idx]
obs_df$mediaCounts <- NULL
obs_df <- cbind(obs_df, mediaCounts)
}

# join to get result df
out_df <- sub_df
if (! is.null(subAux_df) && other){
Expand Down
6 changes: 6 additions & 0 deletions man/ebirdchecklist.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions man/rebird-package.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

47 changes: 43 additions & 4 deletions tests/fixtures/ebirdchecklist.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ http_interactions:
cache-control: no-cache, no-store, max-age=0, must-revalidate
content-encoding: gzip
content-type: application/json;charset=utf-8
date: Sat, 23 Mar 2024 05:57:03 GMT
date: Sun, 24 Mar 2024 02:48:14 GMT
expires: '0'
pragma: no-cache
server: Apache
Expand All @@ -41,7 +41,46 @@ http_interactions:
06:55","subnational1Code":"US-WA","howManyAtleast":2,"howManyAtmost":2,"present":false,"subId":"S117450946","projId":"EBIRD","obsId":"OBS1503894278","howManyStr":"2"},{"speciesCode":"houspa","hideFlags":[],"exoticCategory":"N","obsDt":"2022-05-30
06:55","subnational1Code":"US-WA","howManyAtleast":1,"howManyAtmost":1,"comments":"ON","present":false,"subId":"S117450946","projId":"EBIRD","obsId":"OBS1503894274","howManyStr":"1","obsAux":[{"subId":"S117450946","fieldName":"breeding_code","entryMethodCode":"ebird_breeding_code","auxCode":"ON","obsId":"OBS1503894274","speciesCode":"houspa","value":"ON"}]},{"speciesCode":"pswspa1","hideFlags":[],"obsDt":"2022-05-30
06:55","subnational1Code":"US-WA","howManyAtleast":1,"howManyAtmost":1,"present":false,"subId":"S117450946","projId":"EBIRD","obsId":"OBS1503894276","howManyStr":"1"}]}'
recorded_at: 2024-03-23 06:01:43 GMT
recorded_at: 2024-03-24 02:52:56 GMT
recorded_with: vcr/1.2.2, webmockr/0.9.0
- request:
method: get
uri: https://ebird.org/ws2.0/product/checklist/view/S89475689
body:
encoding: ''
string: ''
headers:
Accept: application/json, text/xml, application/xml, */*
X-eBirdApiToken: <<<redacted>>>
response:
status:
status_code: 200
category: Success
reason: OK
message: 'Success: (200) OK'
headers:
cache-control: no-cache, no-store, max-age=0, must-revalidate
content-encoding: gzip
content-type: application/json;charset=utf-8
date: Sun, 24 Mar 2024 02:48:14 GMT
expires: '0'
pragma: no-cache
server: Apache
strict-transport-security: max-age=31536000 ; includeSubDomains
vary: Origin,Accept-Encoding,Access-Control-Request-Method,Access-Control-Request-Headers
x-content-type-options: nosniff
x-frame-options: DENY
x-xss-protection: 1; mode=block
content-length: '484'
body:
encoding: ''
file: no
string: '{"projId":"EBIRD","subId":"S89475689","protocolId":"P20","locId":"L15211376","groupId":"G6835020","allObsReported":false,"creationDt":"2021-06-02
10:01","lastEditedDt":"2023-01-19 09:41","obsDt":"2021-05-30 02:51","obsTimeValid":true,"checklistId":"CL23150","numObservers":2,"subnational1Code":"US-WA","submissionMethodCode":"EBIRD_android","submissionMethodVersion":"2.8.1_SDK24","userDisplayName":"Dave
Slager","numSpecies":2,"submissionMethodVersionDisp":"2.8.1","subAux":[{"subId":"S89475689","fieldName":"nocturnal","entryMethodCode":"ebird_nocturnal","auxCode":"1"}],"subAuxAi":[],"obs":[{"speciesCode":"flaowl","hideFlags":[],"obsDt":"2021-05-30
02:51","subnational1Code":"US-WA","howManyAtleast":1,"howManyAtmost":1,"comments":"Ho","mediaCounts":{"A":1},"present":false,"subId":"S89475689","projId":"EBIRD","obsId":"OBS1173665676","howManyStr":"1"},{"speciesCode":"grhowl","hideFlags":[],"obsDt":"2021-05-30
02:51","subnational1Code":"US-WA","howManyAtleast":1,"howManyAtmost":1,"comments":"Ho","present":false,"subId":"S89475689","projId":"EBIRD","obsId":"OBS1173665675","howManyStr":"1"}]}'
recorded_at: 2024-03-24 02:52:56 GMT
recorded_with: vcr/1.2.2, webmockr/0.9.0
- request:
method: get
Expand All @@ -62,7 +101,7 @@ http_interactions:
cache-control: no-cache, no-store, max-age=0, must-revalidate
content-encoding: gzip
content-type: application/json
date: Sat, 23 Mar 2024 05:57:04 GMT
date: Sun, 24 Mar 2024 02:48:14 GMT
expires: '0'
pragma: no-cache
server: Apache
Expand All @@ -77,5 +116,5 @@ http_interactions:
file: no
string: '{"errors":[{"status":"400 BAD_REQUEST","code":"Pattern","title":"Field
subId of checklistBySubIdCmd: subId is invalid."}]}'
recorded_at: 2024-03-23 06:01:43 GMT
recorded_at: 2024-03-24 02:52:56 GMT
recorded_with: vcr/1.2.2, webmockr/0.9.0
5 changes: 5 additions & 0 deletions tests/testthat/test-ebirdchecklist.R
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ vcr::use_cassette("ebirdchecklist", {
expect_true("checklistId" %in% names(out1))
expect_equal(out1$checklistId[1], "CL24321")

# works with media
expect_no_error(out2 <- ebirdchecklist("S89475689", other = TRUE))
expect_true('audioCounts' %in% colnames(out2))
expect_equal(nrow(out2), 2)

})

test_that("ebirdchecklist errors for bad input", {
Expand Down

0 comments on commit 81137ee

Please sign in to comment.