Created
May 14, 2010 03:57
-
-
Save hypomodern/400794 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# A webserver version of le_twittre! | |
# usage: io twitter_frontend_server.io | |
# "API" : http://localhost:8080/?user=<username>, otherwise you get the public timeline. | |
# There's no native JSON tools, so I found this janky one. This is how you import files, basically. | |
# As a side-note, the autoloader is pretty cool. Check the Io Guide for more. | |
doFile("json.io") | |
server := HttpServer clone do( | |
setPort(8080) | |
renderResponse := method(request, response, | |
user := request parameters at("user") | |
twitter_url := if(user, | |
"http://api.twitter.com/1/statuses/user_timeline.json?id=#{user}" interpolate, | |
"http://api.twitter.com/1/statuses/public_timeline.json" | |
) | |
twits := URL with(twitter_url) @fetch | |
# better than if(user, user, user = "the Public")! | |
(user) ifNil(user = "the Public") | |
response body appendSeq("<h1>Last 20 Tweets by " .. user .. "</h1>") | |
parsed := formatAndParseJson(twits) | |
response body appendSeq("<ul>") | |
parsed foreach( tweet, | |
response body appendSeq("<li><strong>" .. tweet user name .. ":</strong> " .. tweet text .. "</li>") | |
) | |
response body appendSeq("</ul>") | |
) | |
# the JSON lib I found doesn't handle arrays (???). | |
formatAndParseJson := method(theJson, | |
formatForSillyJsonLib := theJson removeAt(0) removeLast | |
rebuilt := formatForSillyJsonLib split("},{") map(thing, | |
thing removePrefix("{") | |
thing removeSuffix("}") | |
"{ #{thing} }" interpolate | |
) | |
rebuilt map(thing, thing parseJson) | |
) | |
) | |
server start # and now our server is up. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment