diff options
author | elioat <elioat@tilde.institute> | 2022-12-02 15:35:24 -0500 |
---|---|---|
committer | elioat <elioat@tilde.institute> | 2022-12-02 15:35:24 -0500 |
commit | a76b6b2e8c2d8f3c0b4f913f11ca7c27500eb02a (patch) | |
tree | 64955eaa8862e68e2c07fb964236622506e43ad7 /src | |
parent | 32a3bfef3e4307a80e83aa8be825f9e9240124f5 (diff) | |
download | december-2022-a76b6b2e8c2d8f3c0b4f913f11ca7c27500eb02a.tar.gz |
Added a function that builds the data structure for our pokedex entry
Diffstat (limited to 'src')
-rw-r--r-- | src/pokedex.rkt | 25 |
1 files changed, 14 insertions, 11 deletions
diff --git a/src/pokedex.rkt b/src/pokedex.rkt index 16f59c0..0302cb0 100644 --- a/src/pokedex.rkt +++ b/src/pokedex.rkt @@ -14,7 +14,7 @@ ;; basic GET request (require net/url) (require json) -(require nested-hash) ;; package that makes traversing nested hash tables a wee bit easier +(require nested-hash) ;; package that makes traversing nested hash tables a wee bit easier, not certain if I'll need it ;; API URL (define *POKE-API* "https://pokeapi.co/api/v2/pokemon/") @@ -22,17 +22,20 @@ ;; info about this form, ;; https://medium.com/chris-opperwall/practical-racket-using-a-json-rest-api-3d85eb11cc2d (define (get-pokemon id) + "queries the api, returns a butt ton of info" (call/input-url (string->url (~a *POKE-API* id)) ; ~a, like string-append but appends *either* strings or numbers to a string get-pure-port (compose string->jsexpr port->string))) -;; TOMORROW: I'll explore referencing data from returns hash-table, -;; https://docs.racket-lang.org/reference/hashtables.html - -;; (get-pokemon "ditto") -;; (get-pokemon 123) - -(define poke-data (get-pokemon 123)) - -(nested-hash-ref poke-data 'name) ;; this will return "scyther" -(nested-hash-ref poke-data 'id) ;; this will return 123 \ No newline at end of file +(define (dex-entry id) + "selects the info we need from the api, builds a hash table of the data that will build the entry" + (let ([poke-data (get-pokemon id)]) + (define entry-data (make-hash)) + (hash-set! entry-data "name" (hash-ref poke-data 'name)) + (hash-set! entry-data "id" (hash-ref poke-data 'id)) + (hash-set! entry-data "sprite" (hash-ref (hash-ref poke-data 'sprites) 'front_default)) + (hash-set! entry-data "stats" (hash-ref poke-data 'stats)) + (hash-set! entry-data "types" (hash-ref poke-data 'types)) + entry-data)) + +(dex-entry 12) \ No newline at end of file |