pyrtan / examples / hacker-news / hacker-news.toml
1,931 bytes · last changed 2026-10-26 in The catalog grows up: namespaces, publishing by pull request through the gate, gap reports gossiped back to authors; release 0.8.0
| 1 | # Hacker News, through its public Firebase API. Two trees: this one answers |
| 2 | # the question, and `hacker-news-item` fetches one story, so that `each` |
| 3 | # can reuse it. |
| 4 | |
| 5 | name = "hacker-news" |
| 6 | version = 2 |
| 7 | namespace = "pyrtan" |
| 8 | description = "Top stories on Hacker News, from its public API." |
| 9 | tags = ["news", "read-only"] |
| 10 | license = "MIT" |
| 11 | hosts = ["hacker-news.firebaseio.com"] |
| 12 | start = "fetch-ids" |
| 13 | |
| 14 | [[triggers]] |
| 15 | pattern = "[what's the] top story on hacker news" |
| 16 | inputs = { count = 1 } |
| 17 | |
| 18 | [[triggers]] |
| 19 | pattern = "top {count:int} stories on hacker news" |
| 20 | |
| 21 | [inputs.count] |
| 22 | type = "int" |
| 23 | default = 1 |
| 24 | min = 1 |
| 25 | max = 30 |
| 26 | description = "How many stories to return" |
| 27 | |
| 28 | [nodes.fetch-ids] |
| 29 | kind = "observe" |
| 30 | http = { method = "GET", url = "https://hacker-news.firebaseio.com/v0/topstories.json" } |
| 31 | into = "ids_resp" |
| 32 | extract = [{ into = "ids", json = "json" }] |
| 33 | next = "check" |
| 34 | |
| 35 | [nodes.check] |
| 36 | kind = "decide" |
| 37 | on = "ids_resp.status" |
| 38 | branches = [ |
| 39 | { when = { eq = 200 }, then = "any" }, |
| 40 | { when = { gte = 500 }, then = "hn-down" }, |
| 41 | { else = "unexpected" }, |
| 42 | ] |
| 43 | |
| 44 | [nodes.any] |
| 45 | kind = "decide" |
| 46 | on = "ids" |
| 47 | branches = [ |
| 48 | { when = { empty = true }, then = "nothing" }, |
| 49 | { when = { type = "list" }, then = "stories" }, |
| 50 | { else = "unexpected" }, |
| 51 | ] |
| 52 | |
| 53 | [nodes.stories] |
| 54 | kind = "each" |
| 55 | over = "ids" |
| 56 | limit = "{{ inputs.count }}" |
| 57 | tree = "hacker-news-item" |
| 58 | with = { id = "{{ item }}" } |
| 59 | into = "stories" |
| 60 | next = "reply" |
| 61 | |
| 62 | [nodes.reply] |
| 63 | kind = "done" |
| 64 | say = "Top of Hacker News: {{ state.stories[0].title }} — {{ state.stories[0].url }}" |
| 65 | output = { stories = "{{ state.stories }}" } |
| 66 | |
| 67 | [nodes.nothing] |
| 68 | kind = "fail" |
| 69 | reason = "no-stories" |
| 70 | say = "Hacker News returned an empty front page." |
| 71 | |
| 72 | [nodes.hn-down] |
| 73 | kind = "fail" |
| 74 | reason = "upstream-down" |
| 75 | say = "Hacker News answered {{ state.ids_resp.status }}; try again later." |
| 76 | |
| 77 | [nodes.unexpected] |
| 78 | kind = "fail" |
| 79 | reason = "unexpected-response" |
| 80 | say = "Hacker News answered {{ state.ids_resp.status }} with something this tree does not recognise." |