"""Ashby: `ashby:`, the whole board in one JSON answer. A board name may carry a dot (`jimdo.com`) or be a uuid. The field map is ported from `openings.go` in [jobhunt-hq](https://github.com/rembrandtreyes/jobhunt-hq) (MIT). """ from __future__ import annotations import json import logging from collections.abc import Callable from typing import Any from desk.sources import strip_html from ._contract import Ask, Fields, Place, Reads, Row from ._shared import add_place, is_web_url, num, only, parse_dt, path_slug_ok, pay_line, place, table_word, yearly_amount log = logging.getLogger(__name__) NAME = "ashby" LABEL = "Ashby" RANK = 30 PATTERNS = ( r"jobs\.ashbyhq\.com/([A-Za-z0-9_.-]+)", r"api\.ashbyhq\.com/posting-api/job-board/([A-Za-z0-9_.-]+)", ) READS = Reads( scope="board", listing="whole", text="listing", complete="whole", places="many", empty="suspect", fields=Fields(pay="figures", posted="trusted", remote="stated", employment="stated"), robots="api: no robots.txt check needed (public board API), 2026-09-30", ) _API = "api.ashbyhq.com" #: `workplaceType` -> `store._remote`'s words. `isRemote` is true for hybrid #: roles too, so it is only the fallback when the role has no `workplaceType`. _WORKPLACE = {"remote": "remote", "hybrid": "hybrid", "onsite": "onsite"} #: `employmentType` -> the app's employment words. _EMPLOYMENT = {"fulltime": "full_time", "parttime": "part_time", "intern": "internship", "contract": "contract", "temporary": "temporary"} def slug_ok(slug: str) -> bool: return path_slug_ok(slug) def allowed(slug: str) -> Callable[[str], bool]: return only(_API) def careers_url(slug: str) -> str: return f"https://jobs.ashbyhq.com/{slug}" def feed_url(slug: str, ask: Ask) -> str: return f"https://{_API}/posting-api/job-board/{slug}?includeCompensation=true" def probe_url(slug: str) -> str: return f"https://{_API}/posting-api/job-board/{slug}" def probe_ok(body: bytes) -> bool: """Ashby answers an empty listing for an unknown name, so a listing without jobs counts as no board.""" return b'"jobs":[' in body and b'"jobs":[]' not in body def _salary(components: list[dict[str, Any]]) -> tuple[int | None, int | None, str]: """The yearly range from `compensation.summaryComponents` (needs `?includeCompensation=true` on the feed URL, see `feed_url`): the first `Salary` component, yearly or monthly (a monthly one multiplied by 12); an hourly one is left out rather than annualised.""" for component in components: if component.get("compensationType") != "Salary": continue return yearly_amount(component.get("minValue"), component.get("maxValue"), component.get("currencyCode"), component.get("interval"), {"1 YEAR": 1, "1 MONTH": 12}) return None, None, "" def _salary_text(compensation: dict[str, Any]) -> str: """One line per `summaryComponents` entry that states a bound, labelled by the board's own `compensationType` when more than one does (a salary range beside a bonus range, say); with just the one, the label is left off, the same as Greenhouse's. `interval` is " " ("1 YEAR", "1 HOUR"); the last word, lowered, is the period. With nothing to build a line from, Ashby's own prose summary (`compensationTierSummary`, else `scrapeableCompensationSalarySummary`) is what is left to show.""" components = compensation.get("summaryComponents") or [] stated = [c for c in components if num(c.get("minValue")) is not None or num(c.get("maxValue")) is not None] lines = [] for component in stated: period = str(component.get("interval") or "").split(" ")[-1].lower() line = pay_line(num(component.get("minValue")), num(component.get("maxValue")), component.get("currencyCode"), period) if not line: continue if len(stated) > 1: label = str(component.get("compensationType") or "").strip() line = f"{label}: {line}" if label else line lines.append(line) if lines: return "; ".join(lines) return str(compensation.get("compensationTierSummary") or compensation.get("scrapeableCompensationSalarySummary") or "") def _places(j: dict[str, Any], workplace: str | None) -> list[Place]: """The primary `location`, then each of `secondaryLocations`, in the host's words. The primary is remote or an office as `workplaceType` says; a place that names itself remote is remote.""" primary = (j.get("location") or "").strip() named = [(primary, {"remote": True, "hybrid": False, "onsite": False}.get(workplace or ""))] named += [(str((s or {}).get("location") or "").strip(), None) for s in j.get("secondaryLocations") or []] out: list[Place] = [] for text, remote in named: add_place(out, place(text, remote)) return out def _employment(value: Any) -> str | None: return table_word(_EMPLOYMENT, value, log, "employmentType") def parse(body: bytes, slug: str) -> list[Row]: data = json.loads(body) rows = [] for j in data.get("jobs") or []: title = (j.get("title") or "").strip() url = (j.get("jobUrl") or j.get("applyUrl") or "").strip() if not title or not is_web_url(url): continue description = j.get("descriptionHtml") or j.get("descriptionPlain") or "" compensation = j.get("compensation") or {} salary_min, salary_max, currency = _salary(compensation.get("summaryComponents") or []) workplace = _WORKPLACE.get(str(j.get("workplaceType") or "").strip().lower()) places = _places(j, workplace) row: Row = { "source_id": str(j.get("id") or ""), "title": title, "places": places, "location": "; ".join(p["text"] for p in places), "url": url, "posted": parse_dt(j.get("publishedAt")), "department": (j.get("department") or j.get("team") or "").strip(), "description": strip_html(description), "remote": workplace or j.get("isRemote"), "salary_text": _salary_text(compensation), "salary_min": salary_min, "salary_max": salary_max, "currency": currency, } if employment := _employment(j.get("employmentType")): row["employment"] = employment rows.append(row) return rows