Skip to content

Commit

Permalink
Merge pull request #51 from bwillis/fixing-bugs-with-missing-version
Browse files Browse the repository at this point in the history
Fixing bugs with missing version
  • Loading branch information
bwillis committed Apr 7, 2016
2 parents f57a324 + 3a18342 commit 669d095
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 2 deletions.
6 changes: 5 additions & 1 deletion lib/versioncake/strategies/extraction_strategy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ def extract(request)
version
elsif version.is_a?(String) && /[0-9]+/.match(version)
version.to_i
elsif version.nil? # no version was found
elsif version_blank?(version)
nil
else
raise Exception, "Invalid format for version number."
Expand All @@ -20,6 +20,10 @@ def version_key
VersionCake.config.version_key
end

def version_blank?(version)
version.nil? || (version.is_a?(String) && version.length == 0)
end

# Execute should return a number or a numeric string if it successfully finds a version.
# If no version is found, nil should be returned. Any other results returned will raise
# an exception.
Expand Down
2 changes: 1 addition & 1 deletion lib/versioncake/version_context_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def create_context_from_request(raw_request)
request.execute

result = if request.failed
:invalid_version
:version_invalid
else
check_version(resource, request.version)
end
Expand Down
16 changes: 16 additions & 0 deletions spec/unit/version_context_service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,24 @@
it { expect(context.version).to eq nil }
it { expect(context.resource).to eq resource_user }
it { expect(context.result).to eq :no_version }

context 'when the version is blank' do
let(:request) { double(version: '', path: 'users/123') }

it { expect(context.version).to eq nil }
it { expect(context.resource).to eq resource_user }
it { expect(context.result).to eq :no_version }
end
end
end

context 'for an invalid version' do
let(:request) { double(version: 'asdasd', path: 'users/123') }

it { expect(context.version).to eq nil }
it { expect(context.resource).to eq resource_user }
it { expect(context.result).to eq :version_invalid }
end
end

describe '#create_context' do
Expand Down

0 comments on commit 669d095

Please sign in to comment.