Browse Source

Add release version.

Alex Aitken 6 years ago
parent
commit
1ef8df5070
3 changed files with 56 additions and 2 deletions
  1. 23 1
      lib/shopify_api/api_version.rb
  2. 31 0
      test/api_version_test.rb
  3. 2 1
      test/session_test.rb

+ 23 - 1
lib/shopify_api/api_version.rb

@@ -25,10 +25,30 @@ module ShopifyAPI
 
       def initialize
         @version_name = "unstable"
+        @url = API_PREFIX
       end
 
       def construct_api_path(path)
-        "#{API_PREFIX}#{path}"
+        "#{@url}#{path}"
+      end
+
+      def construct_graphql_path
+        construct_api_path("graphql.json")
+      end
+    end
+
+    class Release < ApiVersion
+      FORMAT = /^\d{4}-\d{2}$/.freeze
+      API_PREFIX = '/admin/api/'.freeze
+
+      def initialize(version_number)
+        raise InvalidVersion, version_number unless version_number.match(FORMAT)
+        @version_name = version_number
+        @url = "#{API_PREFIX}#{version_number}/"
+      end
+
+      def construct_api_path(path)
+        "#{@url}#{path}"
       end
 
       def construct_graphql_path
@@ -84,5 +104,7 @@ module ShopifyAPI
     def construct_graphql_path
       raise NotImplementedError
     end
+
+    class InvalidVersion < StandardError; end
   end
 end

+ 31 - 0
test/api_version_test.rb

@@ -75,6 +75,37 @@ class ApiVersionTest < Test::Unit::TestCase
     ])
   end
 
+  test 'allows a release version with the correct format format to be created' do
+    assert ShopifyAPI::ApiVersion::Release.new('2019-03')
+  end
+
+  test 'release versions must follow the format' do
+    assert_raises ShopifyAPI::ApiVersion::InvalidVersion do
+      assert ShopifyAPI::ApiVersion::Release.new('crazy-name')
+    end
+  end
+
+  test 'release versions create a url that is /admin/api/<version_name>/' do
+    assert_equal(
+      '/admin/api/2022-03/shop.json',
+      ShopifyAPI::ApiVersion::Release.new('2022-03').construct_api_path('shop.json')
+    )
+  end
+
+  test 'two versions with the same version number are equal' do
+    version_1 = ShopifyAPI::ApiVersion::Release.new('2018-09')
+    version_2 = ShopifyAPI::ApiVersion::Release.new('2018-09')
+
+    assert_equal version_2, version_1
+  end
+
+  test 'two versions with the different version numbers are not equal' do
+    version_1 = ShopifyAPI::ApiVersion::Release.new('2019-07')
+    version_2 = ShopifyAPI::ApiVersion::Release.new('2019-11')
+
+    refute_equal version_2, version_1
+  end
+
   class TestApiVersion < ShopifyAPI::ApiVersion
     def initialize(name)
       @version_name = name

+ 2 - 1
test/session_test.rb

@@ -290,6 +290,7 @@ class SessionTest < Test::Unit::TestCase
   end
 
   def any_api_version
-    [ShopifyAPI::ApiVersion::NoVersion.new, ShopifyAPI::ApiVersion::Unstable.new].sample(1).first
+    version_name = [:no_version, :unstable].sample(1).first
+    ShopifyAPI::ApiVersion.coerce_to_version(version_name)
   end
 end