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

Commit a306ade

Browse files
committed
Add avatar link to user
1 parent 2a0356c commit a306ade

2 files changed

Lines changed: 21 additions & 0 deletions

File tree

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
public class DevRant {
1414
static final String APP_ID = "3";
1515
static final String BASE_URL = "https://www.devrant.io";
16+
static final String AVATARS_URL = "https://avatars.devrant.io";
1617

1718
// API endpoints.
1819
static final String USER_URL = "/users";
@@ -121,4 +122,13 @@ static JsonObject request(String url) {
121122
static String link(String url) {
122123
return BASE_URL + url;
123124
}
125+
126+
/**
127+
* Create a link to an avatar.
128+
* @param avatarUrl The avatar url.
129+
* @return The complete url.
130+
*/
131+
static String avatarLink(String avatarUrl) {
132+
return AVATARS_URL + "/" + avatarUrl;
133+
}
124134
}

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ public class User {
2323
private int upvotedCount;
2424
private int commentsCount;
2525
private int favoritesCount;
26+
private String avatar;
2627

2728
/**
2829
* Create a new user.
@@ -94,6 +95,9 @@ static User fromJson(JsonObject json) {
9495
* @return Whether the data was fetched successfully.
9596
*/
9697
public boolean fetchData() {
98+
if (isFetched())
99+
return true;
100+
97101
// Users url, user id, app id.
98102
String url = String.format("%1$s/%2$d?app=%3$s", DevRant.API_USERS_URL, id, DevRant.APP_ID);
99103
JsonObject json = DevRant.request(url);
@@ -108,10 +112,12 @@ public boolean fetchData() {
108112
JsonObject contentJson = profileJson.get("content").getAsJsonObject();
109113
JsonObject subContentJson = contentJson.get("content").getAsJsonObject();
110114
JsonObject countsJson = contentJson.get("counts").getAsJsonObject();
115+
JsonObject avatarJson = profileJson.get("avatar").getAsJsonObject();
111116

112117
// Set all the fields.
113118
username = profileJson.get("username").getAsString();
114119
score = profileJson.get("score").getAsInt();
120+
avatar = avatarJson.get("i").getAsString();
115121

116122
about = profileJson.get("about").getAsString();
117123
location = profileJson.get("location").getAsString();
@@ -142,6 +148,11 @@ public String userLink() {
142148
return DevRant.link(DevRant.USER_URL + "/" + username);
143149
}
144150

151+
public String avatarLink() {
152+
fetchData();
153+
return DevRant.avatarLink(avatar);
154+
}
155+
145156
/**
146157
* Get the user id.
147158
*/

0 commit comments

Comments
 (0)