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

Commit 7acda1a

Browse files
committed
Add getStories method, improve DevRant code
Closes #4
1 parent 0b7cf13 commit 7acda1a

1 file changed

Lines changed: 33 additions & 51 deletions

File tree

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

Lines changed: 33 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import java.util.ArrayList;
1919
import java.util.Arrays;
2020
import java.util.List;
21+
import java.util.function.Function;
2122

2223
public class DevRant {
2324
static final String APP_ID = "3";
@@ -98,36 +99,20 @@ public boolean isLoggedIn() {
9899
* @param sort The sorting method.
99100
* @param limit How many rants to get.
100101
* @param skip How many rants to skip.
101-
* @return An array of rants.
102+
* @return An list of rants.
102103
*/
103104
public List<Rant> getRants(Sort sort, int limit, int skip) {
104-
JsonObject json = get(API_RANTS,
105-
new BasicNameValuePair("sort", sort.toString()),
106-
new BasicNameValuePair("limit", String.valueOf(limit)),
107-
new BasicNameValuePair("skip", String.valueOf(skip))
108-
);
109-
110-
// Check for success.
111-
if (!Util.jsonSuccess(json))
112-
return null;
113-
114-
return Util.jsonToList(json.get("rants").getAsJsonArray(), elem -> Rant.fromJson(this, elem.getAsJsonObject()));
105+
return getFeed(API_RANTS, elem -> Rant.fromJson(this, elem), sort, limit, skip);
115106
}
116107

117108
/**
118109
* Search for rants matching a certain term.
119110
*
120111
* @param term The term to search for.
121-
* @return An array of rants matching the search term.
112+
* @return A list of rants matching the search term.
122113
*/
123114
public List<Rant> search(String term) {
124-
JsonObject json = get(API_SEARCH, new BasicNameValuePair("term", term));
125-
126-
// Check for success.
127-
if (!Util.jsonSuccess(json))
128-
return null;
129-
130-
return Util.jsonToList(json.get("results").getAsJsonArray(), elem -> Rant.fromJson(this, elem.getAsJsonObject()));
115+
return getFeed(API_SEARCH, elem -> Rant.fromJson(this, elem), new BasicNameValuePair("term", term));
131116
}
132117

133118
/**
@@ -137,61 +122,58 @@ public List<Rant> search(String term) {
137122
*/
138123
public Rant getSurprise() {
139124
JsonObject json = get(API_SURPRISE);
140-
141-
// Check for success.
142-
if (!Util.jsonSuccess(json))
143-
return null;
144-
145-
return Rant.fromJson(this, json.get("rant").getAsJsonObject());
125+
return Util.jsonSuccess(json) ? Rant.fromJson(this, json.get("rant").getAsJsonObject()) : null;
146126
}
147127

148128
/**
149129
* Get the weekly rants.
150130
*
131+
* @param sort The sorting method.
132+
* @param limit How many rants to get.
133+
* @param skip How many rants to skip.
151134
* @return The weekly rants.
152135
*/
153-
public List<Rant> getWeekly() {
154-
JsonObject json = get(API_WEEKLY);
155-
156-
// Check for success.
157-
if (!Util.jsonSuccess(json))
158-
return null;
159-
160-
return Util.jsonToList(json.get("rants").getAsJsonArray(), elem -> Rant.fromJson(this, elem.getAsJsonObject()));
136+
public List<Rant> getWeekly(Sort sort, int limit, int skip) {
137+
return getFeed(API_WEEKLY, elem -> Rant.fromJson(this, elem), sort, limit, skip);
161138
}
162139

163140
/**
164-
* Get the collab rants.
141+
* Get the collabs.
165142
*
166-
* @return The collab rants.
143+
* @param limit How many rants to get.
144+
* @param skip How many rants to skip.
145+
* @return A list of collabs.
167146
*/
168-
public List<Collab> getCollabs() {
169-
JsonObject json = get(API_COLLABS);
170-
171-
// Check for success.
172-
if (!Util.jsonSuccess(json))
173-
return null;
174-
175-
return Util.jsonToList(json.get("rants").getAsJsonArray(), elem -> Collab.fromJson(this, elem.getAsJsonObject()));
147+
public List<Collab> getCollabs(int limit, int skip) {
148+
return getFeed(API_COLLABS, elem -> Collab.fromJson(this, elem.getAsJsonObject()),
149+
new BasicNameValuePair("limit", String.valueOf(limit)),
150+
new BasicNameValuePair("skip", String.valueOf(skip))
151+
);
176152
}
177153

178154
/**
179155
* Get the story rants.
180156
*
181-
* @return The story rants.
157+
* @param sort The sorting method.
158+
* @param limit How many rants to get.
159+
* @param skip How many rants to skip.
160+
* @return A list of story rants.
182161
*/
183162
public List<Rant> getStories(Sort sort, int limit, int skip) {
184-
JsonObject json = get(API_STORIES,
163+
return getFeed(API_STORIES, elem -> Rant.fromJson(this, elem), sort, limit, skip);
164+
}
165+
166+
private <T> List<T> getFeed(String url, Function<JsonObject, T> converter, Sort sort, int limit, int skip) {
167+
return getFeed(url, converter,
185168
new BasicNameValuePair("sort", sort.toString()),
186169
new BasicNameValuePair("limit", String.valueOf(limit)),
187170
new BasicNameValuePair("skip", String.valueOf(skip))
188171
);
172+
}
189173

190-
// Check for success.
191-
if (!Util.jsonSuccess(json))
192-
return null;
193-
194-
return Util.jsonToList(json.get("rants").getAsJsonArray(), elem -> Rant.fromJson(this, elem.getAsJsonObject()));
174+
private <T> List<T> getFeed(String url, Function<JsonObject, T> converter, NameValuePair... params) {
175+
JsonObject json = get(url, params);
176+
return Util.jsonSuccess(json) ? Util.jsonToList(json.get("rants").getAsJsonArray(), obj -> converter.apply(obj.getAsJsonObject())) : null;
195177
}
196178

197179
/**

0 commit comments

Comments
 (0)