Skip to content

Instantly share code, notes, and snippets.

View dbalatero's full-sized avatar

David Balatero dbalatero

View GitHub Profile
require 'fakeweb'
FakeWeb.allow_net_connect = false
req = Net::HTTP::Get.new('/index.html')
req.basic_auth("[email protected]", "password")
response = Net::HTTP.start('www.google.com', 80) do |http|
# current HEAD
require 'sinatra/base'
class MyApp < Sinatra::Base
get '/' do
'test'
end
end
app = Sinatra.new(MyApp) do
[:get, :post, :put, :delete].each do |method|
module_eval <<-SRC
def #{method.to_s}(url, options = {})
mock_object = get_mock(:#{method.to_s}, url, options)
unless mock_object.nil?
mock_object
else
enforce_allow_net_connect!(:#{method.to_s}, url, options[:params])
remote_proxy_object(url, :#{method.to_s}, options)
end
Given:
klass = Class.new { include Typhoeus }
# doesn't return params
irb(main):020:0> r = klass.put("http://localhost:3000/test/show_params", :params => { :email => '[email protected]', :password => 'testtest' })
=> #<Typhoeus::Response:0x548c88 @headers="HTTP/1.1 200 OK\r\nConnection: close\r\nDate: Mon, 17 Aug 2009 16:06:39 GMT\r\nETag: \"f4676806cf6b2a2d837de41360b366aa\"\r\nContent-Type: text/html; charset=utf-8\r\nX-Runtime: 1\r\nContent-Length: 47\r\nCache-Control: private, max-age=0, must-revalidate\r\n\r\n", @time=0.204065, @body="{\"action\"=>"show_params", "controller"=>"test"}", requested_http_method:put, code200, requested_url"http://localhost:3000/test/show_params"
# does return params
irb(main):021:0> r = klass.post("http://localhost:3000/test/show_params", :params => { :email => '[email protected]', :password => 'testtest' })
class BaseProxyObject
instance_methods.each { |m| undef_method m unless m =~ /^__|expects|respond_to/ }
def initialize(proxied_object)
@proxied_object = proxied_object
end
def respond_to?(method)
base_response = super(method)
if !base_response
class Problem
def initialize(id)
@id = id
end
def id
@id
end
# Bad implementation: if !other.is_a?(Problem), then it will raise
<% content_for(:page_title) { 'Events - Index' } -%>
<h1>Events</h1>
<p>Blah blah blah</p>
class EventsController < ActionController::Base
layout 'events' # uses the layouts/events.html.erb file instead of the default layouts/application.html.erb
# This will fire the setup_global_data function before calling any controller action in this class.
before_filter :setup_global_data
def index
end
def calendar
# Add to config/enviroment.rb when needed.
TYPHOEUS_LOG_FILE = RAILS_ROOT + "/log/typhoeus.log"
module Typhoeus
class_eval do
class << self
alias_method :old_perform_easy_requests, :perform_easy_requests
def perform_easy_requests
class AccountsController < ActionController::Base
def user_exists
account = Account.find(:first, :conditions => ['username = ?', params[:username]])
if account
@code = 204
else
@code = 404
end
render :nothing => true, :code => @code
end