浏览代码

Allow finding of the latest stable release.

Alex Aitken 6 年之前
父节点
当前提交
928f3f07c6
共有 2 个文件被更改,包括 36 次插入0 次删除
  1. 12 0
      lib/shopify_api/api_version.rb
  2. 24 0
      test/api_version_test.rb

+ 12 - 0
lib/shopify_api/api_version.rb

@@ -30,6 +30,10 @@ module ShopifyAPI
       define_version(Unstable.new)
       define_version(Unstable.new)
     end
     end
 
 
+    def self.latest_stable_version
+      @versions.values.select(&:stable?).sort.last
+    end
+
     def to_s
     def to_s
       @version_name
       @version_name
     end
     end
@@ -51,6 +55,10 @@ module ShopifyAPI
       numeric_version <=> other.numeric_version
       numeric_version <=> other.numeric_version
     end
     end
 
 
+    def stable?
+      false
+    end
+
     def construct_api_path(_path)
     def construct_api_path(_path)
       raise NotImplementedError
       raise NotImplementedError
     end
     end
@@ -109,6 +117,10 @@ module ShopifyAPI
         @numeric_version = version_number.tr('-', '').to_i
         @numeric_version = version_number.tr('-', '').to_i
       end
       end
 
 
+      def stable?
+        true
+      end
+
       def construct_api_path(path)
       def construct_api_path(path)
         "#{@url}#{path}"
         "#{@url}#{path}"
       end
       end

+ 24 - 0
test/api_version_test.rb

@@ -106,6 +106,15 @@ class ApiVersionTest < Test::Unit::TestCase
     refute_equal version_2, version_1
     refute_equal version_2, version_1
   end
   end
 
 
+  test 'release verions are stable' do
+    assert_predicate ShopifyAPI::ApiVersion::Release.new('2019-11'), :stable?
+  end
+
+  test 'no release version are not stable' do
+    refute_predicate ShopifyAPI::ApiVersion::NoVersion.new, :stable?
+    refute_predicate ShopifyAPI::ApiVersion::Unstable.new, :stable?
+  end
+
   test 'release versions are ordered by version number with unstable always being the newest and no version always being the oldest' do
   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_1 = ShopifyAPI::ApiVersion::Release.new('2017-11')
     version_2 = ShopifyAPI::ApiVersion::Release.new('2019-11')
     version_2 = ShopifyAPI::ApiVersion::Release.new('2019-11')
@@ -131,6 +140,21 @@ class ApiVersionTest < Test::Unit::TestCase
     ].sort)
     ].sort)
   end
   end
 
 
+  test 'latest_stable_version will return the version that is newest and stable' do
+    ShopifyAPI::ApiVersion.clear_defined_versions
+    ShopifyAPI::ApiVersion.define_version(ShopifyAPI::ApiVersion::Release.new('2017-11'))
+    ShopifyAPI::ApiVersion.define_version(ShopifyAPI::ApiVersion::Release.new('2019-11'))
+    ShopifyAPI::ApiVersion.define_version(ShopifyAPI::ApiVersion::Release.new('2039-01'))
+    ShopifyAPI::ApiVersion.define_version(ShopifyAPI::ApiVersion::Release.new('2039-02'))
+    ShopifyAPI::ApiVersion.define_version(ShopifyAPI::ApiVersion::Unstable.new)
+    ShopifyAPI::ApiVersion.define_version(ShopifyAPI::ApiVersion::NoVersion.new)
+
+    assert_equal(
+      ShopifyAPI::ApiVersion::Release.new('2039-02'),
+      ShopifyAPI::ApiVersion.latest_stable_version
+    )
+  end
+
   class TestApiVersion < ShopifyAPI::ApiVersion
   class TestApiVersion < ShopifyAPI::ApiVersion
     def initialize(name)
     def initialize(name)
       @version_name = name
       @version_name = name