Skip to content

Commit

Permalink
default ApiVersion to NullVersion
Browse files Browse the repository at this point in the history
  • Loading branch information
jtgrenz committed Aug 19, 2019
1 parent b3b6a91 commit 6c637b5
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 9 deletions.
19 changes: 19 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,23 @@ def construct_graphql_path
construct_api_path('graphql.json')
end
end

class NullVersion
def self.nil?
true
end

def self.present?
false
end

def self.empty?
true
end

def self.method_missing(method_name, *args, &block)
raise ApiVersionNotSetError, "You must set ShopifyAPI::Base.api_version before making a request."
end
end
end
end
9 changes: 4 additions & 5 deletions lib/shopify_api/resources/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
module ShopifyAPI
class Base < ActiveResource::Base
class InvalidSessionError < StandardError; end
class ApiVersionNotSetError < StandardError; end
extend Countable

self.timeout = 90
Expand Down Expand Up @@ -59,17 +58,17 @@ def clear_session
end

def api_version
api_version = if _api_version_defined?
if _api_version_defined?
_api_version
elsif superclass != Object && superclass.site
superclass.api_version.dup.freeze
else
ApiVersion::NullVersion
end
raise ApiVersionNotSetError, "You must set ShopifyAPI::Base.api_version before making a request." unless api_version
api_version
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
6 changes: 6 additions & 0 deletions test/api_version_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,12 @@ def teardown
)
end

test "NullVersion raises ApiVersionNotSetError for any method call" do
assert_raises(ShopifyAPI::ApiVersion::ApiVersionNotSetError) do
ShopifyAPI::ApiVersion::NullVersion.anything
end
end

class TestApiVersion < ShopifyAPI::ApiVersion
def initialize(name)
@version_name = name
Expand Down
6 changes: 2 additions & 4 deletions test/base_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -163,11 +163,9 @@ def teardown
assert_equal '2019-04', ShopifyAPI::Base.api_version.to_s
end

test "#api_version raises ApiVersionNotSetError if no version is set." do
test "#api_version= nil should set ApiVersion to ShopifyAPI::ApiVersion::NullVersion" do
ShopifyAPI::Base.api_version = nil
assert_raises(ShopifyAPI::Base::ApiVersionNotSetError) do
ShopifyAPI::Base.api_version
end
assert_equal ShopifyAPI::ApiVersion::NullVersion, ShopifyAPI::Base.api_version
end

def clear_header(header)
Expand Down

0 comments on commit 6c637b5

Please sign in to comment.