Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/ebird/api/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ def __init__(self, api_key, locale):
self.hotspot = False
self.provisional = True
self.sort = "date"
self.rank = "mrec"
socket.setdefaulttimeout(constants.DEFAULT_TIMEOUT)

def get_observations(self, area):
Expand Down Expand Up @@ -130,6 +131,7 @@ def get_historic_observations(self, area, date):
hotspot=self.hotspot,
detail=self.detail,
category=self.category,
rank=self.rank,
)

def get_nearby_observations(self, lat, lng, dist=25):
Expand Down
8 changes: 7 additions & 1 deletion src/ebird/api/observations.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
clean_locale,
clean_max_observations,
clean_provisional,
clean_observation_rank,
clean_sort,
)

Expand Down Expand Up @@ -633,6 +634,7 @@ def get_historic_observations(
hotspot=False,
detail="simple",
category=None,
rank="mrec",
):
"""Get recent observations for a region.

Expand Down Expand Up @@ -671,6 +673,10 @@ def get_historic_observations(
More than one value can be given in a comma-separated string. The default
value is None and records from all categories will be returned.

:param rank: return the last (rank='mrec') or first(rank='create')
observation on the date. See the eBird API documentation for a description
of the fields.

:return: the list of observations in simple format.

:raises ValueError: if any of the arguments fail the validation checks.
Expand All @@ -686,7 +692,7 @@ def get_historic_observations(
url = HISTORIC_OBSERVATIONS_URL % (cleaned[0], date.strftime("%Y/%m/%d"))

params = {
"rank": "mrec",
"rank": clean_observation_rank(rank),
"detail": clean_detail(detail),
"sppLocale": clean_locale(locale),
"includeProvisional": clean_provisional(provisional),
Expand Down
9 changes: 9 additions & 0 deletions src/ebird/api/validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,15 @@ def clean_detail(value):
return cleaned


def clean_observation_rank(value):
cleaned = clean_code(value, transform=Transform.LOWER)
if cleaned not in ("mrec", "create"):
raise ValueError(
"Value for 'rank', %s, must be either 'mrec' or 'create'" % value
)
return cleaned


def clean_provisional(value):
return "true" if bool(value) else "false"

Expand Down
13 changes: 13 additions & 0 deletions tests/unit/validation/test_clean_observation_rank.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import unittest

from ebird.api.validation import clean_observation_rank


class CleanObservationRankTests(unittest.TestCase):
"""Tests for the rank validation function."""

def test_codes_are_lower_case(self):
self.assertEqual("mrec", clean_observation_rank("Mrec"))

def test_invalid_code_raises_error(self):
self.assertRaises(ValueError, clean_observation_rank, "none")