Permalink
Browse files

initial checkin

  • Loading branch information...
scholnicks
scholnicks committed Jul 14, 2013
1 parent 78c6394 commit 69fb743a33defd4fbca0d3cd7f98cb8dff86c287
View
@@ -1,4 +1,9 @@
*.class
*/bin/*
.metadata
RemoteSystemsTempFiles
isbndb.properties
# Package Files #
*.jar
View
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="src"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.7"/>
<classpathentry kind="lib" path="/Users/steve/development/java/library/commons-lang-2.5.jar"/>
<classpathentry kind="lib" path="/Users/steve/development/java/library/jackson-annotations.jar"/>
<classpathentry kind="lib" path="/Users/steve/development/java/library/jackson-core.jar"/>
<classpathentry kind="lib" path="/Users/steve/development/java/library/jackson-databind.jar"/>
<classpathentry kind="lib" path="/Users/steve/development/java/library/log4j-1.2.16.jar"/>
<classpathentry kind="output" path="bin"/>
</classpath>
View
@@ -0,0 +1,2 @@
/bin
/.settings
View
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>IsbnDB</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>
@@ -0,0 +1,77 @@
package net.scholnick.isbndb;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.List;
import java.util.Properties;
import net.scholnick.isbndb.domain.Book;
import org.apache.log4j.Logger;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
public final class BooksProxy {
private static final Logger log = Logger.getLogger(BooksProxy.class);
private final ObjectMapper mapper;
private final String developerKey;
private static final BooksProxy INSTANCE = new BooksProxy();
private long lastAccessedTime;
public static BooksProxy getInstance() {
return INSTANCE;
}
public List<Book> getBooks(String title) throws JsonParseException, JsonMappingException, IOException, URISyntaxException {
checkElapsedTime();
URI uri = new URI("http","isbndb.com","/api/v2/json/" + developerKey + "/books", "q=" + title,null);
return parse( uri.toURL() );
}
private BooksProxy() {
mapper = new ObjectMapper();
lastAccessedTime = -1L;
try {
Properties props = new Properties();
props.load( getClass().getClassLoader().getResourceAsStream("isbndb.properties") );
developerKey = props.getProperty("developer.key");
}
catch (IOException e) {
throw new RuntimeException("Unable to read isbndb.properties",e);
}
}
public List<Book> parse(URL url) throws JsonParseException, JsonMappingException, IOException {
return mapper.readValue(url.openStream(),BooksResult.class).getData();
}
private void checkElapsedTime() {
long sleepTime = 1000L; // 1 second
if (lastAccessedTime != -1L) {
long elapsedTime = System.currentTimeMillis() - lastAccessedTime;
if (elapsedTime > 1000) // 1 sec
return;
sleepTime = 1000L - elapsedTime;
log.debug("Pausing. Elapsed time = " + elapsedTime + " ms, waiting for " + sleepTime + " ms");
}
try {
Thread.sleep(sleepTime);
}
catch (InterruptedException ie) {
// just return
}
}
}
@@ -0,0 +1,28 @@
package net.scholnick.isbndb;
import java.util.List;
import net.scholnick.isbndb.domain.Book;
import org.apache.commons.lang.builder.ToStringBuilder;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
@JsonIgnoreProperties(ignoreUnknown = true)
public final class BooksResult {
private List<Book> data;
public List<Book> getData() {
return data;
}
public void setData(List<Book> data) {
this.data = data;
}
/** {@inheritDoc} */
@Override
public String toString() {
return ToStringBuilder.reflectionToString(this);
}
}
@@ -0,0 +1,30 @@
package net.scholnick.isbndb.domain;
import org.apache.commons.lang.builder.ToStringBuilder;
public final class Author {
private String id;
private String name;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
/** {@inheritDoc} */
@Override
public String toString() {
return ToStringBuilder.reflectionToString(this);
}
}
@@ -0,0 +1,127 @@
package net.scholnick.isbndb.domain;
import java.util.List;
import org.apache.commons.lang.builder.ToStringBuilder;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;
@JsonIgnoreProperties(ignoreUnknown = true)
public final class Book {
@JsonProperty("title_latin") private String title;
@JsonProperty("isbn13") private String isbn;
@JsonProperty("physical_description_text") private String physicalDescription;
@JsonProperty("publisher_name") private String publisher;
@JsonProperty("language") private String language;
@JsonProperty("notes") private String notes;
@JsonProperty("summary") private String summary;
@JsonProperty("edition_info") private String edition;
@JsonProperty("dewey_normal") private String deweyNumber;
@JsonProperty("lcc_number") private String lccNumber;
@JsonProperty("author_data") private List<Author> authors;
@JsonProperty("subjectIds") private List<String> subjectIds;
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getIsbn() {
return isbn;
}
public void setIsbn(String isbn) {
this.isbn = isbn;
}
public List<Author> getAuthors() {
return authors;
}
public void setAuthors(List<Author> authors) {
this.authors = authors;
}
public String getPhysicalDescription() {
return physicalDescription;
}
public void setPhysicalDescription(String physicalDescription) {
this.physicalDescription = physicalDescription;
}
public String getPublisher() {
return publisher;
}
public void setPublisher(String publisher) {
this.publisher = publisher;
}
public String getLanguage() {
return language;
}
public void setLanguage(String language) {
this.language = language;
}
public String getNotes() {
return notes;
}
public void setNotes(String notes) {
this.notes = notes;
}
public String getSummary() {
return summary;
}
public void setSummary(String summary) {
this.summary = summary;
}
public String getEdition() {
return edition;
}
public void setEdition(String edition) {
this.edition = edition;
}
public String getDeweyNumber() {
return deweyNumber;
}
public void setDeweyNumber(String deweyNumber) {
this.deweyNumber = deweyNumber;
}
public String getLccNumber() {
return lccNumber;
}
public void setLccNumber(String lccNumber) {
this.lccNumber = lccNumber;
}
public List<String> getSubjectIds() {
return subjectIds;
}
public void setSubjectIds(List<String> subjectIds) {
this.subjectIds = subjectIds;
}
/** {@inheritDoc} */
@Override
public String toString() {
return ToStringBuilder.reflectionToString(this);
}
}
View
@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="src"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.7"/>
<classpathentry kind="lib" path="/Users/steve/development/java/library/commons-lang-2.5.jar"/>
<classpathentry kind="lib" path="/Users/steve/development/java/library/jackson-annotations.jar"/>
<classpathentry kind="lib" path="/Users/steve/development/java/library/jackson-core.jar"/>
<classpathentry kind="lib" path="/Users/steve/development/java/library/jackson-databind.jar"/>
<classpathentry kind="lib" path="/Users/steve/development/java/library/junit.jar"/>
<classpathentry combineaccessrules="false" kind="src" path="/IsbnDB"/>
<classpathentry kind="output" path="bin"/>
</classpath>
View
@@ -0,0 +1,2 @@
/bin
/.settings
View
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>UnitTests</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>
@@ -0,0 +1,52 @@
package net.scholnick.isbndb.unittests;
import static junit.framework.Assert.*;
import java.io.IOException;
import java.net.URL;
import java.util.List;
import net.scholnick.isbndb.BooksProxy;
import net.scholnick.isbndb.domain.Book;
import org.junit.Test;
public final class BookTestCase {
@Test
public void singleBookTitle() throws IOException {
List<Book> books = BooksProxy.getInstance().parse( getURL("single_book.json") );
assertEquals("Bag of bones", books.get(0).getTitle());
}
@Test
public void singleBookAuthor() throws IOException {
List<Book> books = BooksProxy.getInstance().parse( getURL("single_book.json") );
assertEquals("King, Stephen Harris Morley", books.get(0).getAuthors().get(0).getName());
assertEquals("king_stephen_harris_morley", books.get(0).getAuthors().get(0).getId());
}
@Test
public void multipleBooksTitle() throws IOException {
List<Book> books = BooksProxy.getInstance().parse( getURL("virus.json") );
assertEquals(10, books.size());
assertEquals("Virus", books.get(1).getTitle());
}
@Test
public void downloadBooks() throws Exception {
List<Book> books = BooksProxy.getInstance().getBooks("virus");
assertEquals(10, books.size());
}
@Test
public void downloadBookWithSpacesInTitle() throws Exception {
List<Book> books = BooksProxy.getInstance().getBooks("bag of bones");
assertEquals(10, books.size());
}
private URL getURL(String fileName) throws IOException {
return this.getClass().getClassLoader().getResource("net/scholnick/isbndb/unittests/data/" + fileName);
}
}
Oops, something went wrong.

0 comments on commit 69fb743

Please sign in to comment.