Skip to content

Instantly share code, notes, and snippets.

View awesome's full-sized avatar

So Awesome Man awesome

View GitHub Profile
# http://stackoverflow.com/questions/95554/overriding-a-mime-type-in-rails
render :json => var_containing_my_json, :content_type => 'text/x-json'
irb(main):016:0> res = Net::HTTP.post_form(url, {:hello => "world"})
=> #<Net::HTTPOK 200 OK readbody=true>
irb(main):024:0> pp (res.methods - Object.methods).sort
["[]",
"[]=",
"add_field",
"basic_auth",
"body",
"canonical_each",
"chunked?",
request.env['RAW_POST_DATA']
@awesome
awesome / strip HTML tags
Created November 3, 2009 16:29
ruby strip HTML tags
# copied from (thanks!): http://codesnippets.joyent.com/posts/show/615
str = <<HTML_TEXT
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<body>
<h1>Application error</h1>
<p>Change this error message for exceptions thrown outside of an action (like
in Dispatcher setups or broken Ruby code) in public/500.html</p>
# copied from (thanks!): http://codesnippets.joyent.com/posts/show/615
str = <<HTML_TEXT
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<body>
<h1>Application error</h1>
<p>Change this error message for exceptions thrown outside of an action (like
in Dispatcher setups or broken Ruby code) in public/500.html</p>
@awesome
awesome / uri methods
Created December 28, 2009 03:52
addressable/uri methods
awesomeville:~ soawesomeman$ irb
irb(main):001:0> require 'rubygems'
=> true
irb(main):002:0> require 'addressable/uri'
=> true
irb(main):003:0> require 'pp'
=> true
irb(main):004:0> beer = Addressable::URI.new
=> #<Addressable::URI:0xc37abc URI:>
irb(main):005:0> pp (beer.methods - Object.methods).sort
@awesome
awesome / cucumber.yml
Created January 5, 2010 07:31
turn on color for output; cucumber (0.5.3), cucumber-rails (0.2.2)
<%
rerun = File.file?('rerun.txt') ? IO.read('rerun.txt') : ""
rerun_opts = rerun.to_s.strip.empty? ? "--format progress features" : "--format #{ENV['CUCUMBER_FORMAT'] || 'pretty'} #{rerun}"
std_opts = "#{rerun_opts} --format rerun --out rerun.txt --strict --tags ~@wip"
%>
# ORIGINAL:
#default: <%= std_opts %>
#wip: --tags @wip:3 --wip features
# PATCH:
default: <%= std_opts %> --color
@awesome
awesome / opposite_of_array_intersection.rb
Created January 21, 2010 17:44
Ruby opposite of array intersection
# Ruby opposite of array intersection... or maybe the method is missing from my brain bc not enough coffee
# http://twitter.com/soawesomeman/status/8035087261
def awesome(ar_1, ar_2)
(ar_1 + ar_2) - (ar_1 & ar_2)
end
awesome([1,2,3,4], [3,4,5,6]) # => [1, 2, 5, 6]
ActionController::DoubleRenderError (Render and/or redirect were called multiple times in this action. Please note that you may only call render OR redirect, and at most once per action. Also note that neither redirect nor render terminate execution of the action, so if you want to exit an action after redirecting, you need to do something like "redirect_to(...) and return".):
@awesome
awesome / webrat_via_cucumber_request_patch.rb
Created February 8, 2010 17:06
webrat via cucumber: missing query-string params in your controller?
#
# I heard you like patches in yo controller dawg...
#
# DIRTY PATCH for
# def get,post,put,delete
# http://github.com/brynary/webrat/blob/38535d1e0ec3dab91b4c516dfd4c14ac7f6f81e4/lib/webrat/rails.rb
#
#
#
before_filter :test_helper