Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ApiVersion::NullVersion tweaks #615

Merged
merged 2 commits into from
Sep 4, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions lib/shopify_api/api_version.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ def version_lookup_mode=(mode)
end

def find_version(version_or_handle)
raise ArgumentError, "NullVersion is not a valid version or version handle." if version_or_handle == NullVersion
return version_or_handle if version_or_handle.is_a?(ApiVersion)
handle = version_or_handle.to_s
@versions ||= {}
Expand Down Expand Up @@ -174,6 +175,14 @@ def handle_as_date

class NullVersion
class << self
def new(*_args)
raise NoMethodError, "NullVersion is an abstract class and cannot be instantiated."
end

def matches?(version)
version.nil? || version == self
end

def raise_not_set_error(*_args)
raise ApiVersionNotSetError, "You must set ShopifyAPI::Base.api_version before making a request."
end
Expand Down
2 changes: 1 addition & 1 deletion lib/shopify_api/resources/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ def api_version
end

def api_version=(version)
self._api_version = version.nil? ? ApiVersion::NullVersion : ApiVersion.find_version(version)
self._api_version = ApiVersion::NullVersion.matches?(version) ? ApiVersion::NullVersion : ApiVersion.find_version(version)
end

def prefix(options = {})
Expand Down
4 changes: 2 additions & 2 deletions lib/shopify_api/session.rb
Original file line number Diff line number Diff line change
Expand Up @@ -127,11 +127,11 @@ def site
end

def api_version=(version)
@api_version = version.nil? ? nil : ApiVersion.find_version(version)
@api_version = ApiVersion::NullVersion.matches?(version) ? ApiVersion::NullVersion : ApiVersion.find_version(version)
end

def valid?
domain.present? && token.present? && api_version.present?
domain.present? && token.present? && api_version.is_a?(ApiVersion)
end

def expires_in
Expand Down
15 changes: 15 additions & 0 deletions test/api_version_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,15 @@ class ApiVersionTest < Test::Unit::TestCase
end
end

test "find_version raises ArgumentError when given an ShopifyAPI::ApiVersion::NullVersion object" do
ShopifyAPI::ApiVersion.clear_known_versions
ShopifyAPI::ApiVersion.version_lookup_mode = :define_on_unknown
assert_equal :define_on_unknown, ShopifyAPI::ApiVersion.version_lookup_mode
assert_raises ArgumentError do
ShopifyAPI::ApiVersion.find_version(ShopifyAPI::ApiVersion::NullVersion)
end
end

test 'two versions with the same version number are equal' do
version_1 = ShopifyAPI::ApiVersion.new(handle: '2018-09')
version_2 = ShopifyAPI::ApiVersion.new(handle: '2018-09')
Expand Down Expand Up @@ -125,6 +134,12 @@ class ApiVersionTest < Test::Unit::TestCase
end
end

test "NullVersion cannot be instantiated and raises NoMethodError if attempted" do
assert_raises(NoMethodError) do
ShopifyAPI::ApiVersion::NullVersion.new
end
end

test "handle_to_date converts a version handle to a date" do
version_1 = ShopifyAPI::ApiVersion.new(handle: '2019-01')
version_2 = ShopifyAPI::ApiVersion.new(handle: 'unstable')
Expand Down
5 changes: 5 additions & 0 deletions test/base_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,11 @@ def teardown
assert_equal ShopifyAPI::ApiVersion::NullVersion, ShopifyAPI::Base.api_version
end

test "#api_version= ShopifyAPI::ApiVersion::NullVersion should set ApiVersion to ShopifyAPI::ApiVersion::NullVersion" do
ShopifyAPI::Base.api_version = ShopifyAPI::ApiVersion::NullVersion
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
3 changes: 3 additions & 0 deletions test/session_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ def setup
test "not be valid without an api version" do
session = ShopifyAPI::Session.new(domain: "testshop.myshopify.com", token: "any-token", api_version: nil)
assert_not session.valid?

session = ShopifyAPI::Session.new(domain: "testshop.myshopify.com", token: "any-token", api_version: ShopifyAPI::ApiVersion::NullVersion)
assert_not session.valid?
end

test "be valid with any token, any url and version" do
Expand Down