Created
January 14, 2012 03:35
-
-
Save osima/1610144 to your computer and use it in GitHub Desktop.
fetch code from gist using gist api v3.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Grab(group='commons-httpclient', module='commons-httpclient', version='3.1') | |
import org.apache.commons.httpclient.* | |
import org.apache.commons.httpclient.methods.* | |
import groovy.json.* | |
class GistFetcher { | |
static String ENCODING='UTF-8' | |
String getJsonString( String gistId ){ | |
def retVal = '' | |
def gistUrl = "https://api.github.com/gists/${gistId}" | |
def hc = new HttpClient() | |
def method = new GetMethod( gistUrl ) | |
hc.executeMethod( method ) | |
def reader = new InputStreamReader( method.getResponseBodyAsStream(), ENCODING ) | |
retVal = reader.text | |
reader.close() | |
method.releaseConnection() | |
retVal | |
} | |
Map getJsonObject( String gistId ){ new JsonSlurper().parseText( getJsonString(gistId) ) } | |
String id | |
String filename | |
String getContent(){ | |
assert id!=null | |
assert filename!=null | |
getJsonObject(id).files."${filename}".content | |
} | |
String getType(){ | |
assert id!=null | |
assert filename!=null | |
getJsonObject(id).files."${filename}".type | |
} | |
} | |
def id = '1174714' | |
def filename = 'MCanvas.java' | |
println new GistFetcher( id:id,filename:filename ).getType() | |
println new GistFetcher( id:id,filename:filename ).getContent() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment