Skip to content

Instantly share code, notes, and snippets.

import java.net.InetAddress
object CIDRChecker {
def isInRange(cidrs: Array[String], address: String): Boolean = {
cidrs.exists { cidr =>
if (!cidr.contains("/")) {
InetAddress.getByName(cidr).getHostAddress == InetAddress.getByName(address).getHostAddress
} else {
val Array(cidrAddress, cidrMask) = cidr.split("/")
val cidrInt = InetAddress.getByName(cidrAddress).getAddress.zipWithIndex.map { case (b, i) => (b & 0xff) << (8 * (3 - i)) }.sum
@roundrop
roundrop / webhook_recorder.gs
Created November 11, 2022 02:12
App Script that receives webhook requests and records them in a Google Spreadsheet.
function doPost(e) {
const sheet = SpreadsheetApp.getActiveSpreadsheet().getSheets()[0];
sheet.getRange("1:1").insertCells(SpreadsheetApp.Dimension.ROWS);
sheet.getRange(1, 1).setValue((new Date).toLocaleString('ja-JP'));
sheet.getRange(1, 2).setValue(e.postData.contents);
// send 200 OK status back to confirm receiving webhook
var response = HtmlService.createHtmlOutput();
return response;
@roundrop
roundrop / save_gh_issues.sh
Created February 24, 2017 11:27
Save GitHub Issues by curl
#!/usr/bin/env bash
USER=user_name
PERSONAL_TOKEN=*************************************************
USER_OR_ORG=user_name_or_organization_name
REPO=repo_name
# can get per max 100 issues
curl -u $USER:$PERSONAL_TOKEN -o issues_1.json -# https://api.github.com/repos/$USER_OR_ORG/$REPO/issues?state=all\&page=1\&per_page=100
curl -u $USER:$PERSONAL_TOKEN -o issues_2.json -# https://api.github.com/repos/$USER_OR_ORG/$REPO/issues?state=all\&page=2\&per_page=100
@roundrop
roundrop / SomeController.scala
Created August 10, 2016 02:45
Skinny Framework の multiParams をバリデーションする
// tags が multiParams
// まず xxxxParams に Seq で入れてしまう
def xxxxParams: Params = {
val title = params.getOrElse("title", "")
val body = params.getOrElse("body", "")
val tags = multiParams.get("tags").getOrElse(Seq()).map(_.trim).distinct.filter(_.nonEmpty)
Params(Map("title" -> title, "body" -> body, "tags" -> tags))
}
// tags についてはカスタムな Seq 用のバリデータ(maxLengths)を適用する
def xxxxForm: MapValidator = validation(xxxxParams,
@roundrop
roundrop / MyQuercusServlet.java
Last active August 29, 2015 14:01
QuercusServletの初期化
package jp.roundrop.quercus.servlet;
import com.caucho.quercus.QuercusContext;
import com.caucho.quercus.lib.session.QuercusSessionManager;
import com.caucho.quercus.servlet.QuercusServlet;
import com.caucho.util.Alarm;
import org.slf4j.bridge.SLF4JBridgeHandler;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
@roundrop
roundrop / gist:6789917
Created October 2, 2013 06:52
S2JDBC で PostgreSQL の CopyManager を取得する方法
public CopyManager getCopyManager() throws SQLException {
Connection conn = ((JdbcManagerImplementor) jdbcManager).getDataSource().getConnection();
XAConnection xaConn = ((ConnectionWrapperImpl) conn).getXAConnection();
Connection pgConn = xaConn.getConnection();
return ((PGConnection) pgConn).getCopyAPI();
}
@roundrop
roundrop / index.html
Created September 10, 2013 02:57
javascriptでテキストボックスの末尾にキャレット移動
<html>
<head></head>
<body>
<input type="text" id="foo" value="" />
</body>
</html>
@roundrop
roundrop / code.gas
Created July 4, 2013 11:07
GroovyからGoogle Spreadsheetに何かを書き込むってのを書いてみた 参考: https://gist.github.com/cho45/4222750 GETでリクエストするのであまり大量のデータは書き込めない
function doGet(req) {
var result = { ok : 0 };
if (req.parameters.api_key == 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx') {
var rows = [];
try { rows = JSON.parse(req.parameters.rows) } catch (e) { }
if (rows.length) {
var ss = SpreadsheetApp.openById('*****************************************');
var sheet = ss.getSheets()[0];
for (var i = 0, len = rows.length; i < len; i++) {
@roundrop
roundrop / S2RequestProcessor#setProperty(Object, String, Object) の拡張
Created June 24, 2013 11:05
S2RequestProcessor#setProperty(Object, String, Object) の拡張 PHPでよくある感じのattributes[]などでもActionFormにセットされるように拡張
private static final String INDEXED_DELIM = "[";
private static final String INDEXED_DELIM2 = "]";
@Override
protected void setProperty(Object bean, String name, Object value) {
if (bean == null) {
return;
}
int startIndexedIndex = name.indexOf(INDEXED_DELIM);
int endIndexedIndex = name.indexOf(INDEXED_DELIM2);
@roundrop
roundrop / S2ExceptionHandler
Created June 24, 2013 11:02
org.apache.struts.action.ExceptionHandler の拡張。 これをglobal-exceptionsのhandlerに使用することで、pathにSAStrutsが認識するパスをそのまま記述することが可能となる。
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ExceptionHandler;
import org.apache.struts.config.ExceptionConfig;
import org.seasar.struts.config.S2ActionMapping;