Skip to content

Commit

Permalink
Merge pull request #605 from Shopify/raise-error-on-nil-version
Browse files Browse the repository at this point in the history
raise ApiVersionNotSetError if Base.api_version is nil
  • Loading branch information
jtgrenz authored Aug 29, 2019
2 parents b7bd602 + 7d7ae85 commit 7014e43
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 2 deletions.
17 changes: 17 additions & 0 deletions lib/shopify_api/api_version.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# frozen_string_literal: true
module ShopifyAPI
class ApiVersion
class ApiVersionNotSetError < StandardError; end
class UnknownVersion < StandardError; end
class InvalidVersion < StandardError; end

Expand Down Expand Up @@ -109,5 +110,21 @@ def construct_graphql_path
construct_api_path('graphql.json')
end
end

class NullVersion
class << self
def stable?
raise ApiVersionNotSetError, "You must set ShopifyAPI::Base.api_version before making a request."
end

def construct_api_path(*_path)
raise ApiVersionNotSetError, "You must set ShopifyAPI::Base.api_version before making a request."
end

def construct_graphql_path
raise ApiVersionNotSetError, "You must set ShopifyAPI::Base.api_version before making a request."
end
end
end
end
end
4 changes: 3 additions & 1 deletion lib/shopify_api/resources/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,13 @@ def api_version
_api_version
elsif superclass != Object && superclass.site
superclass.api_version.dup.freeze
else
ApiVersion::NullVersion
end
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 = {})
Expand Down
14 changes: 14 additions & 0 deletions test/api_version_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,20 @@ def teardown
)
end

test "NullVersion raises ApiVersionNotSetError" do
assert_raises(ShopifyAPI::ApiVersion::ApiVersionNotSetError) do
ShopifyAPI::ApiVersion::NullVersion.construct_api_path(:string)
end

assert_raises(ShopifyAPI::ApiVersion::ApiVersionNotSetError) do
ShopifyAPI::ApiVersion::NullVersion.construct_graphql_path
end

assert_raises(ShopifyAPI::ApiVersion::ApiVersionNotSetError) do
ShopifyAPI::ApiVersion::NullVersion.stable?
end
end

class TestApiVersion < ShopifyAPI::ApiVersion
def initialize(name)
@version_name = name
Expand Down
7 changes: 6 additions & 1 deletion test/base_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -158,11 +158,16 @@ def teardown
assert_equal 2, ShopifyAPI::Shop.current.id
end

test "#api_version should set ApiVersion" do
test "#api_version= should set ApiVersion" do
ShopifyAPI::Base.api_version = '2019-04'
assert_equal '2019-04', ShopifyAPI::Base.api_version.to_s
end

test "#api_version= nil should set ApiVersion to ShopifyAPI::ApiVersion::NullVersion" do
ShopifyAPI::Base.api_version = nil
assert_equal ShopifyAPI::ApiVersion::NullVersion, ShopifyAPI::Base.api_version
end

def clear_header(header)
[ActiveResource::Base, ShopifyAPI::Base, ShopifyAPI::Product].each do |klass|
klass.headers.delete(header)
Expand Down

0 comments on commit 7014e43

Please sign in to comment.