Skip to content

Instantly share code, notes, and snippets.

View samwgoldman's full-sized avatar

Sam Goldman samwgoldman

View GitHub Profile
@samwgoldman
samwgoldman / test.rb
Created April 27, 2012 21:13
webmachine-ruby chunked response with rack adapter streaming
require "webmachine"
require "webmachine/adapters/rack"
class TestResource < Webmachine::Resource
def to_html
parts = %w{Hello, World!}
Fiber.new do
parts.each do |part|
Fiber.yield part
sleep 0.5
@samwgoldman
samwgoldman / html_builder_example.rb
Created May 23, 2012 17:38
Using smartlogic/html_builder
require "html"
require "html/list"
require "html/map"
module HTML
class Article < Struct.new(:article)
def to_html
HTML.build(article) do |article|
tag(:article, tag(:h2, article.title) + map(:section, article.sections), id: article.id)
end
@samwgoldman
samwgoldman / facebook_test_user.rb
Created June 20, 2012 13:38
Easily create test users for facebook
require "httparty"
class FacebookTestUser
include HTTParty
base_uri "graph.facebook.com:443"
attr_reader :email, :password
def initialize(options)
@email = options["email"]
@samwgoldman
samwgoldman / capybara_reset_spec.rb
Created June 27, 2012 18:09
Capybara session reset issue
require "rack"
require "capybara/rspec"
App = Rack::Builder.new do
map "/" do
run proc { |env|
request = Rack::Request.new(env)
response = Rack::Response.new
if request.cookies["logged_in"]
response.write "Logged in!"
@samwgoldman
samwgoldman / backtrace.txt
Created September 14, 2012 21:58
IO.popen on jruby issue?
1) HTTPSpec::Clients::CGI issues requests to the app
69 Failure/Error: Unable to find matching line from backtrace
70 Java::JavaLang::NullPointerException:
71 # org.jruby.util.ShellLauncher.getCurrentEnv(ShellLauncher.java:235)
72 # org.jruby.util.ShellLauncher.popenShared(ShellLauncher.java:712)
73 # org.jruby.util.ShellLauncher.popenShared(ShellLauncher.java:689)
74 # org.jruby.util.ShellLauncher.popen(ShellLauncher.java:673)
75 # org.jruby.RubyIO.popen19(RubyIO.java:3722)
76 # org.jruby.RubyIO$s$0$1$popen19.call(RubyIO$s$0$1$popen19.gen:65535)
77 # org.jruby.internal.runtime.methods.DynamicMethod.call(DynamicMethod.java:219)
var http = require("http");
var port = process.env.PORT || 8000;
var listeners = [];
function server(request, response) {
switch (request.method) {
case "GET":
listen(request, response);
@samwgoldman
samwgoldman / gist:3831925
Created October 4, 2012 07:03
gary bernhardt challenge
x = "some file name.with dots.html.markdown"
x.split(".").reverse.inject([x, []]) do |(file, exts), part|
if part =~ /\s/ || part == file
break [file, exts]
else
[file.chomp(".#{part}"), exts.push(part)]
end
end
@samwgoldman
samwgoldman / implementation.js.coffee
Created October 16, 2012 23:08
Async testing pattern. Better way?
class App.Components.Select extends Backbone.View
template: HandlebarsTemplates.select
initialize: ->
@labelField = @options.labelField
@valueField = @options.valueField
@collection.bind("reset", => @render)
render: ->
@samwgoldman
samwgoldman / gist:3904149
Created October 17, 2012 07:08
How to use VCR even if your tests depend on Date.today/Time.now, +/- 100 days/seconds.
VCR.configure do |config|
config.cassette_library_dir = "spec/cassettes"
config.hook_into :faraday
# If a test uses a value that depends on the current time, then the recording
# become stale as time marches on.
# This setting will replace specific "absolute time" values with time-relative
# keywords when the recording is first made.
# On subsequent replayes, the time-relative keywords are substituted with
# the appropriate value, so the value is relative tot he current time.
@samwgoldman
samwgoldman / coffee-script-redux.js
Created November 3, 2012 03:29
Browser-compatible single-file CoffeeScriptRedux
This file has been truncated, but you can view the full file.
(function(){var require = function (file, cwd) {
var resolved = require.resolve(file, cwd || '/');
var mod = require.modules[resolved];
if (!mod) throw new Error(
'Failed to resolve module ' + file + ', tried ' + resolved
);
var cached = require.cache[resolved];
var res = cached? cached.exports : mod();
return res;
};