From db23857eda9d904b6e6640991f86410f29158bc5 Mon Sep 17 00:00:00 2001 From: Vy Date: Fri, 12 Jul 2024 13:02:53 -0700 Subject: [PATCH] Add `Document.move_to_draft` + `Document.update` (#43) * feat: Add support for `Document.move_to_draft` + `Document.update` * fix: Add `metadata` field to Document object * fix: Update `#update` to return empty response * fix: Add `PandaDoc::Objects::Empty` object for empty responses --- lib/panda_doc.rb | 1 + lib/panda_doc/api_client.rb | 4 +++ lib/panda_doc/document.rb | 11 ++++++++ lib/panda_doc/objects/document.rb | 1 + lib/panda_doc/objects/empty.rb | 10 +++++++ spec/document_spec.rb | 46 +++++++++++++++++++++++++++++++ 6 files changed, 73 insertions(+) create mode 100644 lib/panda_doc/objects/empty.rb diff --git a/lib/panda_doc.rb b/lib/panda_doc.rb index ef2ede3..aae1051 100644 --- a/lib/panda_doc.rb +++ b/lib/panda_doc.rb @@ -21,6 +21,7 @@ require "panda_doc/objects/token" require "panda_doc/objects/field" require "panda_doc/objects/document" +require "panda_doc/objects/empty" require "panda_doc/objects/error" require "panda_doc/objects/session" diff --git a/lib/panda_doc/api_client.rb b/lib/panda_doc/api_client.rb index a9c30fd..1c57c05 100644 --- a/lib/panda_doc/api_client.rb +++ b/lib/panda_doc/api_client.rb @@ -51,6 +51,10 @@ def get(path, data = {}) connection.get(normalized_path(path), **data) end + def patch(path, data = {}) + connection.patch(normalized_path(path), **data) + end + private def normalized_path(path) diff --git a/lib/panda_doc/document.rb b/lib/panda_doc/document.rb index 47fd27c..129324a 100644 --- a/lib/panda_doc/document.rb +++ b/lib/panda_doc/document.rb @@ -8,6 +8,13 @@ def create(data) respond(ApiClient.request(:post, "/documents", **data)) end + def update(uuid, **data) + respond( + ApiClient.request(:patch, "/documents/#{uuid}", **data), + type: :empty + ) + end + def send(uuid, **data) respond(ApiClient.request(:post, "/documents/#{uuid}/send", **data)) end @@ -20,6 +27,10 @@ def details(uuid) respond(ApiClient.request(:get, "/documents/#{uuid}/details")) end + def move_to_draft(uuid) + respond(ApiClient.request(:post, "/documents/#{uuid}/draft")) + end + def session(uuid, **data) respond( ApiClient.request(:post, "/documents/#{uuid}/session", **data), diff --git a/lib/panda_doc/objects/document.rb b/lib/panda_doc/objects/document.rb index 09289dd..bd30d62 100644 --- a/lib/panda_doc/objects/document.rb +++ b/lib/panda_doc/objects/document.rb @@ -15,6 +15,7 @@ class Document < Base attribute? :tokens, Types::Array.of(Objects::Token) attribute? :fields, Types::Array.of(Objects::Field) + attribute? :metadata, Types::Hash alias_method :created_at, :date_created alias_method :updated_at, :date_modified diff --git a/lib/panda_doc/objects/empty.rb b/lib/panda_doc/objects/empty.rb new file mode 100644 index 0000000..644a2b7 --- /dev/null +++ b/lib/panda_doc/objects/empty.rb @@ -0,0 +1,10 @@ +# frozen_string_literal: true + +module PandaDoc + module Objects + class Empty + def initialize(_response_body) + end + end + end +end diff --git a/spec/document_spec.rb b/spec/document_spec.rb index 925b815..c43abf2 100644 --- a/spec/document_spec.rb +++ b/spec/document_spec.rb @@ -286,4 +286,50 @@ it_behaves_like "a document object interface" end end + + describe ".move_to_draft" do + subject { described_class.move_to_draft("uuid") } + + before do + allow(PandaDoc::ApiClient).to receive(:request) + .with(:post, "/documents/uuid/draft") + .and_return(response) + end + + context "with failed response" do + let(:response) { failed_response } + + it_behaves_like "a failure result" + end + + context "with successful response" do + let(:response) { successful_response } + let(:body) { document_body } + + it_behaves_like "a document object interface" + end + end + + describe ".update" do + subject { described_class.update("uuid", metadata: { hello: "world" })} + + before do + allow(PandaDoc::ApiClient).to receive(:request) + .with(:patch, "/documents/uuid", metadata: { hello: "world" }) + .and_return(response) + end + + context "with failed response" do + let(:response) { failed_response } + + it_behaves_like "a failure result" + end + + context "with successful response" do + let(:response) { successful_response } + let(:body) { "" } + + it { expect { subject }.not_to raise_error } + end + end end