Skip to content

Instantly share code, notes, and snippets.

View EwanDawson's full-sized avatar

Ewan Dawson EwanDawson

  • Ping Identity
  • Edinburgh, UK
View GitHub Profile
@EwanDawson
EwanDawson / asType.groovy
Created October 10, 2012 11:03
Extend Groovy asType(Class) method to handle additional classes
import org.joda.time.DateTime
def oldAsType = String.metaClass.getMetaMethod("asType", [Class] as Class[])
String.metaClass.asType = { Class type ->
type.isAssignableFrom(DateTime) ?
new DateTime(delegate) :
oldAsType.invoke(delegate, [type] as Class[])
}
assert "2012-01-01T10:10:10" as DateTime == new DateTime("2012-01-01T10:10:10")
assert "1234" as Integer == 1234
@GrabResolver(name='grails-core', root='http://repo.grails.org/grails/core')
@Grab(group='org.grails', module='grails-datastore-gorm-mongo', version='1.0.0.BUILD-SNAPSHOT')
@Grab(group='org.slf4j', module='slf4j-simple', version='1.6.1')
import grails.persistence.*
import org.grails.datastore.gorm.mongo.config.*
MongoDatastoreConfigurer.configure("myDatabase", Book)
Book.withSession {
@EwanDawson
EwanDawson / days_in_year.R
Created May 14, 2013 08:55
#R script to get the number of days in a year. Given a vector containing years, returns a vector of the same length with the number of days in each year. Requires #zoo Example: > diy(2000:2004) [1] 366 365 365 365 366
diy <- function(year) {
require(zoo)
as.numeric(as.Date(as.yearmon(year) + 1) - as.Date(as.yearmon(year)))
}
{
"nonce": "0x0000000000000042",
"difficulty": "0x40000",
"alloc": {
"bbbbbaaaaa82db86a35502193b4c6ee9a76ebe8f": {
"balance": "10015200000000000000000"
}
},
payload[0] * payload[1]
invoke('https://gist.github.com/EwanDawson/67902abfb90239b502a255fcff0f69ca/raw/21037fea2ac819d92663dd2b8f6b80ac21512f32/multiply.groovy', [payload[0], 2])
payload.collate(2).collectEntries { [it[0], it[1]] }
@Grapes(@Grab(group='org.atteo', module='evo-inflector', version='1.2.2'))
import org.atteo.evo.inflector.English
English.plural(payload[0], payload[1] ?: 2)
@Grapes(@Grab("com.jcabi:jcabi-http:1.17.1"))
import com.jcabi.http.request.JdkRequest
import static java.net.URLEncoder.encode
def (title, message) = payload.collect { encode(it).replaceAll("\\+", "%20") }
new JdkRequest("https://api.simplepush.io/send/jzAa6m/${title.toUpperCase()}/$message").fetch()
import java.time.LocalDateTime
import static java.time.temporal.ChronoField.*
def now = LocalDateTime.now()
def everySecond = { it() }
def everyMinute = { if (now.get(SECOND_OF_MINUTE) == 0) it() }
def everyHour = { if (now.get(SECOND_OF_MINUTE) == 0 && now.get(MINUTE_OF_HOUR) == 0) it() }
def alert = { title, message -> invoke('https://gist.githubusercontent.com/EwanDawson/a65e720ed8ce0c9bc26d816de71ee776/raw/af0fa90ea080c758fec4077cd927dd1bd3d3da92/alert-me.groovy', [title, message]) }
everyMinute { alert("The time is now", now.toString()) }