Skip to content

Instantly share code, notes, and snippets.

View hlxwell's full-sized avatar
⛷️
Focusing

Michael He hlxwell

⛷️
Focusing
View GitHub Profile
extension String {
func base64Encoded() -> Data? {
return self.data(using: .utf8)
}
func base64Decoded() -> Data? {
if self.range(of: ":")?.lowerBound != nil {
return self.data(using: .utf8)
}
let base64String = self.replacingOccurrences(of: "-", with: "+").replacingOccurrences(of: "_", with: "/")
@hlxwell
hlxwell / day-spliting.rb
Last active November 28, 2015 12:56
According to duration, split spots into different days
# ActiveSupport has it.
class Array
def sum
if self.size == 0
return 0
else
self.inject(:+)
end
end
end
#!/bin/bash
# Interactive PoPToP install script on a OpenVZ VPS
# Tested on Debian 5, 6, and Ubuntu 11.04
# 2011 v1.1
# Author: Commander Waffles
# http://www.putdispenserhere.com/pptp-debian-ubuntu-openvz-setup-script/
echo "######################################################"
echo "Interactive PoPToP Install Script for OpenVZ VPS"
echo "by Commander Waffles http://www.putdispenserhere.com"
@hlxwell
hlxwell / storyboard.m
Created September 29, 2014 11:37
load Storyboard in code
it(@"should instantiate the view controller", ^{
UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
ViewController *vc = [mainStoryboard instantiateViewControllerWithIdentifier:@"ViewController"];
[[vc shouldNot] beNil]
});
@hlxwell
hlxwell / fabric.rb
Created September 25, 2014 02:19
fabric Test
from fabric.api import *
env.hosts = ["app1", "app2"]
env.use_ssh_config = True
def host_type():
run('df -h')
@hlxwell
hlxwell / animal.py
Created September 25, 2014 02:18
Python test
class Animal(object):
def __init__(self):
self.name = "Animal"
def eat(self, food = "nothing"):
print self.name + " eats " + food
# ==================================================
import animal
@hlxwell
hlxwell / test_wifi.rb
Last active August 29, 2015 14:06
wifi testing
prev_time = Time.now
100000.times do
request = WifiRequest.where(client_mac_addr: "B0:8E:1A:50:03:ED").last
next if request.request_at == prev_time
puts request.slice(:client_mac_addr, :power, :request_at)
prev_time = request.request_at
sleep 5
end
@hlxwell
hlxwell / gist:4d68db4c628537ceaefd
Created September 1, 2014 16:08
routes.rb level permission control
# Method 1 =============================================
class ProjectManagerChecker
def self.matches?(request)
request.env['warden'].user.role == 'project_manager'
end
end
# routes.rb
get '/' => 'project_managers#index', :constraints => ProjectManagerChecker
@hlxwell
hlxwell / kana_methods.rb
Created August 19, 2014 05:13
Judge if string is Kana or not.
class String
def is_katakana?
(self =~ /^[゠-ヿ -〿]+$/) == 0
end
def is_half_katakana?
(self =~ /^[⦅-゚ -〿]+$/) == 0
end
@hlxwell
hlxwell / a.js
Created July 17, 2014 04:24
create a promise
function longRunJob() {
var deferred = $q.defer();
setTimeout(function(){
deferred.notify('About to greet ' + name + '.');
deferred.resolve('Hello, ' + name + '!');
deferred.reject('Greeting ' + name + ' is not allowed.');
}, 10000)
return deferred.promise;
}