Skip to content

Instantly share code, notes, and snippets.

# Application
class App < Sinatra::Base
set :hi, "Hello"
configure :test do
set :hi, "Hello There!"
end
end
# Registering a extention
# Author: Pieter Noordhuis
# Description: Simple demo to showcase Redis PubSub with EventMachine
#
# Update 7 Oct 2010:
# - This example does *not* appear to work with Chrome >=6.0. Apparently,
# the WebSocket protocol implementation in the cramp gem does not work
# well with Chrome's (newer) WebSocket implementation.
#
# Requirements:
# - rubygems: eventmachine, thin, cramp, sinatra, yajl-ruby
@interface UIImage (Shadow)
+ (UIImage *)imageWithContentsOfFile:(NSString *)path withShadow:(BOOL)applyShadow;
+ (UIImage *)imageNamed:(NSString *)name withShadow:(BOOL)applyShadow;
+ (UIImage *)applyShadow:(UIImage *)theImage;
@end
require "time"
require "date"
class Date
def to_time
Time.local(year, month, day)
end
end
class Time
@juliocesar
juliocesar / testing_front_end_rspec_capybara.md
Created October 21, 2010 23:51
Testing front-end for a Sinatra app with RSpec and Capybara

Testing front-end for a Sinatra app with RSpec and Capybara

I've used Cucumber quite a bit on my last job. It's an excellent tool, and I believe readable tests are the way to the future. But I could never get around to write effective scenarios, or maintain the boatload of text that the suite becomes once you get to a point where you have decent coverage. On top of that, it didn't seem to take much for the suite to become really slow as tests were added.

A while ago I've seen a gist by Lachie Cox where he shows how to use RSpec and Capybara to do front-end tests. That sounded perfect for me. I love RSpec, I can write my own matchers when I need them with little code, and it reads damn nicely.

So for my Rails Rumble 2010 project, as usual, I rolled a Sinatra app and figured I should give the idea a shot. Below are my findings.

Gemfile

require 'rubygems'
require 'superfeedr-rb' # [sudo] gem install
require 'pp'
# Blather.logger.level = Logger::DEBUG # increase log level to see XMPP traffic
# Please make sure you use your own superfeedr credentials (http://superfeedr.com/subscriber) as this account is wiped out regularly.
Superfeedr::Client.connect('xxx@superfeedr.com', 'yyy') do |client|
# Susbcribes to the feed :
client.feed('http://superfeedr.com/track/music') do |status, entries|
#! /usr/bin/env ruby
class AdditionProblemGenerator
attr_accessor :max, :number_of_problems
def initialize(max = 20, number_of_problems = 20)
self.max = max
self.number_of_problems = number_of_problems
end
@h3h
h3h / hash.rb
Created March 10, 2011 00:09
Hash#&
class Hash
# Intersection on hash keys (and values) that actually returns a Hash.
# The RHS can be an Array or a Hash.
#
# == Examples:
# >> hsh = {:banana => "elephant", :bar => "foo", :baz => "wuux"}
# >> hsh & [:bar]
# => {:bar=>"foo"}
#
# >> hsh & {:bar => "foo", :baz => "wuux"}
@stammy
stammy / application_helper.rb
Created April 26, 2011 08:42
rails 3 pretty page titles. no more instance vars all up in your controllers
module ApplicationHelper
# pretty page titles
# yield_for(:title, 'default text goes here') in layout
# content_for(:title, 'some text') in view
def yield_for(content_sym, default)
output = content_for(content_sym)
output = default if output.blank?
output
end
end
@mrflip
mrflip / fun_with_blocks.rb
Created April 27, 2011 01:15
Fun with blocks -- a couple things you might be glad to find out Ruby can do
#
# When you're using inject with a hash or somesuch, you can subgroup the source's elements:
#
# v--- the parens part here
{ 1 => 2, 3 => 4, :your => :mom }.inject({}) do |h, (k,v)|
h[k] = v**2 if v.is_a?(Numeric)
h
end
# => {1=>4, 3=>16}