Skip to content

Instantly share code, notes, and snippets.

View holyjak's full-sized avatar
💓
Loving Clojure

Jakub Holý holyjak

💓
Loving Clojure
View GitHub Profile
@holyjak
holyjak / S3FilesResource.java
Created September 9, 2012 11:28
REST service mixing data and representation
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 September 9, 2012 12:18
S3FilesResourceTest - original
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 September 9, 2012 12:22
S3FilesResource - refactored
public interface S3Facade {
List<S3File> listObjects(String bucketName);
}
@holyjak
holyjak / S3FilesResourceTest.java
Created September 9, 2012 12:53
S3FilesResourceTest - refactored
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 15:45
Method Promiscuity Or The Case For Encapsulation: Do not expose more information and structure to the clients than necessary
...
# 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 16:00
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
...
# 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 December 17, 2015 00:00
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/
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 17:40
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/
public interface LogEntry {
// same as before
}
@holyjak
holyjak / augmentSeriesWithPreviousValues.js
Last active December 17, 2015 00:29
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/
// 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 20:27
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/
// 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;