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

Commit 084ae0d

Browse files
committed
Add support for collabs
1 parent f72799d commit 084ae0d

4 files changed

Lines changed: 172 additions & 8 deletions

File tree

Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
package com.scorpiac.javarant;
2+
3+
import com.google.gson.JsonArray;
4+
import com.google.gson.JsonObject;
5+
import com.scorpiac.javarant.exceptions.NoSuchRantException;
6+
7+
public class Collab extends Rant {
8+
private String projectType;
9+
10+
// Data that needs to be fetched.
11+
private boolean fetched = false;
12+
private String description;
13+
private String techStack;
14+
private String teamSize;
15+
private String url;
16+
17+
protected Collab(int id, User user, int upvotes, int downvotes, String projectType, String summary, int commentCount) {
18+
super(id, user, upvotes, downvotes, summary, null, new String[0], commentCount);
19+
this.projectType = projectType;
20+
}
21+
22+
/**
23+
* Get a collab by its id.
24+
*
25+
* @param id The id of the collab to get.
26+
* @return The collab.
27+
*/
28+
public static Collab byId(int id) {
29+
// Collabs url, collab id, app id.
30+
String url = String.format("%1$s/%2$d?app=%3$s", DevRant.API_RANTS_URL, id, DevRant.APP_ID);
31+
JsonObject json = DevRant.request(url);
32+
33+
// Check if the collab exists.
34+
if (!Util.jsonSuccess(json))
35+
throw new NoSuchRantException(id);
36+
37+
// Get the collab and data.
38+
JsonObject obj = json.get("rant").getAsJsonObject();
39+
Collab collab = fromJson(obj);
40+
collab.setData(obj, json.get("comments").getAsJsonArray());
41+
return collab;
42+
}
43+
44+
static Collab fromJson(JsonObject json) {
45+
return new Collab(
46+
json.get("id").getAsInt(),
47+
User.fromJson(json),
48+
json.get("num_upvotes").getAsInt(),
49+
json.get("num_downvotes").getAsInt(),
50+
json.get("c_type_long").getAsString(),
51+
json.get("text").getAsString(),
52+
json.get("num_comments").getAsInt()
53+
);
54+
}
55+
56+
private void setData(JsonObject collab, JsonArray comments) {
57+
commentsFromJson(comments);
58+
description = collab.get("c_description").getAsString();
59+
techStack = collab.get("c_tech_stack").getAsString();
60+
teamSize = collab.get("c_team_size").getAsString();
61+
url = collab.get("c_url").getAsString();
62+
}
63+
64+
/**
65+
* Fetch the data for this collab. If the data is already fetched, it will not be fetched again.
66+
*
67+
* @return Whether the data was fetched successfully.
68+
*/
69+
public boolean fetchData() {
70+
return fetchData(false);
71+
}
72+
73+
/**
74+
* Fetch the data for this collab.
75+
*
76+
* @param force Whether to fetch the data even if it was already fetched.
77+
* @return Whether the data was fetched successfully.
78+
*/
79+
public boolean fetchData(boolean force) {
80+
// Check if we already fetched and force is false.
81+
if (fetched && !force)
82+
return true;
83+
84+
// Collabs url, collab id, app id.
85+
String url = String.format("%1$s/%2$d?app=%3$s", DevRant.API_RANTS_URL, getId(), DevRant.APP_ID);
86+
JsonObject json = DevRant.request(url);
87+
88+
// Check for success.
89+
if (!Util.jsonSuccess(json))
90+
return false;
91+
fetched = true;
92+
93+
setData(json.get("rant").getAsJsonObject(), json.get("comments").getAsJsonArray());
94+
95+
return true;
96+
}
97+
98+
/**
99+
* Get the link to the collab.
100+
*/
101+
@Override
102+
public String link() {
103+
return DevRant.link(DevRant.COLLAB_URL + "/" + getId());
104+
}
105+
106+
/**
107+
* Get the project type.
108+
*/
109+
public String getProjectType() {
110+
return projectType;
111+
}
112+
113+
/**
114+
* Get the project description.
115+
*/
116+
public String getDescription() {
117+
fetchData();
118+
return description;
119+
}
120+
121+
/**
122+
* Get the project tech stack.
123+
*/
124+
public String getTechStack() {
125+
fetchData();
126+
return techStack;
127+
}
128+
129+
/**
130+
* Get the team size.
131+
*/
132+
public String getTeamSize() {
133+
fetchData();
134+
return teamSize;
135+
}
136+
137+
/**
138+
* Get the project url.
139+
*/
140+
public String getUrl() {
141+
fetchData();
142+
return url;
143+
}
144+
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import com.google.gson.JsonObject;
44

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

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

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,19 @@ public class DevRant {
1515
static final String BASE_URL = "https://www.devrant.io";
1616
static final String AVATARS_URL = "https://avatars.devrant.io";
1717

18-
// API endpoints.
1918
static final String USER_URL = "/users";
2019
static final String RANT_URL = "/rants";
20+
static final String COLLAB_URL = "/collabs";
21+
22+
// API endpoints.
2123
static final String API_URL = "/api";
2224
static final String API_RANTS_URL = API_URL + "/devrant/rants";
2325
static final String API_SEARCH_URL = API_URL + "/devrant/search";
2426
static final String API_SURPRISE_URL = API_RANTS_URL + "/surprise";
2527
static final String API_USERS_URL = API_URL + "/users";
2628
static final String API_USER_ID_URL = API_URL + "/get-user-id";
2729
static final String API_WEEKLY_URL = API_URL + "/devrant/weekly-rants";
30+
static final String API_COLLABS_URL = API_URL + "/devrant/collabs";
2831

2932
/**
3033
* Get a list of rants.
@@ -98,6 +101,23 @@ public static Rant[] weekly() {
98101
return Util.jsonToList(json.get("rants").getAsJsonArray(), elem -> Rant.fromJson(elem.getAsJsonObject())).toArray(new Rant[0]);
99102
}
100103

104+
/**
105+
* Get the collab rants.
106+
*
107+
* @return The collab rants
108+
*/
109+
public static Collab[] collabs() {
110+
// Collab url, app id.
111+
String url = String.format("%1$s?app=%2$s&", API_COLLABS_URL, APP_ID);
112+
JsonObject json = request(url);
113+
114+
// Check for success.
115+
if (!Util.jsonSuccess(json))
116+
return null;
117+
118+
return Util.jsonToList(json.get("rants").getAsJsonArray(), elem -> Collab.fromJson(elem.getAsJsonObject())).toArray(new Collab[0]);
119+
}
120+
101121
/**
102122
* Make a request to the DevRant server.
103123
*

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ public class Rant extends RantContent {
1010
private int commentCount;
1111
private Comment[] comments;
1212

13-
private Rant(int id, User user, int upvotes, int downvotes, String text, Image image, String[] tags, int commentCount) {
13+
protected Rant(int id, User user, int upvotes, int downvotes, String text, Image image, String[] tags, int commentCount) {
1414
super(id, user, upvotes, downvotes, text, image);
1515
this.tags = tags;
1616
this.commentCount = commentCount;
@@ -23,7 +23,7 @@ private Rant(int id, User user, int upvotes, int downvotes, String text, Image i
2323
* @return The rant.
2424
*/
2525
public static Rant byId(int id) {
26-
// Users url, rant id, app id.
26+
// Rants url, rant id, app id.
2727
String url = String.format("%1$s/%2$d?app=%3$s", DevRant.API_RANTS_URL, id, DevRant.APP_ID);
2828
JsonObject json = DevRant.request(url);
2929

@@ -56,7 +56,7 @@ static Rant fromJson(JsonObject json) {
5656
*
5757
* @param commentArray The JSON array to get the comments from.
5858
*/
59-
private void commentsFromJson(JsonArray commentArray) {
59+
protected void commentsFromJson(JsonArray commentArray) {
6060
comments = Util.jsonToList(commentArray, elem -> Comment.fromJson(elem.getAsJsonObject())).toArray(new Comment[0]);
6161
}
6262

@@ -91,7 +91,7 @@ public boolean fetchComments(boolean force) {
9191
return true;
9292

9393
// Rants url, rant id, app id.
94-
String url = String.format("%1$s/%2$d?app=%3$s", DevRant.API_RANTS_URL, this.getId(), DevRant.APP_ID);
94+
String url = String.format("%1$s/%2$d?app=%3$s", DevRant.API_RANTS_URL, getId(), DevRant.APP_ID);
9595
JsonObject json = DevRant.request(url);
9696

9797
// Check for success.
@@ -107,8 +107,8 @@ public boolean fetchComments(boolean force) {
107107
/**
108108
* Get the link to the rant.
109109
*/
110-
public String rantLink() {
111-
return DevRant.link(DevRant.RANT_URL + "/" + this.getId());
110+
public String link() {
111+
return DevRant.link(DevRant.RANT_URL + "/" + getId());
112112
}
113113

114114
/**

0 commit comments

Comments
 (0)