Skip to content

Instantly share code, notes, and snippets.

@neoneye
neoneye / demo.swift
Created November 28, 2016 13:22
Extract regex matches
extension String {
func matchingStrings(regex: String) -> [[String]] {
guard let regex = try? NSRegularExpression(pattern: regex, options: []) else { return [] }
let nsString = self as NSString
let results = regex.matches(in: self, options: [], range: NSMakeRange(0, nsString.length))
return results.map { result in
(0..<result.numberOfRanges).map { result.rangeAt($0).location != NSNotFound
? nsString.substring(with: result.rangeAt($0))
: ""
}
@neoneye
neoneye / qemu_osx_rpi_raspbian_jessie.sh
Created June 19, 2017 20:59 — forked from hfreire/qemu_osx_rpi_raspbian_jessie.sh
How to emulate a Raspberry Pi (Raspbian Jessie) on Mac OSX (El Capitan)
# Install QEMU OSX port with ARM support
sudo port install qemu +target_arm
export QEMU=$(which qemu-system-arm)
# Dowload kernel and export location
curl -OL \
https://github.com/dhruvvyas90/qemu-rpi-kernel/blob/master/kernel-qemu-4.1.7-jessie
export RPI_KERNEL=./kernel-qemu-4.1.7-jessie
# Download filesystem and export location
@neoneye
neoneye / PostgresStORM+PickNextId.swift
Last active October 19, 2017 19:35
There can be gaps in the id column. This ensures that the id column is set to the max value
import PostgresStORM
extension PostgresStORM {
/// Set Next ID Value to MAX ID
///
/// This is a workaround for issue [ISS-546](http://jira.perfect.org:8080/browse/ISS-546)
///
/// When inserting new rows then I'm getting errors like this:
///
/// ERROR: duplicate key value violates unique constraint "president_key"
@neoneye
neoneye / PostgresStORM+DoWithTransaction.swift
Created October 21, 2017 12:08
Unsuccessful experiments getting transactions working with StORM
import PerfectPostgreSQL
import PostgresStORM
import StORM
extension PostgresStORM {
static func doWithTransaction<Result>(closure: () throws -> Result) throws -> Result {
let thisConnection = PostgresConnect(
host: PostgresConnector.host,
username: PostgresConnector.username,
password: PostgresConnector.password,
@neoneye
neoneye / guide.md
Last active November 20, 2022 14:26
Install GraphicsMagick on AWS EC2 running Ubuntu Linux
import XCTest
class URLRelativePathFromTests: XCTestCase {
func process(_ p1: String, _ p2: String) -> String? {
let u1 = URL(fileURLWithPath: p1)
let u2 = URL(fileURLWithPath: p2)
return u1.relativePath(from: u2)
}
func testNormal() {
@neoneye
neoneye / backup_database.rb
Created March 11, 2018 11:05
Backup PostgreSQL Database to AWS S3 Storage
#!/usr/bin/env ruby
require 'fileutils'
PATH_PG_DUMP = "/usr/lib/postgresql/9.6/bin/pg_dump"
POSTGRES_HOST = "demoscene.t1qwy5zz3sn3.eu-southwest-3.rds.amazonaws.com"
POSTGRES_PORT = 1234
POSTGRES_USERNAME = "admin"
POSTGRES_PASSWORD = "doesnt look like anything to me"
POSTGRES_DBNAME = "main"
AWS_S3_BUCKET = "there.is.no.spoon"
@neoneye
neoneye / Example.swift
Created March 18, 2018 20:23
Tweakable Values in swift
let value0 = _tv(100)
let value1 = _tv(200)
print("the values are: \(value0) \(value1)")
if _tv(1) > 0.5 {
print("if-case")
} else {
print("else-case")
}
@neoneye
neoneye / UIViewController+Child.swift
Created June 6, 2018 08:40
Install child view controller
import UIKit
extension UIViewController {
/// Taken from this great StackOverflow post by Phani Sai: https://stackoverflow.com/a/35473300/78336
func configureChildViewController(_ childController: UIViewController, onView: UIView?) {
var holderView = self.view
if let onView = onView {
holderView = onView
}
addChildViewController(childController)
@neoneye
neoneye / routes.swift
Last active June 6, 2018 22:17
self documenting api using Perfect
import PerfectHTTP
class HandlerContext {
let request: HTTPRequest
let response: HTTPResponse
init(request: HTTPRequest, response: HTTPResponse) {
self.request = request
self.response = response
}