Skip to content

Instantly share code, notes, and snippets.

View codenamev's full-sized avatar
🌩️

Valentino Stoll codenamev

🌩️
View GitHub Profile
@codenamev
codenamev / resource_steps.rb
Last active December 16, 2015 09:29
Cucumber step to look for a resource (link, image, etc.) on the page.
Then /^I should see the ([^"]*) "([^"]*)"$/ do |element_type, file_name|
case element_type
when /link/
page.should have_xpath("//link[contains(@href, '#{file_name}')]")
when /img/
page.should have_xpath("//img[contains(@src, '#{file_name}')]")
end
end
@codenamev
codenamev / attachment.rb
Created June 21, 2013 14:16
Custom file names with Carrierwave and Amazon S3
# model
class Attachment < ActiveRecord::Base
mount_uploader :document, DocumentUploader
# these methods fix the filename persistance issue
def read_uploader(column)
self[column]
end
def write_uploader(column, identifier)
@codenamev
codenamev / list_all_cron.sh
Created July 22, 2013 19:43
List all cron jobs for all users
#!/bin/bash
for user in $(cut -f1 -d: /etc/passwd); do echo $user; crontab -u $user -l; done
@codenamev
codenamev / proxify.sh
Created September 18, 2013 12:06
Creates an SSH tunnel and sets up a SOCKS proxy on your OSX machine
#!/bin/bash
# Include this file in your .zshrc or .bashrc, or just add this function directly to it
#
# Usage:
# > proxify
# # prompts you for your ssh password
# # OSX may prompt you to confirm the changes to your network settings
#
# Once complete, you will have an SSH tunnel established to your remote host
@codenamev
codenamev / array_sum.js.coffee
Created October 9, 2013 14:57
Ruby-style sum method for Array
# USAGE:
# given:
# <div class="price" data-amount="1">$1.00</div>
# <div class="price" data-amount="2">$2.00</div>
# > $('.price').get().sum (price) -> $(price).data('amount')
# > 3
Array::sum = (fn = (x) -> x) ->
@reduce ((a, b) -> a + fn b), 0
@codenamev
codenamev / ninja_toolbelt.rb
Created November 26, 2013 15:51
Ruby Ninja Toolbelt
# Array#flat_map
# works exactly like: Array.map(&b).flatten!(1)
[[1,2],[3,4]].flat_map {|i| i } #=> [1, 2, 3, 4]
@codenamev
codenamev / nearest_date.rb
Created December 3, 2013 21:20
Find the nearest Monday
# credit: http://stackoverflow.com/questions/4514988/rails-is-there-away-to-get-the-date-object-that-is-the-closest-monday-to-today
# docs: http://www.ruby-doc.org/stdlib-1.9.3/libdoc/date/rdoc/Date.html#method-c-commercial
# Find the next Monday
Date.commercial(Date.today.year, 1+Date.today.cweek, 1)
# Find the nearest Monday
Date.commercial(Date.today.year, Date.today.cwday.modulo(4)+Date.today.cweek, 1)
# Date of writing was: 2013-12-03
@codenamev
codenamev / string_extension.rb
Created December 12, 2013 17:51
Titleize a string with exceptions
# encoding: utf-8
class String
def titleize(options = {})
exclusions = options[:exclude]
return ActiveSupport::Inflector.titleize(self) unless exclusions.present?
self.underscore.humanize.gsub(/\b(?<!['’`])(?!#{exclusions.join('|')})[a-z]/) { $&.capitalize }
end
end
@codenamev
codenamev / page_object.rb
Last active October 26, 2016 12:30
Simple base class implementing "Page Objects" as referenced: http://gaslight.co/blog/6-ways-to-remove-pain-from-feature-testing-in-ruby-on-rails
class PageObject
include Capybara::DSL
include RSpec::Matchers
include Rails.application.routes.url_helpers
attr_accessor :path, :object
def initialize(object = nil)
raise "#{self.class} must be initialized with a model instance" unless !is_singular_object_page? or object
@path = "#{path_name}_path"
require 'open-uri'
require 'json'
language = 'en'
unless article = ARGV.shift
print 'What do you need to know? : '
article = URI::encode gets.chomp
end