Skip to content

Instantly share code, notes, and snippets.

@cupakromer
cupakromer / gist:8201917
Last active January 1, 2016 21:09
Radius iBeacon offline mode minor decoder
def decode_minor(minor)
day = minor >> 11
hour = (minor >> 6) & 0b11111
min = minor & 0b111111
"#{day} #{hour}:#{min} UTC"
end
@cupakromer
cupakromer / gist:7893829
Last active December 30, 2015 22:19
Hiring a Dev Outline

Starting the conversation

  • Bad Idea: Just meet and pitch for 20 min
  • Good Idea: Get involved in community, hang out more

  • Bad Idea: Not knowing there are different prog languages
  • Good Idea: Do research, ask opinions, leave it to dev choice
class Article < ActiveRecord::Base
def self.states(*states)
states.each do |state|
define_method "#{state}?" do
self.state == state
end
define_singleton_method "all_#{state}" do
where("state = ?", state)
@cupakromer
cupakromer / gist:7550244
Created November 19, 2013 18:39
RefactorMelee : Arg this is terrible code, halp!
# The idea here is that if any of the three tokens are not already
# set, then they should be generated. In order to generate one, it
# must be unique.
def generate_tokens!
[ :code, :access_token, :refresh_token ].each do |attr|
next if send(attr)
token = loop do
t = SecureRandom.hex(16)
break t unless self.class.exists?(attr => t)
@cupakromer
cupakromer / .env.development
Created November 14, 2013 22:44
Dynamic database change
# Check this into the features that have different schemas as you develop
# Before you merge to master you either comment this out or remove the file
export DATABASE_NAME=cool_story_bro
class LineItem
attr_reader :pairs
protected :pairs
def initialize(data)
@pairs = Hash[ data.each_pair.map{ |k, v| [k.downcase.to_sym, v] } ]
normalize_amount!
raise ArgumentError.new("Missing a context field") unless @pairs.count > 1
@cupakromer
cupakromer / gist:7419680
Created November 11, 2013 20:19
tmp tbd
def load_xml(input)
File.open(input) do |f|
Nokogiri::XML(f).search('rate').map(&:text).map(&:split)
end
end
@cupakromer
cupakromer / autorun
Last active December 27, 2015 05:19
For RPis
# Cron script
#
# Auto run file to run the bootstrap script from a USB stick. This file should be deleted after basilisk is installed.
#
SHELL=/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
# Assuming device names for raspberry pi
@reboot root mount /dev/sda1 /media && cd /media && ./configure-beacon.sh
@cupakromer
cupakromer / gist:7268464
Created November 1, 2013 17:03
Remove Trailing whitespace in vim
function! <SID>StripTrailingWhitespaces()
" Prep: save last search, and cursor position.
let _s=@/
let l = line(".")
let c = col(".")
" Remove whitespace
%s/\s\+$//e
" Restore search and cursor position
let @/=_s
call cursor(l, c)
@cupakromer
cupakromer / gist:7120458
Last active December 26, 2015 08:09
Controller test with shared example
require 'spec_helper'
describe DashboardController do
include Factories::Godzilla
shared_examples "successfully gets the session cards" do
it "is successful" do
get :session_cards