Skip to content
This repository was archived by the owner on Jan 5, 2019. It is now read-only.

Commit 4b25ff9

Browse files
committed
Move image to RantContent so comments have imgs too
1 parent e3334d8 commit 4b25ff9

5 files changed

Lines changed: 72 additions & 22 deletions

File tree

src/main/java/com/scorpiac/javarant/Comment.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
import com.google.gson.JsonObject;
44

55
public class Comment extends RantContent {
6-
public Comment(int id, User user, int upvotes, int downvotes, String content) {
7-
super(id, user, upvotes, downvotes, content);
6+
private Comment(int id, User user, int upvotes, int downvotes, String content, Image image) {
7+
super(id, user, upvotes, downvotes, content, image);
88
}
99

1010
static Comment fromJson(JsonObject json) {
@@ -13,7 +13,8 @@ static Comment fromJson(JsonObject json) {
1313
User.fromJson(json),
1414
json.get("num_upvotes").getAsInt(),
1515
json.get("num_downvotes").getAsInt(),
16-
json.get("body").getAsString()
16+
json.get("body").getAsString(),
17+
Image.fromJson(json.get("attached_image"))
1718
);
1819
}
1920
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package com.scorpiac.javarant;
2+
3+
import com.google.gson.JsonElement;
4+
import com.google.gson.JsonObject;
5+
6+
public class Image {
7+
private String url;
8+
private int width;
9+
private int height;
10+
11+
private Image(String url, int width, int height) {
12+
this.url = url;
13+
this.width = width;
14+
this.height = height;
15+
}
16+
17+
/**
18+
* Get the image from the JSON, or {@code null} if there is no image.
19+
*
20+
* @param json The JSON element for the image.
21+
* @return The image, or {@code null}.
22+
*/
23+
static Image fromJson(JsonElement json) {
24+
if (json == null || !json.isJsonObject())
25+
return null;
26+
27+
JsonObject obj = json.getAsJsonObject();
28+
return new Image(obj.get("url").getAsString(), obj.get("width").getAsInt(), obj.get("height").getAsInt());
29+
}
30+
31+
/**
32+
* Get the image url.
33+
*
34+
* @return
35+
*/
36+
public String getUrl() {
37+
return url;
38+
}
39+
40+
/**
41+
* Get the image width.
42+
*/
43+
public int getWidth() {
44+
return width;
45+
}
46+
47+
/**
48+
* Get the image height.
49+
*/
50+
public int getHeight() {
51+
return height;
52+
}
53+
}

src/main/java/com/scorpiac/javarant/Rant.java

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,12 @@
66
import com.scorpiac.javarant.exceptions.NoSuchRantException;
77

88
public class Rant extends RantContent {
9-
private String image;
109
private String[] tags;
1110
private int commentCount;
1211
private Comment[] comments;
1312

14-
private Rant(int id, User user, int upvotes, int downvotes, String text, String image, String[] tags, int commentCount) {
15-
super(id, user, upvotes, downvotes, text);
16-
this.image = image;
13+
private Rant(int id, User user, int upvotes, int downvotes, String text, Image image, String[] tags, int commentCount) {
14+
super(id, user, upvotes, downvotes, text, image);
1715
this.tags = tags;
1816
this.commentCount = commentCount;
1917
}
@@ -47,7 +45,7 @@ static Rant fromJson(JsonObject json) {
4745
json.get("num_upvotes").getAsInt(),
4846
json.get("num_downvotes").getAsInt(),
4947
json.get("text").getAsString(),
50-
getImage(json.get("attached_image")),
48+
Image.fromJson(json.get("attached_image")),
5149
Util.jsonToList(json.getAsJsonArray("tags"), JsonElement::getAsString).toArray(new String[0]),
5250
json.get("num_comments").getAsInt()
5351
);
@@ -62,10 +60,6 @@ private void commentsFromJson(JsonArray commentArray) {
6260
comments = Util.jsonToList(commentArray, elem -> Comment.fromJson(elem.getAsJsonObject())).toArray(new Comment[0]);
6361
}
6462

65-
private static String getImage(JsonElement image) {
66-
return image.isJsonObject() ? image.getAsJsonObject().get("url").getAsString() : "";
67-
}
68-
6963
/**
7064
* Get the comments on this rant. If they are not yet retrieved, this will also fetch them.
7165
*
@@ -117,13 +111,6 @@ public String rantLink() {
117111
return DevRant.link(DevRant.RANT_URL + "/" + this.getId());
118112
}
119113

120-
/**
121-
* Get the link to the image, or null if there is none.
122-
*/
123-
public String imageLink() {
124-
return image;
125-
}
126-
127114
/**
128115
* Get the tags from this rant.
129116
*/

src/main/java/com/scorpiac/javarant/RantContent.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,15 @@ public abstract class RantContent {
66
private int upvotes;
77
private int downvotes;
88
private String content;
9+
private Image image;
910

10-
protected RantContent(int id, User user, int upvotes, int downvotes, String content) {
11+
protected RantContent(int id, User user, int upvotes, int downvotes, String content, Image image) {
1112
this.id = id;
1213
this.user = user;
1314
this.upvotes = upvotes;
1415
this.downvotes = downvotes;
1516
this.content = content;
17+
this.image = image;
1618
}
1719

1820
/**
@@ -57,6 +59,13 @@ public String getContent() {
5759
return content;
5860
}
5961

62+
/**
63+
* Get the image, or {@code null} if there is none.
64+
*/
65+
public Image getImage() {
66+
return image;
67+
}
68+
6069
@Override
6170
public String toString() {
6271
return content;

src/main/java/com/scorpiac/javarant/Util.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,10 @@ static <T> List<T> jsonToList(JsonArray json, Function<JsonElement, T> converter
2727
}
2828

2929
/**
30-
* Check whether the JSON is not null and the success member is true.
30+
* Check whether the JSON is not {@code null} and the success member is true.
3131
*
3232
* @param json The JSON to check.
33-
* @return True if the JSON is not null and has a success member which is true.
33+
* @return True if the JSON is not {@code null} and has a success member which is true.
3434
*/
3535
static boolean jsonSuccess(JsonObject json) {
3636
return json != null && json.get("success").getAsBoolean();

0 commit comments

Comments
 (0)