Previously, I wrote about Clojure and Ring in an introductory post where a small debugging application called Echo was developed. If you haven't looked at that post and are looking for an introduction to Ring I suggest you do and then come back here.
For this post, I've created another sample application called
ring-next which contains routines to demonstrate a number of concepts in Ring. These are:
As stated in the Ring repository, it is a library that abstracts the details of HTTP into a simple API. It does this by turning HTTP requests into Clojure maps which can be inspected and modified by a handler which returns an HTTP response. The handlers are Clojure functions that you create. You are also responsible for creating the response. Ring connects your handler with the underlying web server and is responsible for taking the requests and calling your handler with the request map.
If you've had experience with Java Servlets you'll notice a pattern here but will quickly see how much simpler this is here.
Ran into a situation where an external vendor started sending csv files gzipped. The trouble was they started zipping only larger files. Since the system that ingested the files would break on a gz file I needed a routine to unzip the gz files and leave the non-zipped files alone.
def unzip_if_necessary(self, filename): print "unzip_if_necessary: " + filename f = open(filename) # Read magic number (the first 2 bytes) and rewind. magic_number = f.read(2) f.seek(0) f.close() if magic_number == '\x1f\x8b': print "gzip file" data = '' # with gzip.GzipFile(fileobj=f) as f: with gzip.open(filename, 'rb') as f: data = f.read() print "Writing to " + filename with open(filename, "w") as f: f.write(data) else: print "csv file"
Use wget to download a web page and all associated files.
$ wget -r -np -k http://domain.com/url
If you don't have wget and you are using a Mac use brew.
$ brew install wget
After taking a month off from writing blog posts I've just found something interesting to point out. The other day I found this "Advent Calendar" idea for programmers. Each day you solve a puzzle or two to unlock the day. With twenty five days to complete it looks like an interesting activity for the month of December.
If you are curious a bunch of Clojurians are listing their solutions repos here:
gpg: signing failed: Inappropriate ioctl for device
$ GPG_TTY=$(tty) $ export GPG_TTY