Skip to content

Instantly share code, notes, and snippets.

View Joxebus's full-sized avatar
:octocat:
Working from home

Omar Bautista Joxebus

:octocat:
Working from home
View GitHub Profile
@Joxebus
Joxebus / 1_data_category.groovy
Last active June 14, 2021 16:44
Categories samples
import groovy.time.TimeCategory
use(TimeCategory) {
Date date = new Date("12/10/2008")
Date datePlus10Min = date + 30.minutes
Date dateMinus1Year = date - 1.years
Date datePlus10Hours = date + 10.hours
println date // now
println datePlus10Min // now + 10 min
@Joxebus
Joxebus / 1_yaml_slurper_sample.groovy
Last active August 29, 2023 12:49
YAML sample with Groovy 3
import groovy.yaml.YamlSlurper
def slurper = new YamlSlurper()
def configuration = '''
version: 3.0
environment: "dev"
context:
path: "/test"
endpoints:
@Joxebus
Joxebus / 1_read_json_object_with_json_slurper.groovy
Last active January 17, 2021 23:52
Working with JSON on Groovy - Here are some examples about how to work with JSON objects in Groovy
import groovy.json.JsonSlurper
String json = '{ "name" : "Omar", "lastname": "Bautista", "age" : 33 }'
def object = new JsonSlurper().parseText(json)
assert object instanceof Map
assert object.name == 'Omar'
@Joxebus
Joxebus / 1_reading_with_xml_slurper.groovy
Last active January 18, 2021 20:59
Working with XML on Groovy, this gist are a series of examples, also see the difference between XmlSlurper and XmlParser here: https://stackoverflow.com/a/7621603/14757065 for full samples visit: https://groovy-lang.org/processing-xml.html
import groovy.util.XmlSlurper
String xml = '''
<people>
<person age='33'>
<name>Omar</name>
</person>
<person age='30'>
<name>Maria</name>
</person>
@Joxebus
Joxebus / 01_to_string.groovy
Last active December 20, 2020 07:12
Groovy AST transformations examples to see a full list of examples you can go directly to the Groovy Documentation here: http://groovy-lang.org/metaprogramming.html
import groovy.transform.ToString
@ToString
class Person {
String name
String lastname
int age
}
def person = new Person(name:'Omar', lastname:'Bautista', age:33)
@Joxebus
Joxebus / dgm.groovy
Created November 28, 2020 00:16 — forked from wololock/dgm.groovy
@Grab(group = "org.reflections", module = "reflections", version = "0.9.11")
@Grab(group = "org.slf4j", module = "slf4j-simple", version = "1.7.25")
import groovy.json.JsonOutput
import org.codehaus.groovy.reflection.GeneratedMetaMethod
import org.reflections.Reflections
def reflections = new Reflections("org.codehaus.groovy.runtime")
def json = reflections.getSubTypesOf(GeneratedMetaMethod).collect {
@Joxebus
Joxebus / 1_numbers_closures.groovy
Last active December 10, 2020 18:36
Closure examples - Here you can see some samples about how to use closures in Groovy
Closure square = { num ->
num * num
}
Closure pow = { num, index ->
num.power(index)
}
List numbers = [2,5,20,10]
@Joxebus
Joxebus / DatesSample.groovy
Last active February 19, 2021 01:44
Working with dates on Groovy
List months = ["Enero", "Febrero", "Marzo", "Abril", "Mayo", "Junio",
"Julio", "Agosto", "Septiembre", "Octubre", "Noviembre", "Diciembre"]
def today = Date.parse("dd-MM-yyyy", "3-11-2020")
def clonedDate = today.clone()
def yesterday = today - 1
def tomorrow = today + 1
clonedDate++
assert clonedDate == tomorrow
@Joxebus
Joxebus / config
Last active January 13, 2021 00:21
This is an example about how to configure different SSH Keys for different services
# ~/.ssh/config
# For any configuration different than defined hosts
Host *
IdentityFile ~/.ssh/id_rsa
User obautista
# Github
Host github.com
HostName github.com
User git
@Joxebus
Joxebus / read-files-and-directories.groovy
Last active August 18, 2020 08:40
Groovy - 07 - Manipulación de archivos
File file = new File('your-dir-here')
def fileNames = []
file.eachFile {
if(it.isFile()) {
fileNames << it.name
}
}