Skip to content

Instantly share code, notes, and snippets.

View Jamedjo's full-sized avatar

James EdJo Jamedjo

View GitHub Profile
RSpec::Matchers.define :exceed_query_limit do |expected|
supports_block_expectations
match do |block|
query_count(&block) > expected
end
failure_message_when_negated do |actual|
"Expected a maximum of #{expected} queries, got #{@recorder.count}:\n\n#{@recorder.log_message}"
end
@Jamedjo
Jamedjo / Gemfile
Created June 16, 2013 15:09
Sinatra with RSpec config,
source 'https://rubygems.org'
ruby '2.0.0'
gem 'sinatra'
gem 'rspec'
gem 'rack-test'
@Jamedjo
Jamedjo / scrape.rb
Created July 26, 2013 16:04
Scrape top google results for title, meta-description and keywords. Usage: `ruby scrape.rb your search terms go here`
#!/usr/bin/ruby
require 'nokogiri'
require 'open-uri'
#require 'debugger'
require 'cgi'
url = "http://www.google.co.uk/search?q=#{ARGV.join('+')}"
puts url
doc = Nokogiri::HTML(open(url))
# debugger
@Jamedjo
Jamedjo / poltergeist_network_monitor.rb
Last active April 26, 2017 14:25
Tool for detecting slow requests when using Capybara with Poltergeist
class PoltergeistNetworkMonitor
require 'pstore'
require 'uri'
require 'fileutils'
def initialize(page)
@page = page
end
def print_requests(longer_than_seconds:)
@Jamedjo
Jamedjo / capybara_hooks.rb
Created April 13, 2017 10:34
Capybara after_page_change hook
module CapybaraHooks
def after_page_change
end
Capybara::Session::DSL_METHODS.each do |method|
define_method method do |*args, &block|
old_page = page.current_url
super(*args, &block)
new_page = page.current_url
@Jamedjo
Jamedjo / ga-youtube.js
Last active February 8, 2017 20:43
Google analytics youtube script, with fixes. Standard embed code uses `//` instead of `http://`, so the regex needed updating.
//
//To Track Thy Youtube Upon Google Analytics
//Regardless the number of Players upon thy stage
//Revised and Revisioned to Version 2.1
//Within the March of Two Thousand and Thirteen
//
//Performed by LunaMetrics http://www.lunametrics.com @lunametrics
//and Sayf Sharif @sayfsharif
//
//Who beg thy forgiveness for the lack of the regular expression
class VntReader
def initialize(file)
@file=file
end
def body
@file.each_line.select{|l|l.match /\ABODY/}.first.chomp
end
def content

Like content_for but takes a default output from a block if content_for? hasn't been set.

= content_default(:sidebar) do
  = render 'sidebar'
@Jamedjo
Jamedjo / angular_linkto.md
Last active December 31, 2015 20:49
Angularjs compatible link_to. Adds target="_self" to all links created with link_to, so $location doesn't hijack the link. Useful if an external gem/engine is using link_to.

Angularjs link_to

Angular compatible link_to. Turns

<%= link_to "Sign up", "/users/sign_up" %>

into

Sign up

@Jamedjo
Jamedjo / Subdomain.md
Last active December 28, 2015 01:09
Super basic rails subdomain library.

Simple Subdomain

Super simple library for using subdomains in your rails routes.

Lets you do

subdomain('online') do
  root :to => 'online#index'
end

in your routes.rb.