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

Commit 53f6a15

Browse files
committed
Update readme, make error from result not optional
1 parent eefbdc3 commit 53f6a15

8 files changed

Lines changed: 93 additions & 34 deletions

File tree

README.md

Lines changed: 52 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ A devRant API wrapper for Java.
88
## Using JavaRant
99
JavaRant is available on [Maven](http://mvnrepository.com/artifact/com.scorpiac.javarant/javarant), simply add this dependency to your `pom.xml` file:
1010

11-
```
11+
```xml
1212
<dependency>
1313
<groupId>com.scorpiac.javarant</groupId>
1414
<artifactId>javarant</artifactId>
@@ -24,15 +24,61 @@ To access devRant simply create a new `DevRant` object:
2424
DevRant devRant = new DevRant();
2525
```
2626

27-
You can then use it to get specific rants and users, or access the feed:
27+
Most object that are returned from `DevRant` are wrapped in a `Result` object.
28+
This will contain an optional result value, along with an error message.
29+
If the optional result is empty, then an error occurred and the error message will be set.
30+
For example:
31+
32+
```
33+
Result<CommentedRant> result = devRant.getRant(832125);
34+
35+
if (!result.getValue().isPresent()) {
36+
System.out.println("An error occurred: " + result.getError());
37+
} else {
38+
CommentedRant rant = result.getValue().get();
39+
System.out.println(rant.getUser().getUsername() + '\n' + rant.getText());
40+
}
41+
```
42+
43+
The `DevRant` class itself can be used to get specific rants and users.
2844

2945
```
3046
// Get a specific rant.
31-
Optional<CommentedRant> rant = devRant.getRant(686001);
47+
Result<CommentedRant> rant = devRant.getRant(686001);
3248
3349
// Get a user by username.
34-
Optional<User> me = devRant.getUser("LucaScorpion");
50+
Result<User> me = devRant.getUser("LucaScorpion");
51+
```
52+
53+
The `DevRant` class contains 2 methods for getting to specific parts of the api.
54+
First, `getFeed()` which returns a `DevRantFeed` object.
55+
This is used to access the rant and collab feeds.
56+
57+
```
58+
// Get the 10 latest rants.
59+
Result<List<Rant>> recent = devRant.getFeed().getRants(Sort.RECENT, 10, 0);
60+
61+
// Get the 10 best stories.
62+
Result<List<Rant>> stories = devRant.getFeed().getStories(Sort.TOP, 0);
63+
64+
// Get 10 collabs.
65+
Result<List<Collab>> collabs = devRant.getFeed().getCollabs(10);
66+
```
67+
68+
Second, `getAuth()` which returns a `DevRantAuth` object, which is used to access user functionality.
69+
Note that a user needs to be logged in before this can be accessed.
70+
71+
```
72+
// Log in to devRant.
73+
char[] password = "<password>".toCharArray();
74+
devRant.login("<username>", password);
75+
76+
// Upvote a rant.
77+
devRant.getAuth().voteRant(832125, Vote.UP);
78+
79+
// Clear the vote on a comment.
80+
devRant.getAuth().voteComment(832169, Vote.NONE);
3581
36-
// Get the 10 newest rants.
37-
Optional<List<Rant>> recent = devRant.getFeed().getRants(Sort.RECENT);
82+
// Log out to clear the token.
83+
devRant.logout();
3884
```

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
<groupId>com.scorpiac.javarant</groupId>
88
<artifactId>javarant</artifactId>
9-
<version>2.0.0-SNAPSHOT</version>
9+
<version>2.0.0</version>
1010
<packaging>jar</packaging>
1111

1212
<name>JavaRant</name>

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,15 +81,15 @@ public Result<CommentedRant> getRant(int id) {
8181
* @return The user.
8282
*/
8383
public Result<User> getUser(String username) {
84-
Result<Integer> id = requestHandler.get(ApiEndpoint.USER_ID, UserIdResponse.class, new BasicNameValuePair("username", username));
84+
Result<Integer> result = requestHandler.get(ApiEndpoint.USER_ID, UserIdResponse.class, new BasicNameValuePair("username", username));
8585

8686
// Check the result.
87-
if (id.getError().isPresent() || !id.getValue().isPresent()) {
87+
if (!result.getValue().isPresent()) {
8888
// When the username is invalid, no error message is returned by the API.
8989
return new Result<>("Invalid username specified.");
9090
}
9191

92-
return getUser(id.getValue().get());
92+
return getUser(result.getValue().get());
9393
}
9494

9595
/**
@@ -102,7 +102,7 @@ public Result<User> getUser(int id) {
102102
Result<User> result = requestHandler.get(ApiEndpoint.USERS.toString() + '/' + id, UserResponse.class);
103103

104104
// Check the result.
105-
if (result.getError().isPresent() || !result.getValue().isPresent()) {
105+
if (!result.getValue().isPresent()) {
106106
// When the user id is invalid, no error message is returned by the API.
107107
return new Result<>("Invalid user id specified.");
108108
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ public Result<List<Rant>> getStories(Sort sort, int skip) {
7575
* @param limit How many rants to get.
7676
* @return Collabs from the feed.
7777
*/
78-
public Result<List<Rant>> getCollabs(int limit) {
78+
public Result<List<Collab>> getCollabs(int limit) {
7979
return devRant.getRequestHandler().get(ApiEndpoint.COLLABS, RantsFeedResponse.class,
8080
new BasicNameValuePair("limit", String.valueOf(limit))
8181
);

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

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,23 @@ public Result(Response<T> response) {
1818
value = response.getValue();
1919
}
2020

21+
/**
22+
* Get the result value.
23+
* If an error occurred, this will be empty and the error will be set.
24+
*
25+
* @return The value.
26+
*/
2127
public Optional<T> getValue() {
2228
return Optional.ofNullable(value);
2329
}
2430

25-
public Optional<String> getError() {
26-
return Optional.ofNullable(error);
31+
/**
32+
* Get the error message.
33+
* If there was no error, this returns {@code null}.
34+
*
35+
* @return The error, or {@code null} if there was none.
36+
*/
37+
public String getError() {
38+
return error;
2739
}
2840
}

src/test/java/com/scorpiac/javarant/DevRantAuthIT.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import static com.github.tomakehurst.wiremock.client.WireMock.*;
99
import static org.testng.Assert.assertEquals;
1010
import static org.testng.Assert.assertFalse;
11+
import static org.testng.Assert.assertNull;
1112

1213
public class DevRantAuthIT extends ITHelper {
1314
private String authBody;
@@ -27,7 +28,7 @@ public void testUpvoteRant() throws IOException {
2728
));
2829

2930
Result<Rant> result = devRant.getAuth().voteRant(843654, Vote.UP);
30-
assertFalse(result.getError().isPresent());
31+
assertNull(result.getError());
3132
assertEquals(result.getValue().get().getVoteState(), VoteState.UP);
3233
}
3334

@@ -40,7 +41,7 @@ public void testDownvoteRant() throws IOException {
4041
));
4142

4243
Result<Rant> result = devRant.getAuth().voteRant(843654, Vote.DOWN(Reason.NOT_FOR_ME));
43-
assertFalse(result.getError().isPresent());
44+
assertNull(result.getError());
4445
assertEquals(result.getValue().get().getVoteState(), VoteState.DOWN);
4546
}
4647

@@ -53,7 +54,7 @@ public void testNonevoteComment() throws IOException {
5354
));
5455

5556
Result<Comment> result = devRant.getAuth().voteComment(843736, Vote.NONE);
56-
assertFalse(result.getError().isPresent());
57+
assertNull(result.getError());
5758
assertEquals(result.getValue().get().getVoteState(), VoteState.NONE);
5859
}
5960
}

src/test/java/com/scorpiac/javarant/DevRantFeedIT.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
import static com.github.tomakehurst.wiremock.client.WireMock.*;
99
import static org.testng.Assert.assertEquals;
10-
import static org.testng.Assert.assertFalse;
10+
import static org.testng.Assert.assertNull;
1111

1212
public class DevRantFeedIT extends ITHelper {
1313
@Test
@@ -21,7 +21,7 @@ public void testGetRants() throws IOException {
2121
));
2222

2323
Result<List<Rant>> result = devRant.getFeed().getRants(Sort.RECENT, 4, 1);
24-
assertFalse(result.getError().isPresent());
24+
assertNull(result.getError());
2525
List<Rant> rants = result.getValue().get();
2626
assertEquals(rants.size(), 4);
2727

@@ -43,7 +43,7 @@ public void testSearch() throws IOException {
4343
));
4444

4545
Result<List<Rant>> result = devRant.getFeed().search("wtf");
46-
assertFalse(result.getError().isPresent());
46+
assertNull(result.getError());
4747

4848
validateRant(result.getValue().get().get(1),
4949
542296,
@@ -64,7 +64,7 @@ public void testGetWeekly() throws IOException {
6464
));
6565

6666
Result<List<Rant>> result = devRant.getFeed().getWeekly(Sort.ALGO, 2);
67-
assertFalse(result.getError().isPresent());
67+
assertNull(result.getError());
6868
List<Rant> rants = result.getValue().get();
6969

7070
validateRant(rants.get(0),
@@ -86,7 +86,7 @@ public void testGetStories() throws IOException {
8686
));
8787

8888
Result<List<Rant>> result = devRant.getFeed().getStories(Sort.TOP, 4);
89-
assertFalse(result.getError().isPresent());
89+
assertNull(result.getError());
9090
List<Rant> rants = result.getValue().get();
9191

9292
validateRant(rants.get(0),
@@ -106,9 +106,9 @@ public void testGetCollabs() throws IOException {
106106
"/feed-collabs.json"
107107
));
108108

109-
Result<List<Rant>> result = devRant.getFeed().getCollabs(2);
110-
assertFalse(result.getError().isPresent());
111-
List<Rant> rants = result.getValue().get();
109+
Result<List<Collab>> result = devRant.getFeed().getCollabs(2);
110+
assertNull(result.getError());
111+
List<Collab> rants = result.getValue().get();
112112

113113
validateRant(rants.get(0),
114114
838945,

src/test/java/com/scorpiac/javarant/DevRantIT.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public void testGetRant() throws IOException {
1616
));
1717

1818
Result<CommentedRant> result = devRant.getRant(686001);
19-
assertFalse(result.getError().isPresent());
19+
assertNull(result.getError());
2020
CommentedRant rant = result.getValue().get();
2121

2222
validateRant(rant,
@@ -43,7 +43,7 @@ public void testGetRantInvalid() throws IOException {
4343

4444
Result<CommentedRant> result = devRant.getRant(0);
4545
assertFalse(result.getValue().isPresent());
46-
assertTrue(result.getError().isPresent());
46+
assertNotNull(result.getError());
4747
}
4848

4949
@Test
@@ -55,7 +55,7 @@ public void testGetRantServerError() throws IOException {
5555

5656
Result<CommentedRant> result = devRant.getRant(123456);
5757
assertFalse(result.getValue().isPresent());
58-
assertTrue(result.getError().isPresent());
58+
assertNotNull(result.getError());
5959
}
6060

6161
@Test
@@ -71,7 +71,7 @@ public void testGetUserByUsername() throws IOException {
7171
));
7272

7373
Result<User> result = devRant.getUser("LucaScorpion");
74-
assertFalse(result.getError().isPresent());
74+
assertNull(result.getError());
7575

7676
validateUser(result.getValue().get(),
7777
102959,
@@ -98,9 +98,9 @@ public void testGetUserByUsernameInvalid() throws IOException {
9898
"/user-id-invalid.json"
9999
));
100100

101-
Result<User> user = devRant.getUser("invalid");
102-
assertFalse(user.getValue().isPresent());
103-
assertTrue(user.getError().isPresent());
101+
Result<User> result = devRant.getUser("invalid");
102+
assertFalse(result.getValue().isPresent());
103+
assertNotNull(result.getError());
104104
}
105105

106106
@Test
@@ -112,7 +112,7 @@ public void testGetUserInvalid() throws IOException {
112112

113113
Result<User> result = devRant.getUser(123);
114114
assertFalse(result.getValue().isPresent());
115-
assertTrue(result.getError().isPresent());
115+
assertNotNull(result.getError());
116116
}
117117

118118
@Test
@@ -153,7 +153,7 @@ public void testGetCollab() throws IOException {
153153
));
154154

155155
Result<Collab> result = devRant.getCollab(785714);
156-
assertFalse(result.getError().isPresent());
156+
assertNull(result.getError());
157157
Collab collab = result.getValue().get();
158158

159159
validateCollab(collab,

0 commit comments

Comments
 (0)