Skip to content

Commit

Permalink
Add support to specify query params when updating/creating resources
Browse files Browse the repository at this point in the history
  • Loading branch information
Alcides Ramos committed Apr 25, 2023
1 parent 4e48e15 commit 1776ee8
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 51 deletions.
4 changes: 2 additions & 2 deletions lib/almodovar/single_resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ def update(attrs = {})
@xml = Nokogiri::XML.parse(response.body).root
end

def delete
check_errors(http.delete(@url), @url)
def delete(extra_query_params = {})
check_errors(http.delete(@url, extra_query_params), @url, extra_query_params)
end

def url
Expand Down
47 changes: 22 additions & 25 deletions spec/acceptance/create_resource_spec.rb
Original file line number Diff line number Diff line change
@@ -1,40 +1,38 @@
require 'spec_helper'

describe "Creating new resources" do

example "Creating a resource in a collection" do

projects = Almodovar::Resource("http://movida.example.com/projects", auth)

stub_auth_request(:post, "http://movida.example.com/projects").with do |req|
# we parse because comparing strings is too fragile because of order changing, different indentations, etc.
# we're expecting something very close to this:
# <project>
# <name>Wadus</name>
# </project>
# </project>
Nokogiri::XML.parse(req.body).at_xpath("/project/name").text == "Wadus"
end.to_return(body: %q{
<project>
<name>Wadus</name>
<link rel="self" href="http://movida.example.com/projects/1"/>
</project>
})

project = projects.create(project: {name: "Wadus"})

expect(project).to be_a(Almodovar::Resource)
expect(project.name).to eq("Wadus")

stub_auth_request(:get, "http://movida.example.com/projects/1").to_return(body: %q{
<project>
<name>Wadus</name>
<link rel="self" href="http://movida.example.com/projects/1"/>
</project>
})

expect(project.name).to eq(Almodovar::Resource(project.url, auth).name)
end

example "Creating a resource expanding links" do
stub_auth_request(:post, "http://movida.example.com/projects?expand=tasks").with do |req|
# <project>
Expand All @@ -58,19 +56,19 @@
</link>
</project>
})
projects = Almodovar::Resource("http://movida.example.com/projects", auth, expand: :tasks)

projects = Almodovar::Resource("http://movida.example.com/projects", auth, expand: :tasks)
project = projects.create(project: {name: "Wadus", template: "Basic"})

expect(project).to be_a(Almodovar::Resource)
expect(project.name).to eq("Wadus")
expect(project.tasks.size).to eq(1)
expect(project.tasks.first.name).to eq("Starting Meeting")
end

example "Creating linking to existing resources" do
projects = Almodovar::Resource("http://movida.example.com/projects", auth)

stub_auth_request(:post, "http://movida.example.com/projects").with do |req|
# <project>
# <link rel="owner" href="http://example.com/people/luismi"/>
Expand All @@ -83,16 +81,16 @@
<link rel="owner" href="http://example.com/people/luismi"/>
</project>
})

project = projects.create(project: {owner: Almodovar::Resource("http://example.com/people/luismi")})

expect(project).to be_a(Almodovar::Resource)
expect(project.owner.url).to eq("http://example.com/people/luismi")
end

example "Creating single nested resources" do
projects = Almodovar::Resource("http://movida.example.com/projects", auth, expand: :tasks)

stub_auth_request(:post, "http://movida.example.com/projects?expand=tasks").with do |req|
# <project>
# <name>Wadus</name>
Expand All @@ -117,17 +115,17 @@
</link>
</project>
})

project = projects.create(project: {name: "Wadus", timeline: {name: "Start project", wadus: {}}})

expect(project).to be_a(Almodovar::Resource)
expect(project.name).to eq("Wadus")
expect(project.timeline.name).to eq("Start project")
end

example "Creating multiple nested resources" do
projects = Almodovar::Resource("http://movida.example.com/projects", auth, expand: :tasks)

stub_auth_request(:post, "http://movida.example.com/projects?expand=tasks").with do |req|
# <project>
# <link rel="tasks">
Expand All @@ -151,9 +149,9 @@
</link>
</project>
})

project = projects.create(project: {tasks: [{name: "Start project"}]})

expect(project).to be_a(Almodovar::Resource)
expect(project.tasks.first.name).to eq("Start project")
end
Expand Down Expand Up @@ -242,5 +240,4 @@
end
end.to raise_error(Almodovar::UnprocessableEntityError)
end

end
35 changes: 29 additions & 6 deletions spec/acceptance/delete_resource_spec.rb
Original file line number Diff line number Diff line change
@@ -1,21 +1,44 @@
require 'spec_helper'

describe "Deleting resources" do

example "Deleting a resource" do
project = Almodovar::Resource("http://movida.example.com/projects/1", auth)
url = "http://movida.example.com/projects/1"

project = Almodovar::Resource(url, auth)

stub_auth_request(:delete, "http://movida.example.com/projects/1").to_return(status: 200)
stub_auth_request(:delete, url).to_return(status: 200)

project.delete

expect(auth_request(:delete, "http://movida.example.com/projects/1")).to have_been_made.once
expect(auth_request(:delete, url)).to have_been_made.once
end

example "Deleting a resource with query params" do
url = "http://movida.example.com/projects/1"

project = Almodovar::Resource(url, auth)

stub_auth_request(:delete, url).with({
query: {
param1: 1,
param2: 2,
}
}).to_return(status: 200)

project.delete({
param1: 1,
param2: 2,
})

expect(auth_request(:delete, url + "?param1=1&param2=2")).to have_been_made.once
end

example "Deleting a resource raise UnprocessableEntityError" do
project = Almodovar::Resource("http://movida.example.com/projects/1", auth)
url = "http://movida.example.com/projects/1"

project = Almodovar::Resource(url, auth)

stub_auth_request(:delete, "http://movida.example.com/projects/1").to_return(body: %q{
stub_auth_request(:delete, url).to_return(body: %q{
<errors>
<error>Should delete existing tasks first</error>
</errors>
Expand Down
30 changes: 16 additions & 14 deletions spec/acceptance/update_resource_spec.rb
Original file line number Diff line number Diff line change
@@ -1,37 +1,39 @@
require 'spec_helper'

describe "Updating resources" do

example "Updating a resource" do
stub_auth_request(:put, "http://movida.example.com/projects/1").with do |req|
url = "http://movida.example.com/projects/1"

stub_auth_request(:put, url).with do |req|
req.body == {name: "Wadus Wadus"}.to_xml(root: "project")
end.to_return(body: <<-XML)
<project>
<name>Wadus Wadus</name>
<link rel="self" href="http://movida.example.com/projects/1"/>
<link rel="self" href="#{url}"/>
</project>
XML
project = Almodovar::Resource("http://movida.example.com/projects/1", auth)

project = Almodovar::Resource(url, auth)

project.update(project: {name: "Wadus Wadus"})

expect(project.name).to eq("Wadus Wadus")
end

example "Updating a document resource" do
stub_auth_request(:put, "http://movida.example.com/people/1/extra_data").with do |req|
url = "http://movida.example.com/people/1/extra_data"

stub_auth_request(:put, url).with do |req|
req.body == {birthplace: "Calzada de Calatrava"}.to_xml(root: "extra-data")
end.to_return(body: <<-XML)
<extra-data type="document">
<birthplace>Calzada de Calatrava</birthplace>
<birthyear type="integer">1949</birthyear>
</extra-data>
XML
extra_data = Almodovar::Resource("http://movida.example.com/people/1/extra_data", auth)
extra_data.update(extra_data: {birthplace: "Calzada de Calatrava"})

extra_data = Almodovar::Resource(url, auth)
extra_data.update(extra_data: { birthplace: "Calzada de Calatrava" })
expect(extra_data.birthplace).to eq("Calzada de Calatrava")
end

end
end
32 changes: 28 additions & 4 deletions spec/unit/single_resource_spec.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
require 'spec_helper'

describe Almodovar::SingleResource do

it "#update uses proper Content-Type header" do
url = "http://movida.bebanjo.com/titles/1"

stub_request(:put, "http://movida.bebanjo.com/titles/1")
stub_request(:put, url)

Almodovar::SingleResource.new("http://movida.bebanjo.com/titles/1", nil).update(title: {"name" => "Kamikaze"})
Almodovar::SingleResource.new(url, nil).update(title: {"name" => "Kamikaze"})

expect(a_request(:put, "http://movida.bebanjo.com/titles/1").with(headers: {'Content-Type' => "application/xml"})).to have_been_made
expect(a_request(:put, url).with(headers: {'Content-Type' => "application/xml"})).to have_been_made
end

it "#to_hash" do
Expand All @@ -22,4 +22,28 @@

expect(Almodovar::SingleResource.new(url, nil).to_hash).to eq({ "name" => "Resource Name" })
end

it "#delete" do
url = "http://movida.bebanjo.com/titles/1"

stub_request(:delete, url).to_return(status: 200)

expect(Almodovar::SingleResource.new(url, nil).delete).to be_nil
end

it "#delete with query params" do
url = "http://movida.bebanjo.com/titles/1"

stub_request(:delete, url).with({
query: {
param1: 1,
param2: 2
}
}).to_return(status: 200)

expect(Almodovar::SingleResource.new(url, nil).delete({
param1: 1,
param2: 2
})).to be_nil
end
end

0 comments on commit 1776ee8

Please sign in to comment.