Skip to content

Instantly share code, notes, and snippets.

# caching constraint
module Blog
Routes = Raptor.routes(Blog) do
show :to => 'Post.find', :unless :post_cached
show :responder => NoContentResponder
end
class PostCached
def match?(id, cache_control)
post = Post.find(id)
module Blog
Routes = Raptor.routes(Blog) do
show :to => 'Post.find', :http_cache_requirement => :post_cached
end
class PostCached
def match?(id, cache_control)
post = Post.find(id)
post.updated_at =< cache_control
end
module Blog
Routes = Raptor.routes(Blog) do
show :to => 'Post.find', :if => :post_not_cached
end
class PostNotCached
def match?(id, cache_control)
post = Post.find(id)
post.updated_at =< cache_control
end
(unfinished bar)
(defn foo [stuff incoming]
(first
(filter
(fn [a-thing]
(bar a-thing incoming))
stuff)))
;; this actually calls bar
module Kernel
def an(value)
Thingy.new(value)
end
def none
Nothing.new
end
end
# with dependency injection, and slightly different route syntax:
module WebCrawler
Routes = Raptor::App.new(self) do
queue = #... some sort of persistent queue initialized here
crawler = WebCrawler.new(queue)
path "to-be-crawled" do
create crawler, :enqueue!, :require => :logged_in
end
end
Note, the "don't stub external APIs" is a rule, but like all design/testing rules it *should* be broken with good judgement. Furthermore, like all design/testing rules, the best way to learn when to break it is to follow it rigorously until it leads you to a bad place.
Problems
--------
- Stubbing external APIs gives you no design feedback: you cannot change the third party API (unless you monkey patch, but seriously, Fuck That Shit. Since stubbing/mocking are primarily a design tool (for me), having to stub/mock something I cannot/will not change seems pointless.
- Stubbing a third party API relies on you having intimate understanding of the docs, including error conditions, timeouts etc. I'm not confident in my ability to successfully stub a third party API on each conditions.
The Solution
------------
1.9.3p125 :001 > class Foo < RuntimeError; def to_str; "other_string"; end; end
=> nil
1.9.3p125 :002 > begin raise Foo.new; rescue Foo => e; end
RuntimeError: other_string
from (irb):2
from /Users/tcrayford/.rvm/rubies/ruby-1.9.3-p125/bin/irb:16:in `<main>'
specify "the first time rspec has made me sad" do
class CustomError < RuntimeError; end
expect do
raise CustomError.new('what')
end.to raise(CustomError.new('what'))
end
specify "the first time rspec has made me sad" do
class CustomError < RuntimeError; end
expect do
raise CustomError.new('what')
end.to raise_error(CustomError.new('what'))
end