Sfoglia il codice sorgente

Make versions comparable.

Alex Aitken 6 anni fa
parent
commit
1972fb0988
2 ha cambiato i file con 90 aggiunte e 54 eliminazioni
  1. 65 54
      lib/shopify_api/api_version.rb
  2. 25 0
      test/api_version_test.rb

+ 65 - 54
lib/shopify_api/api_version.rb

@@ -2,59 +2,9 @@
 module ShopifyAPI
   class ApiVersion
     class UnknownVersion < StandardError; end
+    class InvalidVersion < StandardError; end
 
-    class NoVersion < ApiVersion
-      API_PREFIX = '/admin/'
-      GRAPHQL_PATH = '/admin/api/graphql.json'
-
-      def initialize
-        @version_name = "no_version"
-      end
-
-      def construct_api_path(path)
-        "#{API_PREFIX}#{path}"
-      end
-
-      def construct_graphql_path
-        GRAPHQL_PATH
-      end
-    end
-
-    class Unstable < ApiVersion
-      API_PREFIX = '/admin/api/unstable/'
-
-      def initialize
-        @version_name = "unstable"
-        @url = API_PREFIX
-      end
-
-      def construct_api_path(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
-        construct_api_path('graphql.json')
-      end
-    end
+    include Comparable
 
     def self.coerce_to_version(version_or_name)
       return version_or_name if version_or_name.is_a?(ApiVersion)
@@ -94,7 +44,11 @@ module ShopifyAPI
     end
 
     def hash
-      version_name.hash
+      @version_name.hash
+    end
+
+    def <=>(other)
+      numeric_version <=> other.numeric_version
     end
 
     def construct_api_path(_path)
@@ -105,6 +59,63 @@ module ShopifyAPI
       raise NotImplementedError
     end
 
-    class InvalidVersion < StandardError; end
+    protected
+
+    attr_reader :numeric_version
+
+    class NoVersion < ApiVersion
+      API_PREFIX = '/admin/'
+
+      def initialize
+        @version_name = "no_version"
+        @numeric_version = 0
+      end
+
+      def construct_api_path(path)
+        "#{API_PREFIX}#{path}"
+      end
+
+      def construct_graphql_path
+        '/admin/api/graphql.json'
+      end
+    end
+
+    class Unstable < ApiVersion
+      API_PREFIX = '/admin/api/unstable/'
+
+      def initialize
+        @version_name = "unstable"
+        @url = API_PREFIX
+        @numeric_version = 9_000_00
+      end
+
+      def construct_api_path(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/'
+
+      def initialize(version_number)
+        raise InvalidVersion, version_number unless version_number.match(FORMAT)
+        @version_name = version_number
+        @url = "#{API_PREFIX}#{version_number}/"
+        @numeric_version = version_number.tr('-', '').to_i
+      end
+
+      def construct_api_path(path)
+        "#{@url}#{path}"
+      end
+
+      def construct_graphql_path
+        construct_api_path('graphql.json')
+      end
+    end
   end
 end

+ 25 - 0
test/api_version_test.rb

@@ -106,6 +106,31 @@ class ApiVersionTest < Test::Unit::TestCase
     refute_equal version_2, version_1
   end
 
+  test 'release versions are ordered by version number with unstable always being the newest and no version always being the oldest' do
+    version_1 = ShopifyAPI::ApiVersion::Release.new('2017-11')
+    version_2 = ShopifyAPI::ApiVersion::Release.new('2019-11')
+    version_3 = ShopifyAPI::ApiVersion::Release.new('2039-01')
+    version_4 = ShopifyAPI::ApiVersion::Release.new('2039-02')
+    unstable = ShopifyAPI::ApiVersion::Unstable.new
+    no_version = ShopifyAPI::ApiVersion::NoVersion.new
+
+    assert_equal([
+      no_version,
+      version_1,
+      version_2,
+      version_3,
+      version_4,
+      unstable,
+    ], [
+      version_3,
+      version_1,
+      no_version,
+      version_4,
+      unstable,
+      version_2,
+    ].sort)
+  end
+
   class TestApiVersion < ShopifyAPI::ApiVersion
     def initialize(name)
       @version_name = name