소스 검색

raise explicit error when shopify call limit header is unavailable

Peter McCracken 11 년 전
부모
커밋
ed9c882982
2개의 변경된 파일24개의 추가작업 그리고 12개의 파일을 삭제
  1. 12 8
      lib/shopify_api/limits.rb
  2. 12 4
      test/limits_test.rb

+ 12 - 8
lib/shopify_api/limits.rb

@@ -1,9 +1,11 @@
 module ShopifyAPI
   module Limits
+    class LimitUnavailable < StandardError; end
+
     def self.included(klass)
       klass.send(:extend, ClassMethods)
     end
-    
+
     module ClassMethods
 
       # Takes form num_requests_executed/max_requests
@@ -20,7 +22,7 @@ module ShopifyAPI
         credit_limit(:shop) - credit_used(:shop)
       end
       alias_method :available_calls, :credit_left
-      
+
       ##
       # Have I reached my API call limit?
       # @return {Boolean}
@@ -29,7 +31,7 @@ module ShopifyAPI
         credit_left <= 0
       end
       alias_method :maxed?, :credit_maxed?
-      
+
       ##
       # How many total API calls can I make?
       # NOTE: subtracting 1 from credit_limit because I think ShopifyAPI cuts off at 299 or shop limits.
@@ -38,7 +40,7 @@ module ShopifyAPI
       #
       def credit_limit(scope=:shop)
         @api_credit_limit ||= {}
-        @api_credit_limit[scope] ||= api_credit_limit_param(scope).pop.to_i - 1     
+        @api_credit_limit[scope] ||= api_credit_limit_param(scope).pop.to_i - 1
       end
       alias_method :call_limit, :credit_limit
 
@@ -51,7 +53,7 @@ module ShopifyAPI
         api_credit_limit_param(scope).shift.to_i
       end
       alias_method :call_count, :credit_used
-      
+
       ##
       # @return {HTTPResonse}
       #
@@ -65,9 +67,11 @@ module ShopifyAPI
       ##
       # @return {Array}
       #
-      def api_credit_limit_param(scope)    
-        response[CREDIT_LIMIT_HEADER_PARAM[scope]].split('/')
-      end    
+      def api_credit_limit_param(scope)
+        header = response[CREDIT_LIMIT_HEADER_PARAM[scope]]
+        raise LimitUnavailable unless header
+        header.split('/')
+      end
     end
   end
 end

+ 12 - 4
test/limits_test.rb

@@ -7,25 +7,33 @@ class LimitsTest < Test::Unit::TestCase
     @header_hash = {'http_x_shopify_shop_api_call_limit' => '100/300'}
     ShopifyAPI::Base.connection.expects(:response).at_least(0).returns(@header_hash)
   end
-  
+
   context "Limits" do
     should "fetch limit total" do
       assert_equal(299, ShopifyAPI.credit_limit(:shop))
     end
-    
+
     should "fetch used calls" do
       assert_equal(100, ShopifyAPI.credit_used(:shop))
     end
-    
+
     should "calculate remaining calls" do
       assert_equal(199, ShopifyAPI.credit_left)
     end
-    
+
     should "flag maxed out credits" do
       assert !ShopifyAPI.maxed?
       @header_hash = {'http_x_shopify_shop_api_call_limit' => '299/300'}
       ShopifyAPI::Base.connection.expects(:response).at_least(1).returns(@header_hash)
       assert ShopifyAPI.maxed?
     end
+
+    should "raise error when header doesn't exist" do
+      @header_hash = {}
+      ShopifyAPI::Base.connection.expects(:response).at_least(1).returns(@header_hash)
+      assert_raise ShopifyAPI::Limits::LimitUnavailable do
+        ShopifyAPI.credit_left
+      end
+    end
   end
 end