Skip to content

Commit

Permalink
Merge pull request #2 from rbrick/master
Browse files Browse the repository at this point in the history
Added the rest of FOAAS REST API.
  • Loading branch information
Spencer Sederberg authored and Spencer Sederberg committed Jun 1, 2015
2 parents fcdacba + 9bc3afd commit 52172eb
Show file tree
Hide file tree
Showing 6 changed files with 386 additions and 258 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,16 @@ import me.AnUnknownMiner.JFOAAS.Output;
public class Example {

public static void main(String[] args) throws IOException, URISyntaxException {
JFOAAS j = new JFOAAS();
JFOAAS j = new JFOAAS("From", "Name");

String fuck = j.withFrom("From").withName("Name").withFuck(Fuck.DONUT).withOutput(Output.STRING).build();
String fuck = j.withFuck(Fuck.DONUT).build();

System.out.println(fuck + "\n");

// You can directly print without using a String!
System.out.println(j.withFrom("From11").withName("Name11").withFuck(Fuck.DIABETES).withOutput(Output.STRING).build());
System.out.println(j.withFrom("From11").withName("Name11").withFuck(Fuck.DIABETES).build());

String uri = j.withFrom("FromURI").withName("NameURI").withFuck(Fuck.SHAKESPEARE).withOutput(Output.URL).build();
String uri = j.url();

// Opens the default browser to the URI.
if(Desktop.isDesktopSupported()) Desktop.getDesktop().browse(new URI(uri));
Expand Down
82 changes: 50 additions & 32 deletions src/me/AnUnknownMiner/JFOAAS/Fuck.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,36 +26,54 @@

public enum Fuck {

OFF,
YOU,
THIS,
THAT,
EVERYTHING,
EVERYONE,
DONUT,
SHAKESPEARE,
LINUS,
KING,
PINK,
LIFE,
CHAINSAW,
OUTSIDE,
THING,
THANKS,
FLYING,
FASCINATING,
MADISON,
COOL,
NUGGET,
YODA,
WHAT,
BECAUSE,
BYE,
DIABETES,
BUS,
XMAS,
AWESOME,
TUCKER,
BUCKET

OFF("/off/:name/:from"),
YOU("/you/:name/:from"),
THIS("/this/:from"),
THAT("/that/:from"),
EVERYTHING("/everything/:from"),
EVERYONE("/everyone/:from"),
DONUT("/donut/:name/:from"),
SHAKESPEARE("/shakespeare/:name/:from"),
LINUS("/linus/:name/:from"),
KING("/king/:name/:from"),
PINK("/pink/:from"),
LIFE("life/:from"),
CHAINSAW("/chainsaw/:name/:from"),
OUTSIDE("/outside/:name/:from"),
THING("/:thing/:from"),
THANKS("/thanks/:from"),
FLYING("/flying/:from"),
FASCINATING("/fascinating/:from"),
MADISON("/madison/:name/:from"),
COOL("/cool/:from"),
FIELD("/field/:name/:from/:reference"),
NUGGET("/nugget/:name/:from"),
YODA("/yoda/:name/:from"),
WHAT("/what/:from"),
BECAUSE("/because/:from"),
BYE("/bye/:from"),
DIABETES("/diabetes/:from"),
BUS("/bus/:name/:from"),
XMAS("/xmas/:name/:from"),
AWESOME("/awesome/:from"),
TUCKER("tucker/:from"),
BUCKET("/bucket/:from"),
BALLMER("/ballmer/:name/:company/:from"),
CAN_I_USE("/caniuse/:tool/:from");

/**
* The URL path representation
*/
private String path;

Fuck(String path) {
this.path = path;
}


public String getPath() {
return path;
}


}
96 changes: 96 additions & 0 deletions src/me/AnUnknownMiner/JFOAAS/HTTPClient.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
/**
* The MIT License (MIT)
* <p/>
* Copyright (c) 2015 Spencer Sederberg
* <p/>
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
* <p/>
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
* <p/>
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

package me.AnUnknownMiner.JFOAAS;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.concurrent.FutureTask;

public class HTTPClient {

private FutureTask<String> result;

private final URL url;

private final ResponseType responseType;

public HTTPClient(String url, ResponseType responseType) throws MalformedURLException {
this.url = new URL(url);
this.responseType = responseType;
}

public ResponseType getResponseType() {
return responseType;
}

public URL getUrl() {
return url;
}


public Future<String> makeGet() {
try {
result = new FutureTask<>(new Callable<String>() {
@Override
public String call() throws Exception {
StringBuilder result = new StringBuilder();

HttpURLConnection connection = (HttpURLConnection) url.openConnection();
// Make a get request
connection.setRequestMethod("GET");
// Set the user agent and the Accept value
connection.addRequestProperty("User-Agent", "Mozilla/5.0");
connection.addRequestProperty("Accept", responseType.getAcceptKey());
BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String l;
while ((l = reader.readLine()) != null) {
result.append(l).append('\n');
}
return result.toString();
}
});
result.run();
} catch (Exception ex) {
ex.printStackTrace();
}

return result;
}

public String getResponse() {
try {
return makeGet().get();
} catch (InterruptedException | ExecutionException e) {
e.printStackTrace();
}
return null;
}
}
Loading

0 comments on commit 52172eb

Please sign in to comment.