Skip to content

Instantly share code, notes, and snippets.

View gabrieldewes's full-sized avatar
🎯
Focusing

Gabriel Dewes gabrieldewes

🎯
Focusing
View GitHub Profile
@gabrieldewes
gabrieldewes / MyWebClient.java
Created April 14, 2020 20:29
WebClient abstraction for property-based configuration
import io.netty.channel.ChannelException;
import io.netty.channel.ChannelOption;
import io.netty.handler.timeout.ReadTimeoutHandler;
import io.netty.handler.timeout.WriteTimeoutHandler;
import lombok.Getter;
import lombok.Setter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.ConfigurationProperties;
@gabrieldewes
gabrieldewes / FormURLEncodedToJSON.js
Created December 4, 2017 19:41
Parse x-www-form-urlencoded to Json object
function FormURLEncodedToJSON(str) {
"use strict";
var obj, i, pt, keys, j, ev;
if (typeof FormURLEncodedToJSON.br !== 'function') {
FormURLEncodedToJSON.br = function(repl) {
if (repl.indexOf(']') !== -1) {
return repl.replace(/\](.+?)(,|$)/g, function($1, $2, $3) {
return FormURLEncodedToJSON.br( $2 + '}' + $3 );
@gabrieldewes
gabrieldewes / DownloadResource.java
Created December 4, 2017 14:32
Baixar um arquivo de um servidor remoto
try {
URL url = new URL("http://127.0.0.1/scripts/download.php");
ReadableByteChannel readableByteChannel = Channels.newChannel(url.openStream());
FileOutputStream fileOutputStream = new FileOutputStream("resourceFileName.db");
fileOutputStream.getChannel().transferFrom(readableByteChannel, 0, Long.MAX_VALUE);
} catch (Exception ex) {
@gabrieldewes
gabrieldewes / InstanceManager.java
Created December 4, 2017 14:29
Controlar instâncias de um sistema via sockets server/client
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.EventListener;
@gabrieldewes
gabrieldewes / UploadResource.java
Created November 23, 2017 18:07
Uploading some binary file to http server
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.nio.file.Files;
public class UploadResource {
// Uploading some binary file to http server
@gabrieldewes
gabrieldewes / toYyyymmddhhmmss.js
Created November 2, 2017 18:36
JS Date "yyyy-mm-dd hh:mm:ss" format
(function() {
"use strict";
Date.prototype.toYyyymmddhhmmss = function() {
var yyyy = this.getFullYear(),
mm = this.getMonth() < 9 ? ("0" + (this.getMonth() + 1)) : (this.getMonth() + 1),
dd = this.getDate() < 10 ? ("0" + this.getDate()) : this.getDate(),
hh = this.getHours() < 10 ? ("0" + this.getHours()) : this.getHours(),
min = this.getMinutes() < 10 ? ("0" + this.getMinutes()) : this.getMinutes(),
ss = this.getSeconds() < 10 ? ("0" + this.getSeconds()) : this.getSeconds();
return "".concat(yyyy).concat("-")
@gabrieldewes
gabrieldewes / count_tables_and_sum_rows.sql
Created February 10, 2017 17:35
Get the number of tables and the total of rows of all tables in MySQL
SELECT SUM(TABLE_ROWS) AS 'Total de Linhas', COUNT(*) AS 'Total de Tabelas'
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_SCHEMA = 'databaseName';
@gabrieldewes
gabrieldewes / data_to_table.js
Last active March 23, 2017 18:45
Simple snippet to generate HTML table for JSON or Array data.
if ("undefined"===typeof jQuery) throw new Error("This function requires jQuery.");
function table(element, data) {
var g, a, b, r, i, e, l,
s = Date.now();
g = $("<table class='table table-responsive table-hover'/>").appendTo(element);
a = $("<thead />").appendTo(g);
/* Generates the table thead */
r = data[0]; // Some element of any position, use the element with more atributtes
for (i in r) {
if (r.hasOwnProperty(i)) {
@gabrieldewes
gabrieldewes / br_states.json
Last active February 9, 2017 16:58
Todos estados do Brasil com código UF, nome, sigla, área em KM² e região, segundo IBGE.
[{
"codigo_uf": 11,
"unidade_federacao": "Rondônia",
"sigla": "RO",
"area_km2": 237765.376,
"regiao": "Norte"
},{
"codigo_uf": 12,
"unidade_federacao": "Acre",
"sigla": "AC",
@gabrieldewes
gabrieldewes / objectId.js
Last active February 3, 2017 20:34
Quickly generate random unique strings
/* It's of unspecified quality and not RFC4122.
* Very slow due to string replaces
*/
var objectId = function () {
var timestamp = (Date.now() / 1000 | 0).toString(16);
return timestamp + 'xxxxxxxxxxxxxxxx'.replace(/[x]/g, function() {
return (Math.random() * 16 | 0).toString(16);
}).toLowerCase();
};