Skip to content

Commit

Permalink
Remove dependency on try (#274)
Browse files Browse the repository at this point in the history
- It also adds tests to `assign_resources` method, which was lacking.
- This fixes #251 (#251)
  • Loading branch information
fwitzke authored Apr 19, 2021
1 parent 99a69e4 commit f12503e
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/droplet_kit/resources/project_resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def to_urn(resources)
resources.to_a.map do |resource|
if resource.is_a?(String) && DropletKit::BaseModel.valid_urn?(resource)
resource
elsif resource.try(:urn) && DropletKit::BaseModel.valid_urn?(resource.urn)
elsif resource.respond_to?(:urn) && resource.urn && DropletKit::BaseModel.valid_urn?(resource.urn)
resource.urn
else
raise DropletKit::Error.new("cannot assign resource without valid urn: #{resource}")
Expand Down
24 changes: 24 additions & 0 deletions spec/lib/droplet_kit/resources/project_resource_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -127,4 +127,28 @@
let(:api_path) { '/v2/projects/c177dc8c-12c1-4483-af1c-877eed0f14cb/resources' }
end
end

describe '#assign_resources' do
it 'calls the api when passing a valid urn' do
request = stub_do_api('/v2/projects/:id/resources', :post)
.to_return(body: '', status: 201)

resource.assign_resources(['do:droplet:123'])
expect(request).to have_been_made
end

it 'calls the api when passing a model than can be converted to urn' do
request = stub_do_api('/v2/projects/:id/resources', :post)
.to_return(body: '', status: 201)

resource.assign_resources([DropletKit::Droplet.new(id: 123)])
expect(request).to have_been_made
end

it 'raises an error when the resouce cannot be assigned' do
expect {
resource.assign_resources(['invalid'])
}.to raise_error(DropletKit::Error, 'cannot assign resource without valid urn: invalid')
end
end
end

0 comments on commit f12503e

Please sign in to comment.