spec/at_spec.rb in at-0.1.2 vs spec/at_spec.rb in at-0.1.3

- old
+ new

@@ -16,10 +16,12 @@ subject.configuration.should == "the result" end end end +#===-----------------------------------------------------------------------===# + class User def initialize(first_name=nil, last_name=nil) @first_name, @last_name = first_name, last_name end @@ -57,5 +59,47 @@ it "should raise a NoMethodError" do Proc.new { subject.last_name }.should raise_error(NoMethodError) end end end + +#===-----------------------------------------------------------------------===# + +require 'net/http' +require 'active_support/core_ext/object/to_query' + +module HtmlDownloader + def base_uri(_base_uri) + @base_uri = _base_uri.gsub(/\/$/, '').gsub(/https?:\/\//, '') + end + + def get(path, params={}) + path.gsub!(/^\//, '') + full_path = params == {} ? "/#{path}" : "/#{path}?#{params.to_param}" + Net::HTTP.get_print(@base_uri, full_path) + end +end + +class Twitter + extend HtmlDownloader + + base_uri 'http://search.twitter.com' + + def self.search(params={}) + @cache ||= Hash.new { |h, k| h[k] = get('/search.json', params) } + @cache[params] + end +end + +describe Twitter do + describe "#search" do + it "should not make an HTTP request for the same params twice" do + Twitter.at.cache = { + { :q => 'test' } => "{'some':'json response'}" + } + + Net::HTTP.should_receive(:get_print).never + + Twitter.search(:q => "test").should == "{'some':'json response'}" + end + end +end \ No newline at end of file