Skip to content

Commit

Permalink
converted transforming data into List to HashMap since its an obvious…
Browse files Browse the repository at this point in the history
… improvement
  • Loading branch information
BasketBandit committed Apr 28, 2019
1 parent bae8bef commit e1941dc
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 26 deletions.
16 changes: 13 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# RuneAPI.java
Java wrapper for the OSRS portion of the [RuneScape](https://oldschool.runescape.com/) API.
Simple open source Java wrapper for the OSRS portion of the [RuneScape](https://oldschool.runescape.com/) API.

## Class Data

Expand All @@ -14,14 +14,24 @@ Java wrapper for the OSRS portion of the [RuneScape](https://oldschool.runescape
### Get

**Stats** - Retrieve the stats stored about the provided player.

**#1** - Loop all skills and print their data.
```java
List<Skill> stats = RuneAPI.getStats(username);
Map<String, Skill> stats = RuneAPI.getStats(username);

for(Skill skill: stats) {
for(Skill skill: stats.values()) {
System.out.printf("%s - %,d - %,d - %,d \n", skill.getName(), skill.getRank(), skill.getLevel(), skill.getExperience());
}
```

**#2** - Find specific skill and print its data.
```java
Map<String, Skill> stats = RuneAPI.getStats(username);
Skill overall = stats.get("Overall");

System.out.printf("%s - %,d - %,d - %,d \n", overall.getName(), overall.getRank(), overall.getLevel(), overall.getExperience());
```

## Download

[![](https://jitpack.io/v/BasketBandit/RuneAPI.java.svg)](https://jitpack.io/#BasketBandit/RuneAPI.java)
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/baseketbandit/runeapi/RuneAPI.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
import com.baseketbandit.runeapi.entity.Skill;
import com.baseketbandit.runeapi.io.RequestHandler;

import java.util.List;
import java.util.Map;

public class RuneAPI {

public static List<Skill> getStats(String username) {
public static Map<String, Skill> getStats(String username) {
return RequestHandler.doRequest(username.replace(" ", "%20"));
}
}
16 changes: 8 additions & 8 deletions src/main/java/com/baseketbandit/runeapi/io/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,32 @@

import com.baseketbandit.runeapi.entity.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.*;

class Parser {

/**
* Breaks down raw data from the API to a list of Skill objects.
* Breaks down raw data from the API to a hash map of Skill objects.
*
* @param data raw data from the RuneScape API
* @return List<Skill> list of objects of type Skill
* @return Map<String, Skill> HashMap of objects of type Skill
*/
static List<Skill> parseStats(String data) {
static Map<String, Skill> parseStats(String data) {
List<Enum> skillNames = Arrays.asList(Skill.Name.values());
ArrayList<Skill> skills = new ArrayList<>();
Map<String, Skill> skills = new HashMap<>();
String[] rawSkill = data.split("\n");

int i = 0;
for(String raw: rawSkill) {
if(i < Skill.Name.values().length) {
String[] r = raw.split(",");
skills.add(new Skill(skillNames.get(i).name(), Integer.parseInt(r[0]), Integer.parseInt(r[1]), Integer.parseInt(r[2])));
skills.put(skillNames.get(i).name(), new Skill(skillNames.get(i).name(), Integer.parseInt(r[0]), Integer.parseInt(r[1]), Integer.parseInt(r[2])));
i++;
continue;
}
break;
}

return skills;
}
}
14 changes: 7 additions & 7 deletions src/main/java/com/baseketbandit/runeapi/io/RequestHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,20 @@
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.HashMap;
import java.util.Map;

public class RequestHandler {
private static final Logger log = LoggerFactory.getLogger(RequestHandler.class);
private static final OkHttpClient client = new OkHttpClient();

/**
* A OkHttp method used to connect to and return the string of data from the server.
* A OkHttp method used to connect to and return a HashMap of data from the server.
*
* @param username the username to do the request against
* @return List<Skill> list of objects of type Skill or an `empty` list if no results are found.
* @return Map<String, Skill> HashMap of objects of type Skill or an `empty` map if no results are found.
*/
public static List<Skill> doRequest(String username) {
public static Map<String, Skill> doRequest(String username) {
try {
Request request = new Request.Builder()
.url("https://secure.runescape.com/m=hiscore_oldschool/index_lite.ws?player=" + username)
Expand All @@ -30,13 +30,13 @@ public static List<Skill> doRequest(String username) {

if(response.code() != 200) {
log.error(response.code() + " - No results found for username `" + username.replace("%20", " ") + "`");
return new ArrayList<>();
return new HashMap<>();
}

return Parser.parseStats(response.body().string());
} catch(IOException e) {
log.error("There was a problem processing that request. [Highscores OSRS]");
return new ArrayList<>();
return new HashMap<>();
}
}
}
21 changes: 15 additions & 6 deletions src/test/java/com/baseketbandit/runeapi/RuneAPITest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,33 +3,42 @@
import com.baseketbandit.runeapi.entity.Skill;
import org.junit.Test;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static org.junit.Assert.*;

public class RuneAPITest {

@Test
public void getStats() {
List<Skill> stats = RuneAPI.getStats("xwr");
for(Skill skill: stats) {
Map<String, Skill> stats = RuneAPI.getStats("xwr");
for(Skill skill: stats.values()) {
System.out.println(skill.getName() + " - " + skill.getLevel());
}
}

@Test
public void getStatsNotFound() {
List<Skill> stats = RuneAPI.getStats("abcdefghijklmnopqrstuvwxyz");
for(Skill skill: stats) {
Map<String, Skill> stats = RuneAPI.getStats("abcdefghijklmnopqrstuvwxyz");
for(Skill skill: stats.values()) {
System.out.println(skill.getName() + " - " + skill.getLevel());
}
}

@Test
public void getStatsNameSpace() {
List<Skill> stats = RuneAPI.getStats("Iron Lewiso");
for(Skill skill: stats) {
Map<String, Skill> stats = RuneAPI.getStats("Iron Lewiso");
for(Skill skill: stats.values()) {
System.out.println(skill.getName() + " - " + skill.getLevel());
}
}

@Test
public void getStatsMapped() {
Map<String, Skill> stats = RuneAPI.getStats("xwr");
Skill cooking = stats.get("Cooking");
System.out.println(cooking.getLevel());
}
}

0 comments on commit e1941dc

Please sign in to comment.