Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions src/nyc/c4q/ramonaharrison/model/Event.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package nyc.c4q.ramonaharrison.model;

import org.json.simple.JSONObject;

/**
* Created by Rafael on 9/14/16.
*/
public class Event {

private String year;
private String text;


public Event(JSONObject json) {

if (json.containsKey("year")) {
this.year = (String) json.get("year");
}
if (json.containsKey("text")) {
this.text = (String) json.get("text");
}
}

public String getYear() {
return year;
}

public String getText() {
return text;
}
}
7 changes: 7 additions & 0 deletions src/nyc/c4q/ramonaharrison/model/Field.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package nyc.c4q.ramonaharrison.model;

/**
* Created by Mac on 9/11/16.
*/
public class Field {
}
110 changes: 110 additions & 0 deletions src/nyc/c4q/ramonaharrison/model/Profile.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
package nyc.c4q.ramonaharrison.model;

import org.json.simple.JSONObject;

/**
* Created by Rafael on 9/11/16.
*/
public class Profile {
private String firstName;
private String lastName;
private String realName;
private String email;
private String skype;
private String phone;
private String image24;
private String image32;
private String image48;
private String image72;
private String image192;
private String image512;

public Profile(JSONObject json) {

if (json.containsKey("first_name")) {
this.firstName = (String) json.get("first_name");
}
if (json.containsKey("last_name")) {
this.lastName = (String) json.get("last_name");
}
if (json.containsKey("real_name")) {
this.realName = (String) json.get("real_name");
}
if (json.containsKey("email")) {
this.email = (String) json.get("email");
}
if (json.containsKey("skype")) {
this.skype = (String) json.get("skype");
}
if (json.containsKey("phone")) {
this.phone = (String) json.get("phone");
}
if (json.containsKey("image_24")) {
this.image24 = (String) json.get("image_24");
}
if (json.containsKey("image_32")) {
this.image32 = (String) json.get("image_32");
}
if (json.containsKey("image_48")) {
this.image48 = (String) json.get("image_48");
}
if (json.containsKey("image_72")) {
this.image72 = (String) json.get("image_72");
}
if (json.containsKey("image_192")) {
this.image192 = (String) json.get("image_192");
}
if (json.containsKey("image_512")) {
this.image512 = (String) json.get("image_512");
}
}

public String getFirstName() {
return firstName;
}

public String getLastName() {
return lastName;
}

public String getRealName() {
return realName;
}

public String getEmail() {
return email;
}

public String getSkype() {
return skype;
}

public String getPhone() {
return phone;
}

public String getImage24() {
return image24;
}

public String getImage32() {
return image32;
}

public String getImage48() {
return image48;
}

public String getImage72() {
return image72;
}

public String getImage192() {
return image192;
}

public String getImage512() {
return image512;
}
}

131 changes: 115 additions & 16 deletions src/nyc/c4q/ramonaharrison/model/User.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
package nyc.c4q.ramonaharrison.model;

import org.json.simple.JSONArray;
import org.json.simple.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
* Created by Ramona Harrison
* on 8/26/16
Expand All @@ -13,24 +17,119 @@

public class User {

// TODO: implement private fields for each of the following user JSON keys:
// "id"
// "name"
// "deleted"
// "color"
// "profile"
// "is_admin"
// "is_owner"
// "is_primary_owner"
// "is_restricted"
// "is_ultra_restricted"
// "has_2fa"
// "two_factor_type"
// "has_files"
private String id;
private String name;
private boolean deleted;
private String color;
private List<Profile> profiles;
private boolean isAdmin;
private boolean isOwner;
private boolean isPrimaryOwner;
private boolean isRestricted;
private boolean isUltraRestricted;
private boolean has2FA;
private String twoFactorType;
private boolean hasFiles;


public User(JSONObject json) {
// TODO: parse a user from the incoming json
if (json.containsKey("id")) {
this.id = (String) json.get("id");
}
if (json.containsKey("name")) {
this.name = (String) json.get("name");
}
if (json.containsKey("deleted")) {
this.deleted = (boolean) json.get("deleted");
}
if (json.containsKey("deleted")) {
this.deleted = (boolean) json.get("deleted");
}
if (json.containsKey("color")) {
this.color = (String) json.get("color");
}
if (json.containsKey("profiles")) {
JSONArray jsonProfiles = (JSONArray) json.get("profiles");
this.profiles = new ArrayList<Profile>();
for (int i = 0; i < jsonProfiles.size(); i++) {
Profile profile = new Profile((JSONObject) jsonProfiles.get(i));
}
}
if (json.containsKey("is_admin")) {
this.isAdmin = (boolean) json.get("is_admin");
}
if (json.containsKey("is_owner")) {
this.isOwner = (boolean) json.get("is_owner");
}
if (json.containsKey("is_primary_owner")) {
this.isPrimaryOwner = (boolean) json.get("is_primary_owner");
}
if (json.containsKey("is_restricted")) {
this.isRestricted = (boolean) json.get("is_restricted");
}
if (json.containsKey("is_ultra_restricted")) {
this.isUltraRestricted = (boolean) json.get("is_ultra_restricted");
}
if (json.containsKey("has_2fa")) {
this.has2FA = (boolean) json.get("has_2fa");
}
if (json.containsKey("two_factor_type")) {
this.twoFactorType = (String) json.get("two_factor_type");
}
if (json.containsKey("has_files")) {
this.hasFiles = (boolean) json.get("has_files");
}
}

public String getId() {
return id;
}

public String getName() {
return name;
}

public boolean isDeleted() {
return deleted;
}

public String getColor() {
return color;
}

public List<Profile> getProfiles() {
return profiles;
}

public boolean isAdmin() {
return isAdmin;
}

// TODO add getters to access private fields
public boolean isOwner() {
return isOwner;
}

public boolean isPrimaryOwner() {
return isPrimaryOwner;
}

public boolean isRestricted() {
return isRestricted;
}

public boolean isUltraRestricted() {
return isUltraRestricted;
}

public boolean isHas2FA() {
return has2FA;
}

public String getTwoFactorType() {
return twoFactorType;
}

public boolean isHasFiles() {
return hasFiles;
}
}
91 changes: 91 additions & 0 deletions src/nyc/c4q/ramonaharrison/network/HTTP.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package nyc.c4q.ramonaharrison.network;

/**
* Created by Rafael on 9/14/16.
*/

import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;

import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

/**
* Simplified API for HTTP.
*/
public class HTTP {
/**
* Reads from 'reader' by 'blockSize' until end-of-stream, and returns its complete contents.
*/
private static String readAll(InputStreamReader reader, int blockSize) throws IOException {
final char buffer[] = new char[blockSize];
StringBuilder builder = new StringBuilder();
while (true) {
final int readSize = reader.read(buffer);
if (readSize >= 0)
builder.append(buffer, 0, readSize);
else
break;
}
return builder.toString();
}

/**
* Returns from 'reader' until end-of-stream, and returns its complete contents.
*/
private static String readAll(InputStreamReader reader) throws IOException {
return readAll(reader, 1024 * 1024);
}

/**
* Interprets a string as a URL. If the string isn't a valid URL, prints an error message and returns null.
*/
public static URL stringToURL(String string) {
try {
return new URL(string);
} catch (MalformedURLException exception) {
System.err.println("invalid URL: " + string + ": " + exception);
return null;
}
}

/**
* Retrieves JSON from a URL.
* <p>
* Opens a connection to the URL, makes a request, and retrieves the response. Returns the body as a JSONObject.
* If the URL cannot be opened or the response cannot be read, prints an error message and returns null.
*/
public static JSONObject get(URL url) {
try {
final HttpURLConnection connection = (HttpURLConnection) url.openConnection();
final InputStreamReader reader = new InputStreamReader(connection.getInputStream());
try {
return stringToJSON(readAll(reader));
} finally {
reader.close();
}
} catch (IOException exception) {
System.err.println("can't open URL: " + url + ": " + exception);
return null;
}
}

/**
* Interprets a string as JSON. If the string isn't valid JSON, prints an error message and returns null.
*/
private static JSONObject stringToJSON(String jsonString) {
JSONParser parser = new JSONParser();
try {
return (JSONObject) parser.parse(jsonString);
} catch (ParseException e) {
System.err.println("invalid json: " + e);
return null;
}
}

}
Loading