factor/extra/bit/ly/ly.factor

34 lines
849 B
Factor
Raw Normal View History

2010-04-13 01:54:30 -04:00
! Copyright (C) 2010 Slava Pestov.
! See http://factorcode.org/license.txt for BSD license.
USING: assocs http.client json.reader kernel namespaces urls ;
IN: bit.ly
SYMBOLS: login api-key ;
<PRIVATE
2010-04-13 04:43:10 -04:00
: of ( assoc key -- value ) swap at ;
2010-04-13 01:54:30 -04:00
: make-request ( long-url -- request )
"http://api.bit.ly/v3/shorten" >url
login get "login" set-query-param
api-key get "apiKey" set-query-param
"json" "format" set-query-param
swap "longUrl" set-query-param ;
2010-04-13 03:54:36 -04:00
ERROR: bad-response json status ;
: check-response ( json -- json )
2010-04-13 04:43:10 -04:00
dup "status_code" of 200 = [
dup "status_txt" of
2010-04-13 03:54:36 -04:00
bad-response
] unless ;
2010-04-13 01:54:30 -04:00
: parse-response ( response data -- short-url )
2010-04-13 04:43:10 -04:00
nip json> check-response "data" of "url" of ;
2010-04-13 01:54:30 -04:00
PRIVATE>
: shorten-url ( long-url -- short-url )
make-request http-get parse-response ;