Skip to content

Commit

Permalink
Merge pull request #3 from substancelab/move_to_session
Browse files Browse the repository at this point in the history
Move request-sending to Session
  • Loading branch information
koppen authored Oct 7, 2023
2 parents 571d3c5 + c9defcb commit 39ae85d
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 42 deletions.
31 changes: 2 additions & 29 deletions lib/reconomic/customer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -87,25 +87,6 @@ class Mapper < Shale::Mapper
map "website", to: :website
map "zip", to: :zip
end

class << self
def construct_from(json)
from_json(json || "")
end

def retrieve(number:, session:)
response = HTTP
.headers({
:accept => "application/json",
"X-AgreementGrantToken" => session.agreement_grant_token,
"X-AppSecretToken" => session.app_secret_token
})
.get(
"https://restapi.e-conomic.com/customers/#{number}"
)
construct_from(response.body.to_s)
end
end
end

attr_accessor \
Expand Down Expand Up @@ -152,16 +133,8 @@ def construct_from(json)
end

def retrieve(number:, session:)
response = HTTP
.headers({
:accept => "application/json",
"X-AgreementGrantToken" => session.agreement_grant_token,
"X-AppSecretToken" => session.app_secret_token
})
.get(
"https://restapi.e-conomic.com/customers/#{number}"
)
construct_from(response.body.to_s)
response_body = session.get("/customers/#{number}")
construct_from(response_body)
end
end
end
14 changes: 1 addition & 13 deletions lib/reconomic/invoice.rb
Original file line number Diff line number Diff line change
Expand Up @@ -144,19 +144,7 @@ def lines
def save(session:)
raise "Expected payment terms to not be a string" if payment_terms.is_a?(String)
body = construct_json_for_save

response = HTTP
.headers({
:content_type => "application/json",
"X-AgreementGrantToken" => session.agreement_grant_token,
"X-AppSecretToken" => session.app_secret_token
})
.post(
"https://restapi.e-conomic.com/invoices/drafts",
body: body

)
raise response.body.to_s unless response.status.success?
session.post("/invoices/drafts", body)
end

private
Expand Down
32 changes: 32 additions & 0 deletions lib/reconomic/session.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,36 @@ def connect_with_token(app_secret_token, agreement_grant_token)
@agreement_grant_token = agreement_grant_token
@app_secret_token = app_secret_token
end

def get(path)
response = authenticated_request.get(url(path))
response.body.to_s
end

def hostname
URI.parse("https://restapi.e-conomic.com")
end

def post(path, body)
response = authenticated_request.post(
url(path),
body: body
)
raise response.body.to_s unless response.status.success?
end

private

def authenticated_request
HTTP
.headers({
:content_type => "application/json",
"X-AgreementGrantToken" => agreement_grant_token,
"X-AppSecretToken" => app_secret_token
})
end

def url(path)
hostname + path
end
end
47 changes: 47 additions & 0 deletions test/reconomic/session_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,51 @@
_(session.agreement_grant_token).must_equal("agreement_grant_token")
end
end

describe "#get" do
it "makes a GET request to the API" do
body = "{\"customerNumber\":1}"
stub_request(:get, "https://restapi.e-conomic.com/customers/1")
.to_return(body: body)

session = Reconomic::Session.new
response_body = session.get("/customers/1")

_(response_body).must_equal({"customerNumber" => 1}.to_json)
end
end

describe "#post" do
it "makes a POST request to the API" do
body = "{\"customerNumber\":1}"
stub_request(:post, "https://restapi.e-conomic.com/invoices/drafts")
.with(
body: body,
headers: {
"Content-Type" => "application/json"
}
)
.to_return(status: 200)

session = Reconomic::Session.new
session.post("/invoices/drafts", body)
end

it "raises an error if the request fails" do
body = "{\"customerNumber\":1}"
stub_request(:post, "https://restapi.e-conomic.com/invoices/drafts")
.with(
body: body,
headers: {
"Content-Type" => "application/json"
}
)
.to_return(status: 500)

session = Reconomic::Session.new
_ {
session.post("/invoices/drafts", body)
}.must_raise(RuntimeError)
end
end
end

0 comments on commit 39ae85d

Please sign in to comment.