From f8d23d34925fb30dad547491c47a717c8a3a5917 Mon Sep 17 00:00:00 2001 From: Rafael Soto Date: Sun, 11 Sep 2016 15:38:10 -0400 Subject: [PATCH 1/6] Fixed User class, added Profile class --- src/nyc/c4q/ramonaharrison/model/Field.java | 7 + src/nyc/c4q/ramonaharrison/model/Profile.java | 110 +++++++++++++++ src/nyc/c4q/ramonaharrison/model/User.java | 131 +++++++++++++++--- 3 files changed, 232 insertions(+), 16 deletions(-) create mode 100644 src/nyc/c4q/ramonaharrison/model/Field.java create mode 100644 src/nyc/c4q/ramonaharrison/model/Profile.java diff --git a/src/nyc/c4q/ramonaharrison/model/Field.java b/src/nyc/c4q/ramonaharrison/model/Field.java new file mode 100644 index 0000000..cea7c8d --- /dev/null +++ b/src/nyc/c4q/ramonaharrison/model/Field.java @@ -0,0 +1,7 @@ +package nyc.c4q.ramonaharrison.model; + +/** + * Created by Mac on 9/11/16. + */ +public class Field { +} diff --git a/src/nyc/c4q/ramonaharrison/model/Profile.java b/src/nyc/c4q/ramonaharrison/model/Profile.java new file mode 100644 index 0000000..9ac584f --- /dev/null +++ b/src/nyc/c4q/ramonaharrison/model/Profile.java @@ -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; + } +} + diff --git a/src/nyc/c4q/ramonaharrison/model/User.java b/src/nyc/c4q/ramonaharrison/model/User.java index bc309eb..a6bb913 100644 --- a/src/nyc/c4q/ramonaharrison/model/User.java +++ b/src/nyc/c4q/ramonaharrison/model/User.java @@ -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 @@ -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 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(); + 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 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; + } } From 7636592bdbb1e54f153322ac1f7c07239ee8f225 Mon Sep 17 00:00:00 2001 From: Akasha Archere Date: Tue, 13 Sep 2016 15:23:08 -0400 Subject: [PATCH 2/6] Completed TODOs in Attachment class --- src/nyc/c4q/ramonaharrison/Main.java | 4 +- .../c4q/ramonaharrison/model/Attachment.java | 162 ++++++++++++++++-- src/nyc/c4q/ramonaharrison/model/Field.java | 43 +++++ 3 files changed, 192 insertions(+), 17 deletions(-) create mode 100644 src/nyc/c4q/ramonaharrison/model/Field.java diff --git a/src/nyc/c4q/ramonaharrison/Main.java b/src/nyc/c4q/ramonaharrison/Main.java index 933e92b..9ef8f62 100644 --- a/src/nyc/c4q/ramonaharrison/Main.java +++ b/src/nyc/c4q/ramonaharrison/Main.java @@ -15,10 +15,10 @@ public static void main(String[] args) { myBot.listMessages(Slack.BOTS_CHANNEL_ID); // Post "Hello, world!" to the #bots channel - //myBot.sendMessage("Hello, world!"); + myBot.sendMessageToBotsChannel("Hello, world!"); // Post a pineapple photo to the #bots channel - //myBot.sendMessage("http://weknowyourdreams.com/images/pineapple/pineapple-07.jpg"); + myBot.sendMessageToBotsChannel("http://weknowyourdreams.com/images/pineapple/pineapple-07.jpg"); } } \ No newline at end of file diff --git a/src/nyc/c4q/ramonaharrison/model/Attachment.java b/src/nyc/c4q/ramonaharrison/model/Attachment.java index 8b9d1ef..26c8185 100644 --- a/src/nyc/c4q/ramonaharrison/model/Attachment.java +++ b/src/nyc/c4q/ramonaharrison/model/Attachment.java @@ -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 @@ -14,26 +18,154 @@ public class Attachment { // TODO: implement private fields for each of the following attachment JSON keys: - // "fallback" - // "color" - // "pretext" - // "author_name" - // "author_link" - // "author_icon" - // "title" - // "title_link" - // "text" - // "fields" - // "image_url" - // "thumb_url" - // "footer" - // "footer_icon" - // "ts" + private String fallback; + private String color; + private String pretext; + private String authorName; + private String authorLink; + private String authorIcon; + private String title; + private String titleLink; + private String text; + private List fields; + private String imageUrl; + private String thumbUrl; + private String footer; + private String footerIcon; + private long ts; // timestamp + public Attachment(JSONObject json) { // TODO: parse an attachment from the incoming json + // check for null before assuming item is in json object + + if (json.containsKey("fallback")) { + this.fallback = (String) json.get("fallback"); + } + + if (json.containsKey("color")) { + this.color = (String) json.get("color"); + } + + if (json.containsKey("pretext")) { + this.pretext= (String) json.get("pretext"); + } + + if (json.containsKey("author_name")) { + this.authorName = (String) json.get("author_name"); + } + + if (json.containsKey("author_link")) { + this.authorLink = (String) json.get("author_link"); + } + + if (json.containsKey("author_icon")) { + this.authorIcon = (String) json.get("author_icon"); + } + + if (json.containsKey("title")) { + this.title = (String) json.get("title"); + } + + if (json.containsKey("title_link")) { + this.titleLink = (String) json.get("title_link"); + } + + if (json.containsKey("text")) { + this.text = (String) json.get("text"); + } + + if (json.containsKey("fields")) { + JSONArray jsonFields = (JSONArray) json.get("fields"); + this.fields = new ArrayList(); + for (int i = 0; i < jsonFields.size(); i++) { + Field field = new Field((JSONObject) jsonFields.get(i)); + } + } + + if (json.containsKey("image_url")) { + this.imageUrl = (String) json.get("image_url"); + } + + if (json.containsKey("thumb_rl")) { + this.thumbUrl = (String) json.get("thumb_url"); + } + + if (json.containsKey("footer")) { + this.footer = (String) json.get("footer"); + } + + if (json.containsKey("footer_icon")) { + this.footerIcon = (String) json.get("footer_icon"); + } + + // ts = timestamp + if (json.containsKey("ts")) { + this.ts = (long) json.get("ts"); + } + } // TODO add getters to access private fields + // command + N to generate Getter methods + + public String getFallback() { + return fallback; + } + + public String getColor() { + return color; + } + + public String getPretext() { + return pretext; + } + public String getAuthorName() { + return authorName; + } + + public String getAuthorLink() { + return authorLink; + } + + public String getAuthorIcon() { + return authorIcon; + } + + public String getTitle() { + return title; + } + + public String getTitleLink() { + return titleLink; + } + + public String getText() { + return text; + } + + public List getFields() { + return fields; + } + + public String getImageUrl() { + return imageUrl; + } + + public String getThumbUrl() { + return thumbUrl; + } + + public String getFooter() { + return footer; + } + + public String getFooterIcon() { + return footerIcon; + } + + public long getTs() { + return ts; + } } diff --git a/src/nyc/c4q/ramonaharrison/model/Field.java b/src/nyc/c4q/ramonaharrison/model/Field.java new file mode 100644 index 0000000..0c1e6f4 --- /dev/null +++ b/src/nyc/c4q/ramonaharrison/model/Field.java @@ -0,0 +1,43 @@ +package nyc.c4q.ramonaharrison.model; + +import org.json.simple.JSONObject; + +/** + * Created by akashaarcher on 9/11/16. + */ +public class Field { + + private String title; + private String value; + private boolean isShort; + + + public Field(JSONObject json) { + + if (json.containsKey("title")) { + this.title = (String) json.get("title"); + } + + if (json.containsKey("value")) { + this.value = (String) json.get("value"); + } + + if (json.containsKey("short")) { + this.isShort = (boolean) json.get("short"); + } + + } + + + public String getTitle() { + return title; + } + + public String getValue() { + return value; + } + + public boolean isShort() { + return isShort; + } +} From 7801d944085f211dbc36c42f1c3752f7b03c5cdb Mon Sep 17 00:00:00 2001 From: Rafael Soto Date: Wed, 14 Sep 2016 15:06:15 -0400 Subject: [PATCH 3/6] Completed todos in Event.java --- src/nyc/c4q/ramonaharrison/model/Event.java | 31 +++++++++++++++++++ src/nyc/c4q/ramonaharrison/network/HTTP.java | 7 +++++ .../network/TodayInHistory.java | 7 +++++ .../response/TodayInHistoryResponse.java | 7 +++++ 4 files changed, 52 insertions(+) create mode 100644 src/nyc/c4q/ramonaharrison/model/Event.java create mode 100644 src/nyc/c4q/ramonaharrison/network/HTTP.java create mode 100644 src/nyc/c4q/ramonaharrison/network/TodayInHistory.java create mode 100644 src/nyc/c4q/ramonaharrison/network/response/TodayInHistoryResponse.java diff --git a/src/nyc/c4q/ramonaharrison/model/Event.java b/src/nyc/c4q/ramonaharrison/model/Event.java new file mode 100644 index 0000000..7c3e3ac --- /dev/null +++ b/src/nyc/c4q/ramonaharrison/model/Event.java @@ -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; + } +} diff --git a/src/nyc/c4q/ramonaharrison/network/HTTP.java b/src/nyc/c4q/ramonaharrison/network/HTTP.java new file mode 100644 index 0000000..156474d --- /dev/null +++ b/src/nyc/c4q/ramonaharrison/network/HTTP.java @@ -0,0 +1,7 @@ +package nyc.c4q.ramonaharrison.network.response; + +/** + * Created by Mac on 9/14/16. + */ +public class HTTP { +} diff --git a/src/nyc/c4q/ramonaharrison/network/TodayInHistory.java b/src/nyc/c4q/ramonaharrison/network/TodayInHistory.java new file mode 100644 index 0000000..f248b7d --- /dev/null +++ b/src/nyc/c4q/ramonaharrison/network/TodayInHistory.java @@ -0,0 +1,7 @@ +package nyc.c4q.ramonaharrison.network; + +/** + * Created by Mac on 9/14/16. + */ +public class TodayInHistory { +} diff --git a/src/nyc/c4q/ramonaharrison/network/response/TodayInHistoryResponse.java b/src/nyc/c4q/ramonaharrison/network/response/TodayInHistoryResponse.java new file mode 100644 index 0000000..354bf47 --- /dev/null +++ b/src/nyc/c4q/ramonaharrison/network/response/TodayInHistoryResponse.java @@ -0,0 +1,7 @@ +package nyc.c4q.ramonaharrison.network.response; + +/** + * Created by Mac on 9/14/16. + */ +public class TodayInHistoryResponse { +} From b4d38197dfed7e8109d19a7f19692a77e03ca9c5 Mon Sep 17 00:00:00 2001 From: Rafael Soto Date: Wed, 14 Sep 2016 15:34:31 -0400 Subject: [PATCH 4/6] Interface for TodayInHistory Completed --- src/nyc/c4q/ramonaharrison/network/HTTP.java | 90 ++++++++++++++++++- .../network/TodayInHistory.java | 14 ++- .../response/TodayInHistoryResponse.java | 30 ++++++- 3 files changed, 127 insertions(+), 7 deletions(-) diff --git a/src/nyc/c4q/ramonaharrison/network/HTTP.java b/src/nyc/c4q/ramonaharrison/network/HTTP.java index 156474d..970955b 100644 --- a/src/nyc/c4q/ramonaharrison/network/HTTP.java +++ b/src/nyc/c4q/ramonaharrison/network/HTTP.java @@ -1,7 +1,91 @@ -package nyc.c4q.ramonaharrison.network.response; +package nyc.c4q.ramonaharrison.network; /** - * Created by Mac on 9/14/16. + * 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. + *

+ * 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; + } + } + +} \ No newline at end of file diff --git a/src/nyc/c4q/ramonaharrison/network/TodayInHistory.java b/src/nyc/c4q/ramonaharrison/network/TodayInHistory.java index f248b7d..99d9573 100644 --- a/src/nyc/c4q/ramonaharrison/network/TodayInHistory.java +++ b/src/nyc/c4q/ramonaharrison/network/TodayInHistory.java @@ -1,7 +1,17 @@ package nyc.c4q.ramonaharrison.network; /** - * Created by Mac on 9/14/16. + * Created by Rafael on 9/14/16. */ +import nyc.c4q.ramonaharrison.network.response.TodayInHistoryResponse; + +import java.net.URL; + public class TodayInHistory { -} + + public static TodayInHistoryResponse getTodayInHistory() { + URL testUrl = HTTP.stringToURL("http://history.muffinlabs.com/date"); + + return new TodayInHistoryResponse(HTTP.get(testUrl)); + } +} \ No newline at end of file diff --git a/src/nyc/c4q/ramonaharrison/network/response/TodayInHistoryResponse.java b/src/nyc/c4q/ramonaharrison/network/response/TodayInHistoryResponse.java index 354bf47..80b4c75 100644 --- a/src/nyc/c4q/ramonaharrison/network/response/TodayInHistoryResponse.java +++ b/src/nyc/c4q/ramonaharrison/network/response/TodayInHistoryResponse.java @@ -1,7 +1,33 @@ package nyc.c4q.ramonaharrison.network.response; /** - * Created by Mac on 9/14/16. + * Created by Rafael on 9/14/16. */ +import nyc.c4q.ramonaharrison.model.Event; +import org.json.simple.JSONArray; +import org.json.simple.JSONObject; + + +import java.util.ArrayList; +import java.util.List; + public class TodayInHistoryResponse { -} + + private List events; + + public TodayInHistoryResponse(JSONObject json) { + + if (json.containsKey("event")) { + JSONArray array = (JSONArray) json.get("event"); + + this.events = new ArrayList(); + for (int i = 0; i < array.size(); i++) { + this.events.add(new Event((JSONObject) array.get(i))); + } + } + } + + public List getEvents() { + return events; + } +} \ No newline at end of file From a96d0b796d41fcf870ee258e48513a245c344c43 Mon Sep 17 00:00:00 2001 From: Akasha Archer Date: Wed, 14 Sep 2016 15:52:52 -0400 Subject: [PATCH 5/6] Revert "Rs0911 slack bot" --- src/nyc/c4q/ramonaharrison/model/Event.java | 31 ----- src/nyc/c4q/ramonaharrison/model/Field.java | 7 - src/nyc/c4q/ramonaharrison/model/Profile.java | 110 --------------- src/nyc/c4q/ramonaharrison/model/User.java | 131 +++--------------- src/nyc/c4q/ramonaharrison/network/HTTP.java | 91 ------------ .../network/TodayInHistory.java | 17 --- .../response/TodayInHistoryResponse.java | 33 ----- 7 files changed, 16 insertions(+), 404 deletions(-) delete mode 100644 src/nyc/c4q/ramonaharrison/model/Event.java delete mode 100644 src/nyc/c4q/ramonaharrison/model/Field.java delete mode 100644 src/nyc/c4q/ramonaharrison/model/Profile.java delete mode 100644 src/nyc/c4q/ramonaharrison/network/HTTP.java delete mode 100644 src/nyc/c4q/ramonaharrison/network/TodayInHistory.java delete mode 100644 src/nyc/c4q/ramonaharrison/network/response/TodayInHistoryResponse.java diff --git a/src/nyc/c4q/ramonaharrison/model/Event.java b/src/nyc/c4q/ramonaharrison/model/Event.java deleted file mode 100644 index 7c3e3ac..0000000 --- a/src/nyc/c4q/ramonaharrison/model/Event.java +++ /dev/null @@ -1,31 +0,0 @@ -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; - } -} diff --git a/src/nyc/c4q/ramonaharrison/model/Field.java b/src/nyc/c4q/ramonaharrison/model/Field.java deleted file mode 100644 index cea7c8d..0000000 --- a/src/nyc/c4q/ramonaharrison/model/Field.java +++ /dev/null @@ -1,7 +0,0 @@ -package nyc.c4q.ramonaharrison.model; - -/** - * Created by Mac on 9/11/16. - */ -public class Field { -} diff --git a/src/nyc/c4q/ramonaharrison/model/Profile.java b/src/nyc/c4q/ramonaharrison/model/Profile.java deleted file mode 100644 index 9ac584f..0000000 --- a/src/nyc/c4q/ramonaharrison/model/Profile.java +++ /dev/null @@ -1,110 +0,0 @@ -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; - } -} - diff --git a/src/nyc/c4q/ramonaharrison/model/User.java b/src/nyc/c4q/ramonaharrison/model/User.java index a6bb913..bc309eb 100644 --- a/src/nyc/c4q/ramonaharrison/model/User.java +++ b/src/nyc/c4q/ramonaharrison/model/User.java @@ -1,11 +1,7 @@ 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 @@ -17,119 +13,24 @@ public class User { - private String id; - private String name; - private boolean deleted; - private String color; - private List 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; - + // 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" public User(JSONObject 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(); - 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 getProfiles() { - return profiles; - } - - public boolean isAdmin() { - return isAdmin; + // TODO: parse a user from the incoming json } - 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; - } + // TODO add getters to access private fields } diff --git a/src/nyc/c4q/ramonaharrison/network/HTTP.java b/src/nyc/c4q/ramonaharrison/network/HTTP.java deleted file mode 100644 index 970955b..0000000 --- a/src/nyc/c4q/ramonaharrison/network/HTTP.java +++ /dev/null @@ -1,91 +0,0 @@ -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. - *

- * 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; - } - } - -} \ No newline at end of file diff --git a/src/nyc/c4q/ramonaharrison/network/TodayInHistory.java b/src/nyc/c4q/ramonaharrison/network/TodayInHistory.java deleted file mode 100644 index 99d9573..0000000 --- a/src/nyc/c4q/ramonaharrison/network/TodayInHistory.java +++ /dev/null @@ -1,17 +0,0 @@ -package nyc.c4q.ramonaharrison.network; - -/** - * Created by Rafael on 9/14/16. - */ -import nyc.c4q.ramonaharrison.network.response.TodayInHistoryResponse; - -import java.net.URL; - -public class TodayInHistory { - - public static TodayInHistoryResponse getTodayInHistory() { - URL testUrl = HTTP.stringToURL("http://history.muffinlabs.com/date"); - - return new TodayInHistoryResponse(HTTP.get(testUrl)); - } -} \ No newline at end of file diff --git a/src/nyc/c4q/ramonaharrison/network/response/TodayInHistoryResponse.java b/src/nyc/c4q/ramonaharrison/network/response/TodayInHistoryResponse.java deleted file mode 100644 index 80b4c75..0000000 --- a/src/nyc/c4q/ramonaharrison/network/response/TodayInHistoryResponse.java +++ /dev/null @@ -1,33 +0,0 @@ -package nyc.c4q.ramonaharrison.network.response; - -/** - * Created by Rafael on 9/14/16. - */ -import nyc.c4q.ramonaharrison.model.Event; -import org.json.simple.JSONArray; -import org.json.simple.JSONObject; - - -import java.util.ArrayList; -import java.util.List; - -public class TodayInHistoryResponse { - - private List events; - - public TodayInHistoryResponse(JSONObject json) { - - if (json.containsKey("event")) { - JSONArray array = (JSONArray) json.get("event"); - - this.events = new ArrayList(); - for (int i = 0; i < array.size(); i++) { - this.events.add(new Event((JSONObject) array.get(i))); - } - } - } - - public List getEvents() { - return events; - } -} \ No newline at end of file From 10efc1eab19c9e92d5931fcd6ce261cc3de62dc5 Mon Sep 17 00:00:00 2001 From: Akasha Archere Date: Wed, 14 Sep 2016 16:15:07 -0400 Subject: [PATCH 6/6] Initial commit of Attachment and Field --- .../c4q/ramonaharrison/model/Attachment.java | 164 ++++++++++++++++-- src/nyc/c4q/ramonaharrison/model/Field.java | 38 +++- 2 files changed, 185 insertions(+), 17 deletions(-) diff --git a/src/nyc/c4q/ramonaharrison/model/Attachment.java b/src/nyc/c4q/ramonaharrison/model/Attachment.java index 8b9d1ef..e07ddb7 100644 --- a/src/nyc/c4q/ramonaharrison/model/Attachment.java +++ b/src/nyc/c4q/ramonaharrison/model/Attachment.java @@ -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 @@ -14,26 +18,154 @@ public class Attachment { // TODO: implement private fields for each of the following attachment JSON keys: - // "fallback" - // "color" - // "pretext" - // "author_name" - // "author_link" - // "author_icon" - // "title" - // "title_link" - // "text" - // "fields" - // "image_url" - // "thumb_url" - // "footer" - // "footer_icon" - // "ts" + private String fallback; + private String color; + private String pretext; + private String authorName; + private String authorLink; + private String authorIcon; + private String title; + private String titleLink; + private String text; + private List fields; + private String imageUrl; + private String thumbUrl; + private String footer; + private String footerIcon; + private long ts; // timestamp + public Attachment(JSONObject json) { // TODO: parse an attachment from the incoming json + // check for null before assuming item is in json object + + if (json.containsKey("fallback")) { + this.fallback = (String) json.get("fallback"); + } + + if (json.containsKey("color")) { + this.color = (String) json.get("color"); + } + + if (json.containsKey("pretext")) { + this.pretext= (String) json.get("pretext"); + } + + if (json.containsKey("author_name")) { + this.authorName = (String) json.get("author_name"); + } + + if (json.containsKey("author_link")) { + this.authorLink = (String) json.get("author_link"); + } + + if (json.containsKey("author_icon")) { + this.authorIcon = (String) json.get("author_icon"); + } + + if (json.containsKey("title")) { + this.title = (String) json.get("title"); + } + + if (json.containsKey("title_link")) { + this.titleLink = (String) json.get("title_link"); + } + + if (json.containsKey("text")) { + this.text = (String) json.get("text"); + } + + if (json.containsKey("fields")) { + JSONArray jsonFields = (JSONArray) json.get("fields"); + this.fields = new ArrayList(); + for (int i = 0; i < jsonFields.size(); i++) { + Field field = new Field((JSONObject) jsonFields.get(i)); + } + } + + if (json.containsKey("image_url")) { + this.imageUrl = (String) json.get("image_url"); + } + + if (json.containsKey("thumb_rl")) { + this.thumbUrl = (String) json.get("thumb_url"); + } + + if (json.containsKey("footer")) { + this.footer = (String) json.get("footer"); + } + + if (json.containsKey("footer_icon")) { + this.footerIcon = (String) json.get("footer_icon"); + } + + // ts = timestamp + if (json.containsKey("ts")) { + this.ts = (long) json.get("ts"); + } + } // TODO add getters to access private fields + // command + N to generate Getter methods + + public String getFallback() { + return fallback; + } + + public String getColor() { + return color; + } + + public String getPretext() { + return pretext; + } -} + public String getAuthorName() { + return authorName; + } + + public String getAuthorLink() { + return authorLink; + } + + public String getAuthorIcon() { + return authorIcon; + } + + public String getTitle() { + return title; + } + + public String getTitleLink() { + return titleLink; + } + + public String getText() { + return text; + } + + public List getFields() { + return fields; + } + + public String getImageUrl() { + return imageUrl; + } + + public String getThumbUrl() { + return thumbUrl; + } + + public String getFooter() { + return footer; + } + + public String getFooterIcon() { + return footerIcon; + } + + public long getTs() { + return ts; + } +} \ No newline at end of file diff --git a/src/nyc/c4q/ramonaharrison/model/Field.java b/src/nyc/c4q/ramonaharrison/model/Field.java index cea7c8d..0c1e6f4 100644 --- a/src/nyc/c4q/ramonaharrison/model/Field.java +++ b/src/nyc/c4q/ramonaharrison/model/Field.java @@ -1,7 +1,43 @@ package nyc.c4q.ramonaharrison.model; +import org.json.simple.JSONObject; + /** - * Created by Mac on 9/11/16. + * Created by akashaarcher on 9/11/16. */ public class Field { + + private String title; + private String value; + private boolean isShort; + + + public Field(JSONObject json) { + + if (json.containsKey("title")) { + this.title = (String) json.get("title"); + } + + if (json.containsKey("value")) { + this.value = (String) json.get("value"); + } + + if (json.containsKey("short")) { + this.isShort = (boolean) json.get("short"); + } + + } + + + public String getTitle() { + return title; + } + + public String getValue() { + return value; + } + + public boolean isShort() { + return isShort; + } }