Skip to content

Commit

Permalink
Merge pull request #4 from substancelab/improve_retrieve
Browse files Browse the repository at this point in the history
Improve Customer
  • Loading branch information
koppen authored Oct 8, 2023
2 parents 39ae85d + 09b3a13 commit 1dfb0a0
Show file tree
Hide file tree
Showing 6 changed files with 208 additions and 1 deletion.
4 changes: 3 additions & 1 deletion lib/reconomic.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
11 changes: 11 additions & 0 deletions lib/reconomic/customer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -132,9 +132,20 @@ def construct_from(json)
Mapper.from_json(json || "")
end

# https://restdocs.e-conomic.com/#post-customers
def create(properties, session:)
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
1 change: 1 addition & 0 deletions lib/reconomic/mapper/type/object.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
require "active_support/inflector"
require "shale/type/value"

module Reconomic
Expand Down
15 changes: 15 additions & 0 deletions lib/reconomic/session.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -24,6 +25,20 @@ def post(path, body)
body: body
)
raise response.body.to_s unless response.status.success?
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),
body: body
)
raise response.body.to_s unless response.status.success?
response.body.to_s
end

private
Expand Down
142 changes: 142 additions & 0 deletions test/reconomic/customer_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,75 @@
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(body: properties.to_json, status: 200)

Reconomic::Customer.create(
properties,
session: session
)

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
it "retrieves a customer from the API" do
body = "{\"customerNumber\":1,\"name\":\"John Doe\",\"address\":\"Somewhere\"}"
Expand All @@ -31,5 +100,78 @@
_(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

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
36 changes: 36 additions & 0 deletions test/reconomic/session_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 1dfb0a0

Please sign in to comment.