<?xml version="1.0" encoding="UTF-8"?>
<commit>
  <added type="array"/>
  <modified type="array">
    <modified>
      <diff>@@ -18,7 +18,12 @@ Documentation: http://github.com/eshopworks/rboss-gem/wikis/home
 
 == AUTHOR
 
-&quot;Joseph Wilk&quot;:http://blog.josephwilk.net/
+Joseph Wilk - http://blog.josephwilk.net/
+
+Contributors:
+  * Matthew Bennett
+  * Jeroen van Dijk
+  * Jared Pace
 
 == LICENSE:
 
@@ -43,4 +48,4 @@ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
 CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
 TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
-SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
\ No newline at end of file
+SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.</diff>
      <filename>README.txt</filename>
    </modified>
    <modified>
      <diff>@@ -42,42 +42,62 @@ module Boss
     end
 
     def next_page
-      make_request(@basepoint + @next_page_path) if @next_page_path
+      send_request(@basepoint + @next_page_path) if @next_page_path
     end
 
     def previous_page
-      make_request(@basepoint + @previous_page_path) if @previous_page_path
+      send_request(@basepoint + @previous_page_path) if @previous_page_path
     end
 
     private
-    def search_boss(terms, search_type=SearchService::WEB, config = {})
+    def search_boss(terms, search_type=SearchService::WEB, config = {}, &amp;block)
+      config = setup_config(config, &amp;block)
+      
+      if config.limit
+        build_and_send_requests_with_limits(terms, search_type, config)
+      else
+        build_and_send_request(terms, search_type, config)
+      end
+    end
+  
+    def setup_config(config)
       config = config.empty? ? Config.new : Config.new(config)
       yield config if block_given?
 
-      raise InvalidFormat, &quot;'#{config.format}' is not a valid format. Valid formats are: #{FORMATS.join(',')}&quot; unless FORMATS.include?(config.format) || config.format?
-      raise InvalidConfig, &quot;count must be &gt; 0&quot; unless config.count&gt;0
-      raise InvalidConfig, &quot;App ID cannot be empty!&quot; if @app_id.empty?
+      validate(config)
       
       config.count = MAX_COUNT if config.count &gt; MAX_COUNT
       
       # Remember search instructions for iterating over pages
       @current_config = config.dup
-      
-      if limit = config.delete_field(:limit)
-        # Do a first request to gather required information
-        results = make_request(build_request_url(terms, search_type, config) )
-        total_count = results.totalhits.to_i
-        limit = total_count if total_count &lt; limit
-        number_of_requests = (limit.to_f / config.count).ceil - 1
-
-        number_of_requests.times { results += next_page }
-        results
-      else
-        results = make_request(build_request_url(terms, search_type, config))
-      end
+      config
+    end
+    
+    def build_and_send_requests_with_limits(terms, search_type, config)
+      limit = config.delete_field(:limit)
+      # Do a first request to gather required information
+      results = build_and_send_request(terms, search_type, config)
+      remaining_requests = number_of_requests_to_reach_limit(results.totalhits.to_i, limit, config.count)
+      remaining_requests.times { results += next_page }
+      results
+    end
+        
+    def validate(config)
+      raise InvalidFormat, &quot;'#{config.format}' is not a valid format. Valid formats are: #{FORMATS.join(',')}&quot; unless FORMATS.include?(config.format) || config.format?
+      raise InvalidConfig, &quot;count must be &gt; 0&quot; unless config.count&gt;0
+      raise InvalidConfig, &quot;App ID cannot be empty!&quot; if @app_id.empty?
     end
     
-    def make_request(url)
+    def number_of_requests_to_reach_limit(total_results, limit, count)
+      limit = total_results if total_results &lt; limit
+      (limit.to_f / count).ceil - 1
+    end
+
+    def build_and_send_request(terms, search_type, config)
+      send_request(build_request_url(terms, search_type, config))
+    end
+    
+    def send_request(url)
       request =  URI.parse(url)
       response = Net::HTTP.get_response(request)
 
@@ -103,8 +123,7 @@ module Boss
 
       search_results
     end
-    
-    private
+          
     def parse_error(data)
       doc = REXML::Document.new(data.body) 
       # message = doc.elements['Error/Message'].text
@@ -116,15 +135,12 @@ module Boss
       end
     end
 
-    private
     def build_request_url(terms, search_type, config)
       #We could use URI.encode but it leaves things like ? unencoded which fails search.
       encoded_terms = CGI.escape(terms)
-      # puts &quot;#{@endpoint}#{search_type}/#{boss_version}/#{encoded_terms}?appid=#{@app_id}#{config.to_url}&quot;
       &quot;#{@endpoint}#{search_type}/#{boss_version}/#{encoded_terms}?appid=#{@app_id}#{config.to_url}&quot;
     end
 
-    private
     def boss_version
       &quot;v#{Boss::YAHOO_VERSION}&quot;
     end</diff>
      <filename>lib/boss/api.rb</filename>
    </modified>
    <modified>
      <diff>@@ -5,7 +5,6 @@ module Boss
   class Config &lt; OpenStruct
 
     def initialize(hash={})
-      #Setup defaults
       hash[:count] ||= hash[:limit] ||= 10
       
       super({:lang =&gt; &quot;en&quot;}.merge(hash))
@@ -21,4 +20,4 @@ module Boss
 
   end
 
-end
\ No newline at end of file
+end</diff>
      <filename>lib/boss/config.rb</filename>
    </modified>
    <modified>
      <diff>@@ -191,20 +191,22 @@ describe Boss::Api do
   end
   
   describe &quot;#search&quot; do
-    
-    [ {:count =&gt; 2,   :limit =&gt; 100, :number_of_requests =&gt; 50, :totalhits =&gt; &quot;400&quot;},
-      {:count =&gt; 50,  :limit =&gt; 100, :number_of_requests =&gt; 2,  :totalhits =&gt; &quot;400&quot;},
-      {:count =&gt; 3,   :limit =&gt; 50,  :number_of_requests =&gt; 17, :totalhits =&gt; &quot;50&quot;},
-      {:count =&gt; 3,   :limit =&gt; 50,  :number_of_requests =&gt; 14, :totalhits =&gt; &quot;40&quot;},
-      {:count =&gt; 100, :limit =&gt; 1,   :number_of_requests =&gt; 1,  :totalhits =&gt; &quot;40&quot;},
-      {:limit =&gt; 50, :count =&gt; nil, :number_of_requests =&gt; 1, :totalhits =&gt; &quot;100&quot;}
+    [ {:count =&gt; 2,   :limit =&gt; 100, :totalhits =&gt; &quot;400&quot;, :number_of_requests =&gt; 50}, # Normal case
+      {:count =&gt; 50,  :limit =&gt; 100, :totalhits =&gt; &quot;400&quot;, :number_of_requests =&gt; 2 }, # Normal case
+      {:count =&gt; 3,   :limit =&gt; 50,  :totalhits =&gt; &quot;50&quot;,  :number_of_requests =&gt; 17}, # Rounding down
+      {:count =&gt; 3,   :limit =&gt; 50,  :totalhits =&gt; &quot;40&quot;,  :number_of_requests =&gt; 14}, # Totalhits &lt; limit
+      {:count =&gt; 100, :limit =&gt; 1,   :totalhits =&gt; &quot;40&quot; , :number_of_requests =&gt; 1 }, # Limit &lt; count
+      {:count =&gt; nil, :limit =&gt; 50,  :totalhits =&gt; &quot;100&quot;, :number_of_requests =&gt; 1 }  # No count specified
     ].each do |prop|
-      it &quot;should do #{prop[:number_of_requests]} number of requests when count is #{prop[:count]} and limit is #{prop[:limit]}&quot; do
-        response_mock = mock_http_response(:body =&gt; yahoo_json(:nextpage =&gt; &quot;nextpage&quot;, :totalhits =&gt; prop[:totalhits]))
-        Net::HTTP.should_receive(:get_response).exactly(prop[:number_of_requests]).times.and_return{ response_mock }
-        @api.search_web(&quot;monkey?magic&quot;, :count =&gt; prop[:count], :limit =&gt; prop[:limit])
+      describe &quot;requesting #{prop[:count] || 'nil'} results per page&quot; do
+        describe &quot;when we ask for #{prop[:limit]} results from a total of #{prop[:totalhits]}&quot; do
+          it &quot;should make #{prop[:number_of_requests]} requests&quot; do
+            response_mock = mock_http_response(:body =&gt; yahoo_json(:nextpage =&gt; &quot;nextpage&quot;, :totalhits =&gt; prop[:totalhits]))
+            Net::HTTP.should_receive(:get_response).exactly(prop[:number_of_requests]).times.and_return{ response_mock }
+            @api.search_web(&quot;monkey?magic&quot;, :count =&gt; prop[:count], :limit =&gt; prop[:limit])
+          end
+        end
       end
-      
     end
   end
 end</diff>
      <filename>spec/boss/api_spec.rb</filename>
    </modified>
  </modified>
  <removed type="array"/>
  <parents type="array">
    <parent>
      <id>b1cec7f5d73442e246849c33601de5aa5087fae7</id>
    </parent>
  </parents>
  <author>
    <name>Joseph Wilk</name>
    <email>josephwilk@joesniff.co.uk</email>
  </author>
  <url>http://github.com/eshopworks/rboss-gem/commit/905f3d564b1eaf329ed071c2e69b9b003650dac2</url>
  <id>905f3d564b1eaf329ed071c2e69b9b003650dac2</id>
  <committed-date>2009-06-17T09:03:14-07:00</committed-date>
  <authored-date>2009-06-17T09:03:14-07:00</authored-date>
  <message>Major refactoring on internals. Cleaning up confusing spec.</message>
  <tree>26fb05de6f70b613d51974692aa6d95a04fe920f</tree>
  <committer>
    <name>Joseph Wilk</name>
    <email>josephwilk@joesniff.co.uk</email>
  </committer>
</commit>
