|
1 | 1 | # JavaRant |
2 | 2 | A devRant API wrapper for Java. |
3 | 3 |
|
| 4 | +[](https://mvnrepository.com/artifact/com.scorpiac.javarant/javarant) |
| 5 | +[]() |
| 6 | +[]() |
| 7 | + |
4 | 8 | ## Using JavaRant |
5 | | -JavaRant is available on Maven, simply add this dependency to your `pom.xml` file: |
| 9 | +JavaRant is available on [Maven](http://mvnrepository.com/artifact/com.scorpiac.javarant/javarant), simply add this dependency to your `pom.xml` file: |
6 | 10 |
|
7 | | -``` |
| 11 | +```xml |
8 | 12 | <dependency> |
9 | 13 | <groupId>com.scorpiac.javarant</groupId> |
10 | 14 | <artifactId>javarant</artifactId> |
11 | | - <version>1.3</version> |
| 15 | + <version>2.0.0</version> |
12 | 16 | </dependency> |
13 | 17 | ``` |
14 | 18 |
|
15 | | -## Class overview |
| 19 | +## Getting started |
16 | 20 |
|
17 | | -### DevRant |
18 | | -This is the main class to interact with devRant. |
19 | | -To use it, you first need to create an instance of it. |
| 21 | +To access devRant simply create a new `DevRant` object: |
20 | 22 |
|
21 | 23 | ``` |
22 | 24 | DevRant devRant = new DevRant(); |
23 | 25 | ``` |
24 | 26 |
|
25 | | -It can then be used to get rants and collabs from the feed or by searching. |
| 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: |
26 | 31 |
|
27 | 32 | ``` |
28 | | -List<Rant> rants = devRant.getRants(Sort.ALGO, 10, 0); |
29 | | -List<Rant> wtf = devRant.search("wtf"); |
30 | | -List<Rant> weekly = devRant.getWeekly(Sort.TOP(Range.ALL), 20, 0); |
31 | | -List<Rant> stories = devRant.getStories(Sort.RECENT, 20, 0); |
32 | | -
|
33 | | -List<Collab> collabs = devRant.getCollabs(10, 0); |
| 33 | +Result<CommentedRant> result = devRant.getRant(832125); |
34 | 34 |
|
35 | | -Rant random = devRant.getSurprise(); |
| 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 | +} |
36 | 41 | ``` |
37 | 42 |
|
38 | | -It can also be used to get specific items based on an id (or username). |
39 | | -These methods will throw an exception if the specified item does not exist (`NoSuchRantException` for rants and `NoSuchUserException` for users). |
40 | | -Additionally, `getCollab` will also throw a `NotACollabException` if the requested rant is a normal rant instead of a collab. |
| 43 | +The `DevRant` class itself can be used to get specific rants and users. |
41 | 44 |
|
42 | 45 | ``` |
43 | | -Rant rant = devRant.getRant(422850); |
44 | | -Collab collab = devRant.getCollab(420025); |
| 46 | +// Get a specific rant. |
| 47 | +Result<CommentedRant> rant = devRant.getRant(686001); |
45 | 48 |
|
46 | | -User me = devRant.getUser("LucaScorpion"); |
47 | | -User me2 = devRant.getUser(102959); |
| 49 | +// Get a user by username. |
| 50 | +Result<User> me = devRant.getUser("LucaScorpion"); |
48 | 51 | ``` |
49 | 52 |
|
50 | | -To post or vote anything we first need to log in. |
51 | | -This will make a request to get an authentication token, which is stored for later use. |
52 | | -Your username and password will _not_ be stored. |
53 | | -The login method uses `char[]` instead of `String` for the password parameter for security reasons (and most of the times you have a password it should already be in the form of a `char[]`). |
| 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. |
54 | 56 |
|
55 | 57 | ``` |
56 | | -devRant.login("username", "password".toCharArray()); |
57 | | -``` |
| 58 | +// Get the 10 latest rants. |
| 59 | +Result<List<Rant>> recent = devRant.getFeed().getRants(Sort.RECENT, 10, 0); |
58 | 60 |
|
59 | | -Voting on rants and comments can be done by passing the `Rant` or `Comment` object to vote on, or by directly passing the id. |
| 61 | +// Get the 10 best stories. |
| 62 | +Result<List<Rant>> stories = devRant.getFeed().getStories(Sort.TOP, 0); |
60 | 63 |
|
| 64 | +// Get 10 collabs. |
| 65 | +Result<List<Collab>> collabs = devRant.getFeed().getCollabs(10); |
61 | 66 | ``` |
62 | | -devRant.vote(rant, Vote.DOWN(Reason.NOT_FOR_ME)); |
63 | | -devRant.vote(comment, Vote.NONE); |
64 | 67 |
|
65 | | -devRant.voteRant(429863, Vote.UP); |
66 | | -devRant.voteComment(424558, Vote.UP); |
67 | | -``` |
68 | | - |
69 | | -To post a rant you simply need the content and tags. |
70 | | -To post a comment you need the text and a `Rant` object, or the id of the rant to comment on. |
| 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. |
71 | 70 |
|
72 | 71 | ``` |
73 | | -devRant.postRant("Hello world!", "hello, not a rant"); |
| 72 | +// Log in to devRant. |
| 73 | +char[] password = "<password>".toCharArray(); |
| 74 | +devRant.login("<username>", password); |
74 | 75 |
|
75 | | -devRant.postComment(rant, "This is a comment."); |
76 | | -devRant.postComment(424553, "And another one."); |
77 | | -``` |
| 76 | +// Upvote a rant. |
| 77 | +devRant.getAuth().voteRant(832125, Vote.UP); |
78 | 78 |
|
79 | | -You can log out to clear the authorization token. |
| 79 | +// Clear the vote on a comment. |
| 80 | +devRant.getAuth().voteComment(832169, Vote.NONE); |
80 | 81 |
|
81 | | -``` |
| 82 | +// Log out to clear the token. |
82 | 83 | devRant.logout(); |
83 | 84 | ``` |
84 | | - |
85 | | -### RantContent |
86 | | -This is the base class for rants and comments, which have the following attributes: |
87 | | - |
88 | | -- id |
89 | | -- user |
90 | | -- upvotes |
91 | | -- downvotes |
92 | | -- score |
93 | | -- voteState |
94 | | -- content |
95 | | -- image |
96 | | - |
97 | | -### Rant |
98 | | -In addition to the `RantContent` attributes, rants also contain: |
99 | | - |
100 | | -- tags |
101 | | -- commentCount |
102 | | -- comments |
103 | | - |
104 | | -The comments can be accessed by calling `getComments()`. |
105 | | -This will also fetch them if that has not been done yet. |
106 | | -Alternatively you can call `fetchComments()` to fetch the comments, or `fetchComments(force)` to force fetch them (i.e. fetch them again). |
107 | | - |
108 | | -### Collab |
109 | | -Collabs are an extension of rants. |
110 | | -Besides the attributes from a rant, collabs contain the following: |
111 | | - |
112 | | -- projectType |
113 | | -- description |
114 | | -- techStack |
115 | | -- teamSize |
116 | | -- url |
117 | | - |
118 | | -Like with a rant the comments can be fetched or force fetched. |
119 | | -There is also more data which needs to be fetched, similar to the comments this is done by calling `fetchData()` or `fetchData(force)` to force fetch it. |
120 | | - |
121 | | -### Comment |
122 | | -A comment only contains the attributes from `RantContent`. |
123 | | - |
124 | | -### Image |
125 | | -An image has the following attributes: |
126 | | - |
127 | | -- url |
128 | | -- width |
129 | | -- height |
130 | | - |
131 | | -### User |
132 | | -Users can be get from rants, comments, by id or username. |
133 | | -When they are get from a rant or comment, only the id, username and score are given. |
134 | | -The other data will be fetched and stored as soon as it's accessed. |
135 | | -Alternatively you can fetch the data by calling `fetchData()`, or `fetchData(force)` to force fetch the data (i.e. fetch it again). |
136 | | - |
137 | | -### Sort |
138 | | -These are the different sort options, which are used when getting rants from the feed. |
139 | | -You can pick between `ALGO`, `RECENT` or `TOP`. |
140 | | - |
141 | | -### Range |
142 | | -The range is used for the `TOP` sorting method. |
143 | | -The ranges are `DAY`, `WEEK`, `MONTH`, and `ALL`. |
144 | | - |
145 | | -### Vote |
146 | | -These are the different vote options, which are used to vote on rants and comments. |
147 | | -The vote options are `UP`, `NONE` and `DOWN`. |
148 | | - |
149 | | -### Reason |
150 | | -These are the reasons for a downvote. |
151 | | -The options are `NOT_FOR_ME`, `REPOST` and `OFFENSIVE_SPAM`. |
0 commit comments