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

Commit 8ccd60b

Browse files
committed
Add top range and downvote reason
Closes #8 Closes #5
1 parent 7acda1a commit 8ccd60b

6 files changed

Lines changed: 82 additions & 19 deletions

File tree

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

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import java.util.Arrays;
2020
import java.util.List;
2121
import java.util.function.Function;
22+
import java.util.stream.Collectors;
2223

2324
public class DevRant {
2425
static final String APP_ID = "3";
@@ -167,7 +168,8 @@ private <T> List<T> getFeed(String url, Function<JsonObject, T> converter, Sort
167168
return getFeed(url, converter,
168169
new BasicNameValuePair("sort", sort.toString()),
169170
new BasicNameValuePair("limit", String.valueOf(limit)),
170-
new BasicNameValuePair("skip", String.valueOf(skip))
171+
new BasicNameValuePair("skip", String.valueOf(skip)),
172+
sort.getParameter()
171173
);
172174
}
173175

@@ -255,7 +257,7 @@ public boolean vote(Rant rant, Vote vote) {
255257
public boolean voteRant(int id, Vote vote) {
256258
// Rants url, id, vote url.
257259
String url = String.format("%1$s/%2$d%3$s", API_RANTS, id, API_VOTE);
258-
return Util.jsonSuccess(post(url, new BasicNameValuePair("vote", String.valueOf(vote.getValue()))));
260+
return Util.jsonSuccess(post(url, new BasicNameValuePair("vote", vote.toString()), vote.getParameter()));
259261
}
260262

261263
/**
@@ -279,7 +281,7 @@ public boolean vote(Comment comment, Vote vote) {
279281
public boolean voteComment(int id, Vote vote) {
280282
// API url, comments url, id, vote url.
281283
String url = String.format("%1$s%2$s/%3$d%4$s", API, API_COMMENT, id, API_VOTE);
282-
return Util.jsonSuccess(post(url, new BasicNameValuePair("vote", String.valueOf(vote.getValue()))));
284+
return Util.jsonSuccess(post(url, new BasicNameValuePair("vote", String.valueOf(vote.toString())), vote.getParameter()));
283285
}
284286

285287
/**
@@ -356,13 +358,14 @@ JsonObject get(String url, NameValuePair... params) {
356358

357359
/**
358360
* Get a list with all the parameters, including default and auth parameters.
361+
* This also filters out any parameters that are {@code null}.
359362
*
360363
* @param params The parameters to use.
361364
* @return A list containing the given parameters, the default parameters, and the auth parameters.
362365
*/
363366
private List<NameValuePair> getParameters(NameValuePair... params) {
364-
List<NameValuePair> paramList = new ArrayList<>(params.length + 5);
365-
paramList.addAll(Arrays.asList(params));
367+
List<NameValuePair> paramList = new ArrayList<>(params.length + 6);
368+
paramList.addAll(Arrays.stream(params).filter(p -> p != null).collect(Collectors.toList()));
366369

367370
// Add the parameters which always need to be present.
368371
paramList.add(new BasicNameValuePair("app", APP_ID));
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.scorpiac.javarant;
2+
3+
import org.apache.http.NameValuePair;
4+
5+
class Option {
6+
private final String name;
7+
private final NameValuePair parameter;
8+
9+
protected Option(String name, NameValuePair parameter) {
10+
this.name = name;
11+
this.parameter = parameter;
12+
}
13+
14+
@Override
15+
public String toString() {
16+
return name;
17+
}
18+
19+
/**
20+
* Get the parameter for this {@link Option}, or {@code null} if it has no option.
21+
*/
22+
public NameValuePair getParameter() {
23+
return parameter;
24+
}
25+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.scorpiac.javarant;
2+
3+
public enum Range {
4+
DAY, WEEK, MONTH, ALL;
5+
6+
@Override
7+
public String toString() {
8+
return name().toLowerCase();
9+
}
10+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.scorpiac.javarant;
2+
3+
public enum Reason {
4+
NOT_FOR_ME("0"),
5+
REPOST("1"),
6+
OFFENSIVE_SPAM("2");
7+
8+
private final String value;
9+
10+
Reason(String value) {
11+
this.value = value;
12+
}
13+
14+
@Override
15+
public String toString() {
16+
return value;
17+
}
18+
}
Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,17 @@
11
package com.scorpiac.javarant;
22

3-
public enum Sort {
4-
ALGO, RECENT, TOP;
3+
import org.apache.http.NameValuePair;
4+
import org.apache.http.message.BasicNameValuePair;
55

6-
@Override
7-
public String toString() {
8-
return this.name().toLowerCase();
6+
public final class Sort extends Option {
7+
public static final Sort ALGO = new Sort("algo", null);
8+
public static final Sort RECENT = new Sort("recent", null);
9+
10+
private Sort(String name, NameValuePair option) {
11+
super(name, option);
12+
}
13+
14+
public static Sort TOP(Range range) {
15+
return new Sort("top", new BasicNameValuePair("range", range.toString()));
916
}
1017
}
Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
package com.scorpiac.javarant;
22

3-
public enum Vote {
4-
UP(1),
5-
NONE(0),
6-
DOWN(-1);
3+
import org.apache.http.NameValuePair;
4+
import org.apache.http.message.BasicNameValuePair;
75

8-
private final int value;
6+
public final class Vote extends Option {
7+
public static final Vote UP = new Vote("1", null);
8+
public static final Vote NONE = new Vote("0", null);
99

10-
private Vote(int value) {
11-
this.value = value;
10+
private Vote(String value, NameValuePair parameter) {
11+
super(value, parameter);
1212
}
1313

14-
public int getValue() {
15-
return value;
14+
public static Vote DOWN(Reason reason) {
15+
return new Vote("-1", new BasicNameValuePair("reason", reason.toString()));
1616
}
1717
}

0 commit comments

Comments
 (0)