Browse Source

Allow finding of the latest stable release.

Alex Aitken 6 years ago
parent
commit
928f3f07c6
2 changed files with 36 additions and 0 deletions
  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