]> gitweb.factorcode.org Git - factor.git/blob - extra/marvel/marvel.factor
mason: show git SHA1 and timestamp of last completed build
[factor.git] / extra / marvel / marvel.factor
1 ! Copyright (C) 2014 Doug Coleman.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: assocs checksums checksums.md5 http.client json.reader
4 kernel math.parser namespaces sequences strings system urls ;
5 IN: marvel
6
7 ! http://developer.marvel.com/docs
8 ! Register for an api key.
9 SYMBOLS: marvel-base-url marvel-public-key marvel-private-key ;
10
11 marvel-base-url [ "http://gateway.marvel.com/v1/public/" ] initialize
12
13 : calculate-hash ( ts -- hash )
14     number>string
15     marvel-private-key get
16     marvel-public-key get
17     3append md5 checksum-bytes bytes>hex-string ;
18
19 :: make-marvel-params-url ( endpoint params -- url )
20     nano-count
21     [ "ts" params set-at ]
22     [ calculate-hash "hash" params set-at ] bi
23     marvel-public-key get "apikey" params set-at
24     marvel-base-url get endpoint [ >url ] bi@ derive-url
25     params [ swap set-query-param ] assoc-each ;
26
27 : make-marvel-url ( endpoint -- string )
28     H{ } clone make-marvel-params-url ;
29
30 : get-marvel-params-url ( endpoint params -- data )
31     make-marvel-params-url http-get nip >string json> ;
32
33 : get-marvel-url ( endpoint -- data )
34     H{ } clone get-marvel-params-url ;
35
36 : get-characters ( -- json ) "characters" get-marvel-url ;
37 : get-comics ( -- json ) "comics" get-marvel-url ;
38 : get-creators ( -- json ) "creators" get-marvel-url ;
39 : get-events ( -- json ) "events" get-marvel-url ;
40 : get-series ( -- json ) "series" get-marvel-url ;
41 : get-stories ( -- json ) "stories" get-marvel-url ;