Skip to content

Instantly share code, notes, and snippets.

Avatar
💓
Loving Clojure

Jakub Holy holyjak

💓
Loving Clojure
View GitHub Profile
@holyjak
holyjak / S3FilesResource.java
Created Sep 9, 2012
REST service mixing data and representation
View S3FilesResource.java
public class S3FilesResource {
AmazonS3Client amazonS3Client;
// ...
@Path("/files")
public String listS3Files() {
StringBuilder html = new StringBuilder("<html><body>");
List<S3ObjectSummary> files = this.amazonS3Client.listObjects("myBucket").getObjectSummaries();
@holyjak
holyjak / S3FilesResourceTest.java
Created Sep 9, 2012
S3FilesResourceTest - original
View S3FilesResourceTest.java
public class S3FilesResourceTest {
@Test
public void listFilesButNotDirectoriesAsHtml() throws Exception {
S3FilesResource resource = new S3FilesResource(/* pass AWS credentials ... */);
String html = resource.listS3Files();
assertThat(html)
.contains("<a href='/content?fileName=/dir/file1.txt'>/dir/file1.txt</a>")
.contains("<a href='/content?fileName=/dir/another.txt'>/dir/another.txt</a>")
.doesNotContain("/dir/</a>"); // directories should be excluded
@holyjak
holyjak / S3Facade.java
Created Sep 9, 2012
S3FilesResource - refactored
View S3Facade.java
public interface S3Facade {
List<S3File> listObjects(String bucketName);
}
@holyjak
holyjak / S3FilesResourceTest.java
Created Sep 9, 2012
S3FilesResourceTest - refactored
View S3FilesResourceTest.java
public class S3FilesResourceTest {
private static class FakeS3Facade implements S3Facade {
List<S3File> fileList;
public List<S3File> listObjects(String bucketName) {
return fileList;
}
}
@holyjak
holyjak / A_Client.py
Created May 4, 2013
Method Promiscuity Or The Case For Encapsulation: Do not expose more information and structure to the clients than necessary
View A_Client.py
...
# The client code
@my.route('/cleanjson/<collection>/')
def getcleanjson(collection):
cleandata = Cleanjson()
cleandata.get_mongojson(str(collection), "?s={'_id': 1}")
cleandata.parse_json(cleandata.rawjson)
return cleandata.cleanjson
@holyjak
holyjak / A_Client2.py
Created May 4, 2013
Method Promiscuity Or The Case For Encapsulation: Do not expose more information and structure to the clients than necessary - improved version See https://gist.github.com/jakubholynet/5517865
View A_Client2.py
...
# The client code
@my.route('/cleanjson/<collection>/')
def getcleanjson(collection):
return Cleanjson().get_mongojson(str(collection), "?s={'_id': 1}", format=True)
@holyjak
holyjak / LogEntry.java
Last active Dec 17, 2015
Simple vs. Easy: Writing A Generic Code To Avoid Duplication - replacing a number of anemic data structures with a generic one - BEFORE See http://wondersofcode.wordpress.com/2013/05/04/simple-vs-easy…id-duplication/
View LogEntry.java
public interface LogEntry {
Date getTimestamp();
/** For dumping into a .tsv file to be imported into Hadoop */
String toTabDelimitedString();
String getSearchTerms();
boolean hasSearchTerms();
String[][] getColumns();
String getKey();
void mergeWith(LogEntry entry);
}
@holyjak
holyjak / LogEntry2.java
Created May 4, 2013
Simple vs. Easy: Writing A Generic Code To Avoid Duplication - replacing a number of anemic data structures with a generic one - IMPROVED. See http://wondersofcode.wordpress.com/2013/05/04/simple-vs-easy…id-duplication/
View LogEntry2.java
public interface LogEntry {
// same as before
}
@holyjak
holyjak / augmentSeriesWithPreviousValues.js
Last active Dec 17, 2015
Intention Hidden In Implementation And Misty Edge of Validity - why does this function do what it does and under what assumptions does it operate? See http://wondersofcode.wordpress.com/2013/05/05/intention-hidden-in-implementation-and-misty-edge-of-validity/
View augmentSeriesWithPreviousValues.js
// compare two series and add the delta to the data for the current period
augmentSeriesWithPreviousValues:function (seriesData, previousSeriesData) {
var series = seriesData.series;
seriesData.previous = [];
var previousSeries = previousSeriesData.series;
... // checks, computing maxRows, maxColumns, previousCategories
// that maps category name to series index (id)
for (var seriesId = 0; seriesId < series.length; seriesId++) {
@holyjak
holyjak / augmentSeriesWithPreviousValues2.js
Created May 5, 2013
Intention Hidden In Implementation And Misty Edge of Validity - why does this function do what it does and under what assumptions does it operate? REFACTORED. See http://wondersofcode.wordpress.com/2013/05/05/intention-hidden-in-implementation-and-misty-edge-of-validity/
View augmentSeriesWithPreviousValues2.js
// Set seriesData.previous to hold data series of the previous period
// so that we may compute deltas (e.g. +10%).
// We also ensure that all the series, previous and current, have the same
// number of data points, adding fake ones if necessary, so that the delta-computation
// code <tbd:add reference> doesn't blow up. There may be some data missing
// at the end of a period, for example because months have different numbers of days.
// The fake elements and also any other undefined values will default to [0,0], which
// should be OK with all (line,area,..) graphs and tables.
// E.x.: seriesData.series[0].data = [ [1,"12%"],[2,"22%"],..,[31,"18%"] ],
// the previous month had only 30 days so we add a fake element for day 31;