Browse Source

default ApiVersion to NullVersion

Jon G 5 years ago
parent
commit
ea0c6c1698
4 changed files with 31 additions and 9 deletions
  1. 19 0
      lib/shopify_api/api_version.rb
  2. 4 5
      lib/shopify_api/resources/base.rb
  3. 6 0
      test/api_version_test.rb
  4. 2 4
      test/base_test.rb

+ 19 - 0
lib/shopify_api/api_version.rb

@@ -1,6 +1,7 @@
 # frozen_string_literal: true
 module ShopifyAPI
   class ApiVersion
+    class ApiVersionNotSetError < StandardError; end
     class UnknownVersion < StandardError; end
     class InvalidVersion < StandardError; end
 
@@ -109,5 +110,23 @@ module ShopifyAPI
         construct_api_path('graphql.json')
       end
     end
+
+    class NullVersion
+      def self.nil?
+        true
+      end
+
+      def self.present?
+        false
+      end
+
+      def self.empty?
+        true
+      end
+
+      def self.method_missing(method_name, *args, &block)
+        raise ApiVersionNotSetError, "You must set ShopifyAPI::Base.api_version before making a request."
+      end
+    end
   end
 end

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

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

+ 6 - 0
test/api_version_test.rb

@@ -136,6 +136,12 @@ class ApiVersionTest < Test::Unit::TestCase
     )
   end
 
+  test "NullVersion raises ApiVersionNotSetError for any method call" do
+    assert_raises(ShopifyAPI::ApiVersion::ApiVersionNotSetError) do
+      ShopifyAPI::ApiVersion::NullVersion.anything
+    end
+  end
+
   class TestApiVersion < ShopifyAPI::ApiVersion
     def initialize(name)
       @version_name = name

+ 2 - 4
test/base_test.rb

@@ -163,11 +163,9 @@ class BaseTest < Test::Unit::TestCase
     assert_equal '2019-04', ShopifyAPI::Base.api_version.to_s
   end
 
-  test "#api_version raises ApiVersionNotSetError if no version is set." do
+  test "#api_version= nil should set ApiVersion to ShopifyAPI::ApiVersion::NullVersion" do
     ShopifyAPI::Base.api_version = nil
-    assert_raises(ShopifyAPI::Base::ApiVersionNotSetError) do
-      ShopifyAPI::Base.api_version
-    end
+    assert_equal ShopifyAPI::ApiVersion::NullVersion, ShopifyAPI::Base.api_version
   end
 
   def clear_header(header)