Skip to content

Commit

Permalink
Return empty array when handle not found in HandleService#get
Browse files Browse the repository at this point in the history
  • Loading branch information
gkostin1966 committed Apr 22, 2022
1 parent 483cf71 commit 3576d9e
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 5 deletions.
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
handle_rest (0.0.2)
handle_rest (0.0.3)
faraday (~> 0.9)
faraday_middleware (~> 0.14.0)

Expand Down
2 changes: 1 addition & 1 deletion handle_rest.gemspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Gem::Specification.new do |s|
s.name = "handle_rest"
s.version = "0.0.2"
s.version = "0.0.3"
s.summary = "Ruby interface to CNRI Handle REST API"

s.description = %( Ruby interface to the CNRI Handle REST API.
Expand Down
15 changes: 14 additions & 1 deletion lib/handle_rest/handle_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,12 @@ def get(handle)
if response.success?
response.body["values"].map { |v| HandleRest::ValueLine.from_h(v) }
else
raise_response_error(response)
case response_code(response)
when 100
[]
else
raise_response_error(response)
end
end
end

Expand Down Expand Up @@ -142,6 +147,14 @@ def raise_response_error(response)
raise "#{error["handle"]} - #{error["responseCode"]}: #{response_code_message(error["responseCode"])}"
end

# Response Code
#
# @param response [Faraday::Response]
# @return [Integer] code
def response_code(response)
response.body["responseCode"]&.to_i || 0
end

# Response Code Message Map
#
# @param response_code [Integer]
Expand Down
11 changes: 10 additions & 1 deletion spec/handle_rest/handle_service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,20 @@

context "when response error" do
let(:success) { false }
let(:response_body) { {"handle" => handle.to_s, "responseCode" => 2} }
let(:response_body) { {"handle" => handle.to_s, "responseCode" => code} }
let(:code) { 2 }

it "raise exception" do
expect { handle_service.get(handle) }.to raise_exception(RuntimeError, "PREFIX/HANDLE - 2: Error")
end

context "when handle not found error" do
let(:code) { 100 }

it "returns and empty array" do
expect(handle_service.get(handle)).to be_empty
end
end
end
end

Expand Down
2 changes: 1 addition & 1 deletion spec/integration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@
it "delete the handle" do # rubocop:disable RSpec/MultipleExpectations, RSpec/ExampleLength
if admin_ps.read_values && admin_ps.remove_values
admin_hs.delete(handle)
expect { admin_hs.get(handle) }.to raise_exception(RuntimeError, "#{handle} - 100: Handle Not Found")
expect(admin_hs.get(handle)).to be_empty
else
expect { admin_hs.delete(handle) }.to raise_exception(RuntimeError, "#{handle} - 400: Invalid Admin")
end
Expand Down

0 comments on commit 3576d9e

Please sign in to comment.