Parcourir la source

raise ApiVersionNotSetError if Base.api_version is nil

Jon G il y a 5 ans
Parent
commit
c31dd5c34e
2 fichiers modifiés avec 12 ajouts et 2 suppressions
  1. 4 1
      lib/shopify_api/resources/base.rb
  2. 8 1
      test/base_test.rb

+ 4 - 1
lib/shopify_api/resources/base.rb

@@ -3,6 +3,7 @@ require 'shopify_api/version'
 module ShopifyAPI
   class Base < ActiveResource::Base
     class InvalidSessionError < StandardError; end
+    class ApiVersionNotSetError < StandardError; end
     extend Countable
 
     self.timeout = 90
@@ -58,11 +59,13 @@ module ShopifyAPI
       end
 
       def api_version
-        if _api_version_defined?
+        api_version = if _api_version_defined?
           _api_version
         elsif superclass != Object && superclass.site
           superclass.api_version.dup.freeze
         end
+        raise ApiVersionNotSetError, "You must set ShopifyAPI::Base.api_version before making a request." unless api_version
+        api_version
       end
 
       def api_version=(version)

+ 8 - 1
test/base_test.rb

@@ -158,11 +158,18 @@ class BaseTest < Test::Unit::TestCase
     assert_equal 2, ShopifyAPI::Shop.current.id
   end
 
-  test "#api_version should set ApiVersion" do
+  test "#api_version= should set ApiVersion" do
     ShopifyAPI::Base.api_version = '2019-04'
     assert_equal '2019-04', ShopifyAPI::Base.api_version.to_s
   end
 
+  test "#api_version raises ApiVersionNotSetError if no version is set." do
+    ShopifyAPI::Base.api_version = nil
+    assert_raises(ShopifyAPI::Base::ApiVersionNotSetError) do
+      ShopifyAPI::Base.api_version
+    end
+  end
+
   def clear_header(header)
     [ActiveResource::Base, ShopifyAPI::Base, ShopifyAPI::Product].each do |klass|
       klass.headers.delete(header)