<?xml version="1.0" encoding="UTF-8"?>
<commit>
  <added type="array"/>
  <modified type="array">
    <modified>
      <diff>@@ -100,7 +100,6 @@ class APICache
       cache.get
     else
       begin
-        raise APICache::CannotFetch unless api.queryable?
         value = api.get
         cache.set(value)
         value</diff>
      <filename>lib/api_cache.rb</filename>
    </modified>
    <modified>
      <diff>@@ -2,16 +2,18 @@ require 'net/http'
 
 class APICache
   # Wraps up querying the API.
-  # 
-  # Ensures that the API is not called more frequently than every +period+ seconds, and times out API requests after +timeout+ seconds.
+  #
+  # Ensures that the API is not called more frequently than every +period+
+  # seconds, and times out API requests after +timeout+ seconds.
   #
   class API
     # Takes the following options
     #
-    # period:: Maximum frequency to call the API
+    # period:: Maximum frequency to call the API. If set to 0 then there is no
+    #          limit on how frequently queries can be made to the API.
     # timeout:: Timeout when calling api (either to the proviced url or
     #           excecuting the passed block)
-    # block:: If passed then the block is excecuted instead of HTTP GET 
+    # block:: If passed then the block is excecuted instead of HTTP GET
     #         against the provided key
     #
     def initialize(key, options, &amp;block)
@@ -20,27 +22,6 @@ class APICache
       @period = options[:period]
     end
 
-    # Checks whether the API can be queried (i.e. whether :period has passed
-    # since the last query to the API).
-    #
-    # If :period is 0 then there is no limit on how frequently queries can
-    # be made to the API.
-    #
-    def queryable?
-      if previously_queried?
-        if Time.now - queried_at &gt; @period
-          APICache.logger.debug &quot;Queryable: true - retry_time has passed&quot;
-          true
-        else
-          APICache.logger.debug &quot;Queryable: false - queried too recently&quot;
-          false
-        end
-      else
-        APICache.logger.debug &quot;Queryable: true - never used API before&quot;
-        true
-      end
-    end
-
     # Fetch data from the API.
     #
     # If no block is given then the key is assumed to be a URL and which will
@@ -51,6 +32,7 @@ class APICache
     # APICache::Invalid.
     #
     def get
+      check_queryable!
       APICache.logger.debug &quot;Fetching data from the API&quot;
       set_queried_at
       Timeout::timeout(@timeout) do
@@ -83,14 +65,31 @@ class APICache
       r.header['location'] ? redirecting_get(r.header['location']) : r
     end
 
+    # Checks whether the API can be queried (i.e. whether :period has passed
+    # since the last query to the API).
+    #
+    def check_queryable!
+      if previously_queried?
+        if Time.now - queried_at &gt; @period
+          APICache.logger.debug &quot;Queryable: true - retry_time has passed&quot;
+        else
+          APICache.logger.debug &quot;Queryable: false - queried too recently&quot;
+          raise APICache::CannotFetch,
+            &quot;Cannot fetch #{@key}: queried too recently&quot;
+        end
+      else
+        APICache.logger.debug &quot;Queryable: true - never used API before&quot;
+      end
+    end
+
     def previously_queried?
       APICache.store.exists?(&quot;#{@key}_queried_at&quot;)
     end
-    
+
     def queried_at
       APICache.store.get(&quot;#{@key}_queried_at&quot;)
     end
-    
+
     def set_queried_at
       APICache.store.set(&quot;#{@key}_queried_at&quot;, Time.now)
     end</diff>
      <filename>lib/api_cache/api.rb</filename>
    </modified>
    <modified>
      <diff>@@ -41,7 +41,7 @@ describe APICache do
 
   describe &quot;get method&quot; do
     before :each do
-      @api = mock(APICache::API, :get =&gt; @api_data, :queryable? =&gt; true)
+      @api = mock(APICache::API, :get =&gt; @api_data)
       @cache = mock(APICache::Cache, :get =&gt; @cache_data, :set =&gt; true)
       
       APICache::API.stub!(:new).and_return(@api)</diff>
      <filename>spec/api_cache_spec.rb</filename>
    </modified>
    <modified>
      <diff>@@ -6,15 +6,23 @@ describe APICache::API do
       :period =&gt; 1,
       :timeout =&gt; 5
     }
+
+    # Reset the store otherwise get queried too recently erros
+    APICache.store = nil
   end
 
-  it &quot;should not be queryable? for :period seconds after a request&quot; do
+  it &quot;should not be queryable for :period seconds after a request&quot; do
     api = APICache::API.new('http://www.google.com/', @options)
-    api.should be_queryable
+
     api.get
-    api.should_not be_queryable
+    lambda {
+      api.get
+    }.should raise_error(APICache::CannotFetch, &quot;Cannot fetch http://www.google.com/: queried too recently&quot;)
+
     sleep 1
-    api.should be_queryable
+    lambda {
+      api.get
+    }.should_not raise_error
   end
 
   describe &quot;without a block - key is the url&quot; do</diff>
      <filename>spec/api_spec.rb</filename>
    </modified>
  </modified>
  <removed type="array"/>
  <parents type="array">
    <parent>
      <id>e51b438dcf50872d1e0b0b541da1ca0291150741</id>
    </parent>
  </parents>
  <author>
    <name>Martyn Loughran</name>
    <email>me@mloughran.com</email>
  </author>
  <url>http://github.com/mloughran/api_cache/commit/8569dee4cba1f377e8d2777623404f3a0d2f328e</url>
  <id>8569dee4cba1f377e8d2777623404f3a0d2f328e</id>
  <committed-date>2009-05-05T14:42:40-07:00</committed-date>
  <authored-date>2009-05-05T14:42:40-07:00</authored-date>
  <message>Refactoring: Check whether API is queryable internally when calling get method

* Reduces coupling
* Added meaningful message to exception if API queried too recently</message>
  <tree>becc90620801e8a98029c489810fa5ff65418b96</tree>
  <committer>
    <name>Martyn Loughran</name>
    <email>me@mloughran.com</email>
  </committer>
</commit>
