Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Completed TODOs in Attachment class #3

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
4 changes: 2 additions & 2 deletions src/nyc/c4q/ramonaharrison/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -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");

}
}
165 changes: 148 additions & 17 deletions src/nyc/c4q/ramonaharrison/model/Attachment.java
Original file line number Diff line number Diff line change
@@ -1,39 +1,170 @@
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
*
* <p>
* A class representing a message attachment.
* See https://api.slack.com/docs/message-attachments
*
*/

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<Field> 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<Field>();
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<Field> 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;
}
}
43 changes: 43 additions & 0 deletions src/nyc/c4q/ramonaharrison/model/Field.java
Original file line number Diff line number Diff line change
@@ -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;
}
}