summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSolomon Peachy <pizza@shaftnet.org>2013-08-23 22:19:06 -0400
committerSolomon Peachy <pizza@shaftnet.org>2013-08-23 22:19:06 -0400
commit22e762673aa6c0a439938708c4f67de83f5675e9 (patch)
tree9350860ec31a7b8a3a7494d4e43bbf71a79aec8b
downloadslp_misc-22e762673aa6c0a439938708c4f67de83f5675e9.tar.gz
slp_misc-22e762673aa6c0a439938708c4f67de83f5675e9.tar.bz2
slp_misc-22e762673aa6c0a439938708c4f67de83f5675e9.zip
Add the code I wrote for my coding challenge when I interviewed with
Zappos.
-rw-r--r--zappos/ZapposCodingChallenge.java187
-rw-r--r--zappos/images/14078-p-DETAILED.jpgbin0 -> 17293 bytes
-rw-r--r--zappos/images/15643-p-DETAILED.jpgbin0 -> 15838 bytes
-rw-r--r--zappos/images/1632312-p-DETAILED.jpgbin0 -> 36849 bytes
-rw-r--r--zappos/images/944325-p-DETAILED.jpgbin0 -> 21159 bytes
-rw-r--r--zappos/json-simple-1.1.1.jarbin0 -> 23737 bytes
-rw-r--r--zappos/skus.txt8
7 files changed, 195 insertions, 0 deletions
diff --git a/zappos/ZapposCodingChallenge.java b/zappos/ZapposCodingChallenge.java
new file mode 100644
index 0000000..e83d9a0
--- /dev/null
+++ b/zappos/ZapposCodingChallenge.java
@@ -0,0 +1,187 @@
+//package org.shaftnet.zappos;
+
+import java.io.*;
+import java.util.*;
+import java.net.URL;
+import java.net.MalformedURLException;
+import org.json.simple.*;
+import org.json.simple.parser.*;
+
+/**
+ * Sample Zappos Coding Challenge
+ *
+ * (c) 2013 Solomon Peachy <slp@shaftnet.org>, All Rights Reserved
+ *
+ * Some comments:
+ *
+ * The nature of this assignment doesn't lend itself well towards
+ * OO programming as it's inherently procedural. I could set this up to
+ * be more OO, but it would feel forced. This is actually something that
+ * would more naturally be implemented using something like perl.
+ *
+ * You will need the json-simple jar, available here:
+ *
+ * http://json-simple.googlecode.com/files/json-simple-1.1.1.jar
+ *
+ * To compile:
+ * java -cp *jar *java
+ *
+ * To run:
+ * java -cp *jar:. ZapposCodingChallenge filename
+ *
+ * Other comments:
+ *
+ * This doesn't have the degree of robustness and error reporting I'd like,
+ * but my Java skills are a little rusty. There are probably better ways of handling
+ * some of these things, but this is the first Java code I've written in more than
+ * ten years.
+ */
+public class ZapposCodingChallenge {
+
+ /* Note that these should be configurable, ideally via a config file
+ or something like that. However, due to time constraints I'm leaving
+ this as a WIBNI. */
+ private static final String apiKey = "52ddafbe3ee659bad97fcce7c53592916a6bfd73";
+ private static final String skuPattern = "\\s+(\\d*).*";
+ private static final String baseURL = "http://api.zappos.com/Product/";
+
+ /**
+ * This is a dummy instantiation; this class is static.
+ */
+ public ZapposCodingChallenge() {
+
+ }
+
+ /**
+ * Let's do this thing!
+ */
+ public static void main(String[] args) {
+ if (args.length == 0) {
+ System.out.println("Usage: ZapposCodingChallenge filename");
+ System.exit(0);
+ }
+
+ String filename = args[0];
+
+ /* Initialize a new JSON parser */
+ JSONParser parser = new JSONParser();
+
+ /* Create the images directory */
+ File f = new File("images");
+ if (!f.exists()) {
+ if (!f.mkdir()) {
+ System.out.println("Can't create images directory!");
+ System.exit(4);
+ }
+ }
+
+ /* Open filename */
+ try {
+ BufferedReader br = new BufferedReader(new FileReader(filename));
+ try {
+ /* Read file in, assuming one line per SKU.. */
+ while (true) {
+ String sku = br.readLine();
+ if (sku == null) {
+ break;
+ }
+
+ /* Sanitize the SKUs.
+ This is inefficient; is faster to predefine the regex
+ and compile it
+ */
+ sku = sku.replaceAll(skuPattern, "$1");
+ if (sku.length() == 0) {
+ continue;
+ }
+
+ System.out.println("Attempting to fetch: " + sku);
+
+ /* Fetch URL */
+ StringBuffer buffer = new StringBuffer();
+ try {
+ URL url = new URL(baseURL + sku + "?key=" + apiKey);
+ InputStream is = url.openStream();
+ int ptr = 0;
+ while ((ptr = is.read()) != -1) {
+ buffer.append((char)ptr);
+ }
+ is.close();
+ } catch (MalformedURLException e) {
+ System.err.println(e);
+ System.exit(2);
+ } catch (IOException e) {
+ System.out.println("Failed to fetch: " + sku);
+ System.err.println(e);
+ continue;
+ }
+
+ /* Parse the JSON response */
+ try {
+ /* Stupid json library. Wish I had time to find something better. */
+ ContainerFactory containerFactory = new ContainerFactory(){
+ public List creatArrayContainer() {
+ return new LinkedList();
+ }
+
+ public Map createObjectContainer() {
+ return new LinkedHashMap();
+ }
+ };
+
+ Map top = (Map)parser.parse(buffer.toString(), containerFactory);
+ if (top.get("statusCode").toString().equals("200")) {
+ LinkedList ll = (LinkedList) top.get("product");
+ Map jo = (Map) ll.get(0);
+
+ String defaultImageUrl = (String) jo.get("defaultImageUrl");
+
+ /* And now fetch the Image, and drop in the right place.
+ This doesn't cleanly handle the situation where the file exists already. */
+ try {
+ URL url = new URL(defaultImageUrl);
+ String path = url.getPath();
+
+ File fo = new File("images/" + path.substring(path.lastIndexOf("/") + 1));
+ FileOutputStream fw = new FileOutputStream(fo);
+
+ InputStream is = url.openStream();
+
+ /* This seems pretty inefficient, but it works. */
+ byte b[] = new byte[1024];
+ int c;
+ while ((c = is.read(b)) > 0) {
+ fw.write(b, 0, c);
+ }
+ fw.close();
+ is.close();
+ } catch (MalformedURLException e) {
+ System.err.println(e);
+ System.exit(2);
+ } catch (IOException e) {
+ System.out.println("Failed to fetch: " + sku);
+ System.err.println(e);
+ continue;
+ }
+
+ }
+ } catch (ParseException e) {
+ System.out.println("Failed to parse response!");
+ System.err.println(e);
+ continue;
+ }
+ }
+ } catch (EOFException e) {
+ /* We're done, ignore the exception. */
+ }
+ br.close();
+ } catch (IOException e) {
+ System.err.println(e);
+ System.exit(1);
+ }
+
+ System.exit(0);
+ }
+
+
+}
diff --git a/zappos/images/14078-p-DETAILED.jpg b/zappos/images/14078-p-DETAILED.jpg
new file mode 100644
index 0000000..d2503e0
--- /dev/null
+++ b/zappos/images/14078-p-DETAILED.jpg
Binary files differ
diff --git a/zappos/images/15643-p-DETAILED.jpg b/zappos/images/15643-p-DETAILED.jpg
new file mode 100644
index 0000000..0d4a944
--- /dev/null
+++ b/zappos/images/15643-p-DETAILED.jpg
Binary files differ
diff --git a/zappos/images/1632312-p-DETAILED.jpg b/zappos/images/1632312-p-DETAILED.jpg
new file mode 100644
index 0000000..631ee16
--- /dev/null
+++ b/zappos/images/1632312-p-DETAILED.jpg
Binary files differ
diff --git a/zappos/images/944325-p-DETAILED.jpg b/zappos/images/944325-p-DETAILED.jpg
new file mode 100644
index 0000000..14a2aa3
--- /dev/null
+++ b/zappos/images/944325-p-DETAILED.jpg
Binary files differ
diff --git a/zappos/json-simple-1.1.1.jar b/zappos/json-simple-1.1.1.jar
new file mode 100644
index 0000000..66347a6
--- /dev/null
+++ b/zappos/json-simple-1.1.1.jar
Binary files differ
diff --git a/zappos/skus.txt b/zappos/skus.txt
new file mode 100644
index 0000000..1e83335
--- /dev/null
+++ b/zappos/skus.txt
@@ -0,0 +1,8 @@
+107999
+ZCN24365
+7860495436
+
+7860495
+7554973
+ 7324204
+