Skip to content

Instantly share code, notes, and snippets.

View Jamedjo's full-sized avatar

James EdJo Jamedjo

View GitHub Profile
@Jamedjo
Jamedjo / sign_in_helpers.rb
Created January 31, 2014 00:47
Capybara sign in/up helpers for use with Cucumber. Uses FactoryGirl to get sign up attributes, and to create a user for signing in.
module SignInHelpers
def sign_up_as(user_type, form_params={})
attributes = FactoryGirl.attributes_for(user_type).merge(form_params)
destory_user(attributes[:email])
visit '/users/sign_up'
within("#new_user") do
fill_in "Name", :with => attributes[:name]
fill_in "Email", :with => attributes[:email]
fill_in "Password", :with => attributes[:password]
# http://www.codewars.com/kata/batchitemprocessor/ruby
require './batchitemprocessor'
class Test
# This class mimics the one provided in the kata
def self.assert_equals(actual, expected, message="")
if actual!=expected
puts "\nTest '#{message}' failed.\nExpected #{expected}\nGot #{actual}"
else
puts "."
@Jamedjo
Jamedjo / undo.hs
Created November 21, 2014 19:57
Standalone version of lambdabot's undo function.
-- Copyright (c) 2006 Spencer Janssen
-- GPL version 2 or later (see http://www.gnu.org/copyleft/gpl.html)
{-# LANGUAGE Rank2Types #-}
module Main where
import Control.Monad
import Data.Generics
@Jamedjo
Jamedjo / sprites.rake
Created February 1, 2015 22:29
Resize images and move from ./originals to ./
namespace :sprites do
originals = Rake::FileList.new('app/assets/images/**/originals/*.png')
resized = originals.pathmap("%{/originals$,/}d%f")
task resize: resized
end
from_originals = ->(tn) {
tn.pathmap("%d/originals/%f")
}
@Jamedjo
Jamedjo / strikingly_email_cookie.html
Last active August 29, 2015 14:17
Store Strikingly email in a cookie
@Jamedjo
Jamedjo / auth_helper.rb
Last active August 29, 2015 14:19
Rspec helper for http basic auth which works across feature, controller and request specs.
module AuthHelper
module Feature
def basic_auth(login, password)
if page.driver.respond_to?(:basic_auth)
page.driver.basic_auth(login, password)
elsif page.driver.respond_to?(:basic_authorize)
page.driver.basic_authorize(login, password)
elsif page.driver.respond_to?(:browser) && page.driver.browser.respond_to?(:basic_authorize)
page.driver.browser.basic_authorize(login, password)
else
# lib/tasks/db.rake
namespace :db do
desc "Dumps the database to db/APP_NAME.dump"
task :dump => :environment do
cmd = nil
epoch = Time.now.to_i
with_config do |app, host, db, user|
cmd = "pg_dump --verbose #{pg_login_config(host, user)} --clean --format=c #{db} > #{Rails.root}/db/#{app}-#{epoch}.dump"
end
puts cmd
@Jamedjo
Jamedjo / strikingly_enter_submit.js
Created June 26, 2015 00:56
Strikingly enter submit fix
function addEvent(element, eventName, fn) {
if (element.addEventListener)
element.addEventListener(eventName, fn, false);
else if (element.attachEvent)
element.attachEvent('on' + eventName, fn);
}
var wasEnterPressed = function(e){
return e.which == 13 || e.keyCode == 13;
};
@Jamedjo
Jamedjo / csv_to_sendwithus.py
Last active August 29, 2015 14:24
Script to create sendwithus customers from csv with data
def batch(iterable, n=1):
l = len(iterable)
for ndx in range(0, l, n):
yield iterable[ndx:min(ndx+n, l)]
import sendwithus
import csv
max_count = 100 # keep this as or around 100 for batch requests
csv_location = './customers.csv'
require 'csv'
ActionController::Renderers.add :csv do |collection, options|
self.content_type ||= Mime::CSV
self.headers['Content-Disposition'] = "attachment; filename=#{options[:filename]}.csv" if options[:filename]
self.response_body = collection.to_csv
end
module CsvRenderer
def to_csv(options={})