Преглед на файлове

set api_version to NullVersion if given nil or NullVersion

Jon G преди 5 години
родител
ревизия
7c9703d43d
променени са 5 файла, в които са добавени 15 реда и са изтрити 3 реда
  1. 4 0
      lib/shopify_api/api_version.rb
  2. 1 1
      lib/shopify_api/resources/base.rb
  3. 2 2
      lib/shopify_api/session.rb
  4. 5 0
      test/base_test.rb
  5. 3 0
      test/session_test.rb

+ 4 - 0
lib/shopify_api/api_version.rb

@@ -179,6 +179,10 @@ module ShopifyAPI
           raise NoMethodError, "NullVersion is an abstract class and cannot be instantiated."
         end
 
+        def matches?(version)
+          version.nil? || version == self
+        end
+
         def raise_not_set_error(*_args)
           raise ApiVersionNotSetError, "You must set ShopifyAPI::Base.api_version before making a request."
         end

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

@@ -68,7 +68,7 @@ module ShopifyAPI
       end
 
       def api_version=(version)
-        self._api_version = version.nil? ? ApiVersion::NullVersion : ApiVersion.find_version(version)
+        self._api_version = ApiVersion::NullVersion.matches?(version) ? ApiVersion::NullVersion : ApiVersion.find_version(version)
       end
 
       def prefix(options = {})

+ 2 - 2
lib/shopify_api/session.rb

@@ -127,11 +127,11 @@ module ShopifyAPI
     end
 
     def api_version=(version)
-      @api_version = version.nil? ? nil : ApiVersion.find_version(version)
+      @api_version = ApiVersion::NullVersion.matches?(version) ? ApiVersion::NullVersion : ApiVersion.find_version(version)
     end
 
     def valid?
-      domain.present? && token.present? && api_version.present?
+      domain.present? && token.present? && api_version.is_a?(ApiVersion)
     end
 
     def expires_in

+ 5 - 0
test/base_test.rb

@@ -168,6 +168,11 @@ class BaseTest < Test::Unit::TestCase
     assert_equal ShopifyAPI::ApiVersion::NullVersion, ShopifyAPI::Base.api_version
   end
 
+  test "#api_version= ShopifyAPI::ApiVersion::NullVersion should set ApiVersion to ShopifyAPI::ApiVersion::NullVersion" do
+    ShopifyAPI::Base.api_version = ShopifyAPI::ApiVersion::NullVersion
+    assert_equal ShopifyAPI::ApiVersion::NullVersion, ShopifyAPI::Base.api_version
+  end
+
   def clear_header(header)
     [ActiveResource::Base, ShopifyAPI::Base, ShopifyAPI::Product].each do |klass|
       klass.headers.delete(header)

+ 3 - 0
test/session_test.rb

@@ -21,6 +21,9 @@ class SessionTest < Test::Unit::TestCase
   test "not be valid without an api version" do
     session = ShopifyAPI::Session.new(domain: "testshop.myshopify.com", token: "any-token", api_version: nil)
     assert_not session.valid?
+
+    session = ShopifyAPI::Session.new(domain: "testshop.myshopify.com", token: "any-token", api_version: ShopifyAPI::ApiVersion::NullVersion)
+    assert_not session.valid?
   end
 
   test "be valid with any token, any url and version" do