Skip to content

Commit

Permalink
Fix bug in UrlService#set method.
Browse files Browse the repository at this point in the history
  • Loading branch information
gkostin1966 committed Apr 26, 2022
1 parent 3576d9e commit baed402
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 10 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.3)
handle_rest (0.0.4)
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.3"
s.version = "0.0.4"
s.summary = "Ruby interface to CNRI Handle REST API"

s.description = %( Ruby interface to the CNRI Handle REST API.
Expand Down
12 changes: 6 additions & 6 deletions lib/handle_rest/url_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@ def initialize(index, service)

# Get a Handle's URL
#
# raise RuntimeError if 'handle' is invalid, index:prefix/suffix, otherwise
# raise RuntimeError if 'handle' is invalid, prefix/suffix, otherwise
# return nil if 'handle' does NOT exist, otherwise
# return nil if value line at 'index' does NOT exist, otherwise
# raise RuntimeError if value line at 'index' is NOT an 'URL', otherwise
# raise RuntimeError if get of 'url' value line at 'index' fails, otherwise
# return 'url' of 'index' value line.
#
# @param handle [String] index:prefix/suffix
# @param handle [String] prefix/suffix
# @return [String] url or nil
# @raise [RuntimeError]
def get(handle)
Expand All @@ -43,13 +43,13 @@ def get(handle)

# Set a Handle's URL
#
# raise RuntimeError if 'handle' is invalid, index:prefix/suffix, otherwise
# raise RuntimeError if 'handle' is invalid, prefix/suffix, otherwise
# raise RuntimeError if 'url' is invalid, scheme/protocol://host name[:port number] [/path][/query_string][/#fragment], otherwise
# raise RuntimeError if value line at 'index' exist and is NOT an 'URL', otherwise
# raise RuntimeError if set of 'url' value line at 'index' fails, otherwise
# return 'url' of 'index' value line
#
# @param handle [String] index:prefix/suffix
# @param handle [String] prefix/suffix
# @param new_url [String] scheme/protocol://host name[:port number] [/path][/query_string][/#fragment]
# @return [String] url
# @raise [RuntimeError]
Expand All @@ -64,12 +64,12 @@ def set(handle, new_url)
raise "Value type '#{index_value.type}' at index '#{index_value.index}' is NOT an 'URL' type." unless index_value.nil? || index_value.type == "URL"
old_url = index_value&.value
index_value = nil
value_lines = @service.write(hdl, url)
value_lines = @service.write(hdl, [ValueLine.new(@index, url)])
index_value_lines = value_lines.select { |value_line| value_line.index == @index }
index_value = index_value_lines[0].value unless index_value_lines.empty?
raise "Failed to add url '#{new_url}' to '#{handle}' at index '#{@index}'." if old_url.nil? && index_value.nil?
raise "Failure deleted url '#{old_url}' from '#{handle}' at index '#{@index}'!!!" if !old_url.nil? && index_value.nil?
raise "Failed to replace url '#{old_url}' with '#{new_url}' for '#{handle}' at index '#{@index}'." if old_url == index_value.value
raise "Failed to replace url '#{old_url}' with '#{new_url}' for '#{handle}' at index '#{@index}'." if old_url != new_url && old_url == index_value.value
raise "Failure corrupted url '#{old_url}' with '#{index_value.value}' for '#{handle}' at index '#{@index}'!!!" if old_url != index_value.value && new_url != index_value.value
index_value.value
end
Expand Down
4 changes: 2 additions & 2 deletions spec/handle_rest/url_service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
before do
allow(service).to receive(:is_a?).with(HandleRest::Service).and_return true
allow(service).to receive(:read).with(HandleRest::Handle.from_s(handle)).and_return value_lines
allow(service).to receive(:write).with(HandleRest::Handle.from_s(handle), HandleRest::UrlValue.from_s(new_url)).and_return new_value_lines
allow(service).to receive(:write).with(HandleRest::Handle.from_s(handle), [HandleRest::ValueLine.new(service_index, HandleRest::UrlValue.from_s(new_url))]).and_return new_value_lines
end

describe "#initialize" do
Expand Down Expand Up @@ -184,7 +184,7 @@
end

context "when service write fails" do
before { allow(service).to receive(:write).with(HandleRest::Handle.from_s(handle), HandleRest::UrlValue.from_s(new_url)).and_raise RuntimeError }
before { allow(service).to receive(:write).with(HandleRest::Handle.from_s(handle), [HandleRest::ValueLine.new(service_index, HandleRest::UrlValue.from_s(new_url))]).and_raise RuntimeError }

it { expect { url_service_set }.to raise_exception RuntimeError }
end
Expand Down

0 comments on commit baed402

Please sign in to comment.