From 3822e49f6bf1f19c284f0e9eff0b46b08157ef38 Mon Sep 17 00:00:00 2001 From: Jakob Skjerning Date: Sun, 8 Oct 2023 12:13:29 +0200 Subject: [PATCH 1/6] Raise an error if the request fails --- lib/reconomic.rb | 4 +++- lib/reconomic/session.rb | 1 + test/reconomic/customer_test.rb | 11 +++++++++++ 3 files changed, 15 insertions(+), 1 deletion(-) diff --git a/lib/reconomic.rb b/lib/reconomic.rb index 5e7b0cf..9392f32 100644 --- a/lib/reconomic.rb +++ b/lib/reconomic.rb @@ -9,5 +9,7 @@ module Reconomic class Error < StandardError; end - # Your code goes here... + + # Used when the e-conomic API returns an error + class EconomicError < Error; end end diff --git a/lib/reconomic/session.rb b/lib/reconomic/session.rb index 595c9e2..d1328ab 100644 --- a/lib/reconomic/session.rb +++ b/lib/reconomic/session.rb @@ -11,6 +11,7 @@ def connect_with_token(app_secret_token, agreement_grant_token) def get(path) response = authenticated_request.get(url(path)) + raise Reconomic::EconomicError, response.status.to_s unless response.status.success? response.body.to_s end diff --git a/test/reconomic/customer_test.rb b/test/reconomic/customer_test.rb index e526b4f..3d52d52 100644 --- a/test/reconomic/customer_test.rb +++ b/test/reconomic/customer_test.rb @@ -31,5 +31,16 @@ _(customer.customer_number).must_equal(1) _(customer.name).must_equal("John Doe") end + + it "raises an error if the request fails" do + stub_request(:get, "https://restapi.e-conomic.com/customers/1") + .to_return(status: 401) + + session = Reconomic::Session.new + + _ { + Reconomic::Customer.retrieve(number: 1, session: session) + }.must_raise(Reconomic::EconomicError) + end end end From a8e85f6e6ee9b1b2db1c1097019a7f55a43e2c9a Mon Sep 17 00:00:00 2001 From: Jakob Skjerning Date: Sun, 8 Oct 2023 12:15:22 +0200 Subject: [PATCH 2/6] Add missing require --- lib/reconomic/mapper/type/object.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/reconomic/mapper/type/object.rb b/lib/reconomic/mapper/type/object.rb index 5b6710f..e12ca4e 100644 --- a/lib/reconomic/mapper/type/object.rb +++ b/lib/reconomic/mapper/type/object.rb @@ -1,3 +1,4 @@ +require "active_support/inflector" require "shale/type/value" module Reconomic From 8c567b9aa565a6b632fee53ace3f8f059db4c974 Mon Sep 17 00:00:00 2001 From: Jakob Skjerning Date: Sun, 8 Oct 2023 12:37:41 +0200 Subject: [PATCH 3/6] Make it possible to create customers --- lib/reconomic/customer.rb | 5 +++++ test/reconomic/customer_test.rb | 35 +++++++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+) diff --git a/lib/reconomic/customer.rb b/lib/reconomic/customer.rb index fbfb887..24c8aed 100644 --- a/lib/reconomic/customer.rb +++ b/lib/reconomic/customer.rb @@ -132,6 +132,11 @@ def construct_from(json) Mapper.from_json(json || "") end + # https://restdocs.e-conomic.com/#post-customers + def create(properties, session:) + session.post("/customers", properties.to_json) + end + def retrieve(number:, session:) response_body = session.get("/customers/#{number}") construct_from(response_body) diff --git a/test/reconomic/customer_test.rb b/test/reconomic/customer_test.rb index 3d52d52..24b6772 100644 --- a/test/reconomic/customer_test.rb +++ b/test/reconomic/customer_test.rb @@ -18,6 +18,41 @@ end end + describe ".create" do + it "posts a customer to the API" do + properties = { + "currency" => "EUR", + "customerGroup" => { + "customerGroupNumber" => 1 + }, + "name" => "Acme Inc", + "paymentTerms" => { + "paymentTermsNumber" => 2 + }, + "vatZone" => { + "vatZoneNumber" => 2 + } + } + + session = Reconomic::Session.new + stub_request(:post, "https://restapi.e-conomic.com/customers") + .with( + body: properties, + headers: { + "Content-Type" => "application/json" + } + ) + .to_return(status: 200) + + Reconomic::Customer.create( + properties, + session: session + ) + + assert_requested(:post, "https://restapi.e-conomic.com/customers") + end + end + describe ".retrieve" do it "retrieves a customer from the API" do body = "{\"customerNumber\":1,\"name\":\"John Doe\",\"address\":\"Somewhere\"}" From 46a3fcd5947e3a2b3dbd7502f6a6d7d2a8c7f8d9 Mon Sep 17 00:00:00 2001 From: Jakob Skjerning Date: Sun, 8 Oct 2023 12:57:19 +0200 Subject: [PATCH 4/6] Add Session#put --- lib/reconomic/session.rb | 8 ++++++++ test/reconomic/session_test.rb | 36 ++++++++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+) diff --git a/lib/reconomic/session.rb b/lib/reconomic/session.rb index d1328ab..6cafa6a 100644 --- a/lib/reconomic/session.rb +++ b/lib/reconomic/session.rb @@ -27,6 +27,14 @@ def post(path, body) raise response.body.to_s unless response.status.success? end + def put(path, body) + response = authenticated_request.put( + url(path), + body: body + ) + raise response.body.to_s unless response.status.success? + end + private def authenticated_request diff --git a/test/reconomic/session_test.rb b/test/reconomic/session_test.rb index f70b7bd..4179a15 100644 --- a/test/reconomic/session_test.rb +++ b/test/reconomic/session_test.rb @@ -64,4 +64,40 @@ }.must_raise(RuntimeError) end end + + describe "#put" do + it "makes a PUT request to the API" do + body = "{\"customerNumber\":1}" + stub_request(:put, "https://restapi.e-conomic.com/customers/42") + .with( + body: body, + headers: { + "Content-Type" => "application/json" + } + ) + .to_return(status: 200) + + session = Reconomic::Session.new + session.put("/customers/42", body) + + assert_requested(:put, "https://restapi.e-conomic.com/customers/42") + end + + it "raises an error if the request fails" do + body = "{\"customerNumber\":1}" + stub_request(:put, "https://restapi.e-conomic.com/customers/42") + .with( + body: body, + headers: { + "Content-Type" => "application/json" + } + ) + .to_return(status: 500) + + session = Reconomic::Session.new + _ { + session.put("/customers/42", body) + }.must_raise(RuntimeError) + end + end end From ab1580b8c02a97b1ce195be651d645516486773e Mon Sep 17 00:00:00 2001 From: Jakob Skjerning Date: Sun, 8 Oct 2023 13:45:01 +0200 Subject: [PATCH 5/6] Return objects from create and update --- lib/reconomic/customer.rb | 8 ++- lib/reconomic/session.rb | 2 + test/reconomic/customer_test.rb | 98 ++++++++++++++++++++++++++++++++- 3 files changed, 106 insertions(+), 2 deletions(-) diff --git a/lib/reconomic/customer.rb b/lib/reconomic/customer.rb index 24c8aed..af36177 100644 --- a/lib/reconomic/customer.rb +++ b/lib/reconomic/customer.rb @@ -134,12 +134,18 @@ def construct_from(json) # https://restdocs.e-conomic.com/#post-customers def create(properties, session:) - session.post("/customers", properties.to_json) + response_body = session.post("/customers", properties.to_json) + construct_from(response_body) end def retrieve(number:, session:) response_body = session.get("/customers/#{number}") construct_from(response_body) end + + def update(number, properties, session:) + response_body = session.put("/customers/#{number}", properties.to_json) + construct_from(response_body) + end end end diff --git a/lib/reconomic/session.rb b/lib/reconomic/session.rb index 6cafa6a..98d1d0a 100644 --- a/lib/reconomic/session.rb +++ b/lib/reconomic/session.rb @@ -25,6 +25,7 @@ def post(path, body) body: body ) raise response.body.to_s unless response.status.success? + response.body.to_s end def put(path, body) @@ -33,6 +34,7 @@ def put(path, body) body: body ) raise response.body.to_s unless response.status.success? + response.body.to_s end private diff --git a/test/reconomic/customer_test.rb b/test/reconomic/customer_test.rb index 24b6772..f9c69e8 100644 --- a/test/reconomic/customer_test.rb +++ b/test/reconomic/customer_test.rb @@ -42,7 +42,7 @@ "Content-Type" => "application/json" } ) - .to_return(status: 200) + .to_return(body: properties.to_json, status: 200) Reconomic::Customer.create( properties, @@ -51,6 +51,40 @@ assert_requested(:post, "https://restapi.e-conomic.com/customers") end + + it "returns the created customer" do + properties = { + "currency" => "EUR", + "customerGroup" => { + "customerGroupNumber" => 1 + }, + "name" => "Acme Inc", + "paymentTerms" => { + "paymentTermsNumber" => 2 + }, + "vatZone" => { + "vatZoneNumber" => 2 + } + } + + session = Reconomic::Session.new + stub_request(:post, "https://restapi.e-conomic.com/customers") + .with( + body: properties, + headers: { + "Content-Type" => "application/json" + } + ) + .to_return(body: properties.to_json, status: 200) + + result = Reconomic::Customer.create( + properties, + session: session + ) + + _(result).must_be_instance_of(Reconomic::Customer) + _(result.name).must_equal("Acme Inc") + end end describe ".retrieve" do @@ -78,4 +112,66 @@ }.must_raise(Reconomic::EconomicError) end end + + describe ".update" do + it "puts a customer to the API" do + properties = { + "currency" => "EUR", + "customerGroup" => { + "customerGroupNumber" => 1 + }, + "name" => "Acme Inc", + "paymentTerms" => { + "paymentTermsNumber" => 2 + }, + "vatZone" => { + "vatZoneNumber" => 2 + } + } + + session = Reconomic::Session.new + stub_request(:put, "https://restapi.e-conomic.com/customers/42") + .with( + body: properties, + headers: { + "Content-Type" => "application/json" + } + ) + .to_return(body: properties.to_json, status: 200) + + Reconomic::Customer.update( + 42, + properties, + session: session + ) + + assert_requested(:put, "https://restapi.e-conomic.com/customers/42") + end + + it "returns the updated customer" do + properties = { + "customerNumber" => 42, + "name" => "Acme Inc" + } + + session = Reconomic::Session.new + stub_request(:put, "https://restapi.e-conomic.com/customers/42") + .with( + body: properties, + headers: { + "Content-Type" => "application/json" + } + ) + .to_return(body: properties.to_json, status: 200) + + result = Reconomic::Customer.update( + 42, + properties, + session: session + ) + + _(result).must_be_instance_of(Reconomic::Customer) + _(result.name).must_equal("Acme Inc") + end + end end From 09b3a133f8b1eb40c911807cc249c615bc280fd0 Mon Sep 17 00:00:00 2001 From: Jakob Skjerning Date: Sun, 8 Oct 2023 13:59:27 +0200 Subject: [PATCH 6/6] Note that PUT is a complete update --- lib/reconomic/session.rb | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/lib/reconomic/session.rb b/lib/reconomic/session.rb index 98d1d0a..c31d102 100644 --- a/lib/reconomic/session.rb +++ b/lib/reconomic/session.rb @@ -28,6 +28,10 @@ def post(path, body) response.body.to_s end + # Update a resource by sending a PUT request to the API. + # + # Note that PUT requests expect the entire resource to be sent in the request, + # ie there is no partial update. def put(path, body) response = authenticated_request.put( url(path),