Created
May 1, 2014 18:33
-
-
Save aows/91aff80e03c95104d06c to your computer and use it in GitHub Desktop.
Pushbullet with Groovy
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
import static groovyx.net.http.Method.GET | |
import static groovyx.net.http.Method.POST | |
import static groovyx.net.http.ContentType.JSON | |
import groovy.json.JsonSlurper | |
@Grab(group='org.codehaus.groovy.modules.http-builder', module='http-builder', version='0.7' ) | |
class GroovyBullet { | |
def apikey | |
def http = new groovyx.net.http.HTTPBuilder('https://api.pushbullet.com/api/') | |
GroovyBullet( apikey ) { | |
this.apikey = apikey | |
} | |
Collection getDevices() { | |
http.request( GET, JSON ) { | |
uri.path = 'devices' | |
headers.'Authorization' = "Basic ${apikey.bytes.encodeBase64().toString()}" | |
response.success = { resp, json -> | |
return json.devices | |
} | |
} | |
} | |
void sendNote( iden, title, text ) { | |
send( iden, [type: 'note', title: title, body: text] ) | |
} | |
void sendLink( iden, title, url ) { | |
send( iden, [type: 'link', url: url, title: title] ) | |
} | |
private void send ( iden, requestBody ) { | |
requestBody.putAll( [device_iden: iden] ) | |
http.request( POST, JSON ) { | |
uri.path = 'pushes' | |
headers.'Authorization' = "Basic ${apikey.bytes.encodeBase64().toString()}" | |
body = requestBody | |
} | |
} | |
} | |
def apikey = "apikey" | |
def groovyBullet = new GroovyBullet( apikey ) | |
def iden = groovyBullet.getDevices()[0].iden | |
groovyBullet.sendNote(iden, 'hi from PDX', '') | |
groovyBullet.sendLink(iden, 'hi from PDX', 'http://www.google.com') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment