Skip to content

Instantly share code, notes, and snippets.

View kcurtin's full-sized avatar

Kevin Curtin kcurtin

View GitHub Profile
@kcurtin
kcurtin / ex_unite_case_template.ex
Last active February 26, 2016 16:39
Using case templates to setup your database for integration tests with elixir and ecto.
defmodule DBTransactions do
use ExUnit.CaseTemplate
setup_all do
Ecto.Adapters.SQL.begin_test_transaction(Repo)
on_exit fn ->
Ecto.Adapters.SQL.rollback_test_transaction(Repo)
end
end
@kcurtin
kcurtin / weather_parser.ex
Created February 14, 2016 16:34
Interfacing with the erlang xmerl library to parse xml data. Using xml files from weather.gov for sample data (http://w1.weather.gov/xml/current_obs/)
defmodule WeatherParser do
require Record
Record.defrecord :xmlElement, Record.extract(:xmlElement, from_lib: "xmerl/include/xmerl.hrl")
Record.defrecord :xmlText, Record.extract(:xmlText, from_lib: "xmerl/include/xmerl.hrl")
def parse(xml, attrs) do
xml
|> :binary.bin_to_list
|> :xmerl_scan.string
|> extract_values(attrs, [])
@kcurtin
kcurtin / fizzbuzz.ex
Last active December 26, 2015 15:53
Fizzbuzz in elixir using pattern matching
Enum.each 1..100, fn (n) ->
fizzbuzz = fn
(0, 0, _) -> "fizzbuzz"
(0, _, _) -> "fizz"
(_, 0, _) -> "buzz"
(_, _, n) -> n
end
IO.puts fizzbuzz.(rem(n, 3), rem(n, 5), n)
end
@kcurtin
kcurtin / .travis.yml
Created December 16, 2014 20:29
Travis
before_script:
- ./script/travis/setup_db.sh
script: bundle exec rspec spec/ --tag $TEST_TAG
env:
matrix:
- TEST_TAG=js DB_TEST=1
- TEST_TAG=~js DB_TEST=2
@kcurtin
kcurtin / psql update
Last active August 29, 2015 14:06
Upgrade postgres to 9.3 from 9.2.8
# Had some issues, so referenced these 3 solutions:
# https://gist.github.com/cjolly/2870054
# https://gist.github.com/cloud8421/6667898
# http://stackoverflow.com/questions/20754669/how-to-fix-postgres-after-updating-upgrading-brew
launchctl unload ~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist
pg_dumpall > ninedottwo-dump
# Not sure if this is necessary, but has to be cleared out no matter what for when we run initdb
@kcurtin
kcurtin / date_range.rb
Created June 14, 2013 14:04
Finally found a good way to set up default values for a Struct..
require 'date'
DateRange = Struct.new(:start_date, :end_date) do
def start_date
self[:start_date] || Date.today - 30
end
def end_date
self[:end_date] || Date.today
end
@kcurtin
kcurtin / rspec_macro.vim
Created May 22, 2013 18:40
For anyone working with a test suite that uses the original (soon to be deprecated?) "should" syntax for rspec, this is a nice little macro that will convert shoulds to expects
" Example:
" response.body.should have_content("something")
" expect(response.body).to have_content("something")
" =================================================
" Macro that converts rspec "should"s to "expect"s
" =================================================
function! ConvertToExpect()
:normal! dd
:normal! P
@kcurtin
kcurtin / test_spec.rb
Created April 18, 2013 19:24
Test to ensure there are text versions of all your HTML email templates
describe UserMailer do
it "there is a corresponding text email template for each HTML template" do
# Path to the mailer templates
mailers_path = "#{Rails.root}/app/views/user_mailer/"
result = Dir.foreach(mailers_path) do |template|
# Ignore partials
next if template.match /^_/
# Check if it is an HTML template
@kcurtin
kcurtin / what.rb
Created November 15, 2012 03:15
WTF?
Tweet.where('created_at > ?','2012-11-15 02:18:28')
Tweet Load (0.5ms) SELECT "tweets".* FROM "tweets" WHERE (created_at > '2012-11-15 02:18:28') ORDER BY published_at DESC
=> [#<Tweet id: 30, content: "RARARA", published_at: "2012-11-15 02:18:28", handle: nil, created_at: "2012-11-15 02:18:28", updated_at: "2012-11-15 02:18:28", person_id: 1>]
#Aren't the created_at dates equal? So the query should return false...
'2012-11-15 02:18:28'.to_datetime.in_time_zone('UTC')
@kcurtin
kcurtin / answer.rb
Created November 4, 2012 16:25
sexy string conversion
original = 'g fmnc wms bgblr rpylqjyrc gr zw fylb. rfyrq ufyr amknsrcpq ypc dmp. bmgle gr gl zw fylb gq glcddgagclr ylb rfyr'q ufw rfgq rcvr gq qm jmle. sqgle qrpgle.kyicrpylq() gq pcamkkclbcb. lmu ynnjw ml rfc spj.'
original.each_char.each_with_object("") do |character, final|
character.next!.next! unless character.match /\W/
final << character
end