Skip to content

Commit

Permalink
JSON Binding Interface and Friend Object
Browse files Browse the repository at this point in the history
  • Loading branch information
hatboysam committed Dec 31, 2013
1 parent 4eace1e commit 74ec086
Show file tree
Hide file tree
Showing 5 changed files with 102 additions and 17 deletions.
43 changes: 43 additions & 0 deletions src/main/java/com/habosa/javasnap/Friend.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package com.habosa.javasnap;

import org.json.JSONException;
import org.json.JSONObject;

/**
* Author: samstern
* Date: 12/31/13
*/
public class Friend implements JSONBinder<Friend> {

private static final String USERNAME_KEY = "name";
private static final String DISPLAY_NAME_KEY = "display";

private String username;
private String displayName;

public Friend() { }

public Friend bind(JSONObject obj) {
try {
this.username = obj.getString(USERNAME_KEY);
this.displayName = obj.getString(DISPLAY_NAME_KEY);
} catch (JSONException e) {
return this;
}

return this;
}

public String getUsername() {
return username;
}

public String getDisplayName() {
return displayName;
}

@Override
public String toString() {
return username + " ~> " + displayName;
}
}
13 changes: 13 additions & 0 deletions src/main/java/com/habosa/javasnap/JSONBinder.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.habosa.javasnap;

import org.json.JSONObject;

/**
* Author: samstern
* Date: 12/31/13
*/
public interface JSONBinder<T> {

public T bind(JSONObject obj);

}
1 change: 1 addition & 0 deletions src/main/java/com/habosa/javasnap/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import java.io.File;
import java.io.FileOutputStream;
import java.util.Arrays;
import java.util.Scanner;

public class Main {
Expand Down
24 changes: 14 additions & 10 deletions src/main/java/com/habosa/javasnap/Snap.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
* Author: samstern
* Date: 12/30/13
*/
public class Snap {
public class Snap implements JSONBinder<Snap> {

public static int TYPE_IMAGE = 0;
public static int TYPE_VIDEO = 1;
Expand All @@ -36,35 +36,39 @@ public class Snap {
private int state;
private int time;

public Snap(JSONObject snapObject) {
public Snap() { }

public Snap bind(JSONObject obj) {
// Check for fields that always exist
try {
this.id = snapObject.getString(ID_KEY);
this.type = snapObject.getInt(TYPE_KEY);
this.state = snapObject.getInt(STATE_KEY);
this.id = obj.getString(ID_KEY);
this.type = obj.getInt(TYPE_KEY);
this.state = obj.getInt(STATE_KEY);
} catch (JSONException e) {
e.printStackTrace();
return;
return this;
}

// Check sender or recipient separately
try {
this.sender = snapObject.getString(SENDER_KEY);
this.sender = obj.getString(SENDER_KEY);
} catch (JSONException e) {
// Ignore
}
try {
this.recipient = snapObject.getString(RECIPENT_KEY);
this.recipient = obj.getString(RECIPENT_KEY);
} catch (JSONException e) {
// Ignore
}

// Check for time separately because it may not exist.
try {
this.time = snapObject.getInt(TIME_KEY);
this.time = obj.getInt(TIME_KEY);
} catch (JSONException e) {
return;
return this;
}

return this;
}

/**
Expand Down
38 changes: 31 additions & 7 deletions src/main/java/com/habosa/javasnap/Snapchat.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,7 @@
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.*;

/**
* Author: samstern
Expand All @@ -30,6 +28,7 @@ public class Snapchat {
public static final String AUTH_TOKEN_KEY = "auth_token";
public static final String ID_KEY = "id";
public static final String SNAPS_KEY = "snaps";
public static final String FRIENDS_KEY = "friends";

public static final String LOGIN_PATH = "bq/login";
public static final String UPLOAD_PATH = "ph/upload";
Expand Down Expand Up @@ -72,14 +71,39 @@ public static JSONObject login(String username, String password) {
public static Snap[] getSnaps(JSONObject loginObject) {
try {
JSONArray snapArr = loginObject.getJSONArray(SNAPS_KEY);
int length = snapArr.length();
Snap[] result = new Snap[length];
List<Snap> resultList = bindArray(snapArr, Snap.class);
return resultList.toArray(new Snap[resultList.size()]);
} catch (JSONException e) {
return new Snap[0];
}
}

public static Friend[] getFriends(JSONObject loginObject) {
try {
JSONArray friendsArr = loginObject.getJSONArray(FRIENDS_KEY);
List<Friend> resultList = bindArray(friendsArr, Friend.class);
return resultList.toArray(new Friend[resultList.size()]);
} catch (JSONException e) {
return new Friend[0];
}
}

private static <T> List<T> bindArray(JSONArray arr, Class<? extends JSONBinder<T>> clazz) {
try {
int length = arr.length();
List<T> result = new ArrayList<T>();
for (int i = 0; i < length; i++) {
result[i] = new Snap(snapArr.getJSONObject(i));
JSONObject obj = arr.getJSONObject(i);
T bound = clazz.newInstance().bind(obj);
result.add(bound);
}
return result;
} catch (JSONException e) {
return new Snap[0];
return new ArrayList<T>();
} catch (InstantiationException e) {
return new ArrayList<T>();
} catch (IllegalAccessException e) {
return new ArrayList<T>();
}
}

Expand Down

0 comments on commit 74ec086

Please sign in to comment.