diff --git a/README.md b/README.md index 61e5ff8..3d8fafe 100644 --- a/README.md +++ b/README.md @@ -174,6 +174,38 @@ file = File.open("document.pdf", "w") do |f| end ``` +#### Listing document sections ([API](https://developers.pandadoc.com/reference/document-sections-info)) + +```ruby +sections = PandaDoc::DocumentSection.list("document_uuid") + +sections.results.each do |section| + puts section.uuid + puts section.name + puts section.status +end +``` + +#### Creating a document section ([API](https://developers.pandadoc.com/reference/create-document-section)) + +```ruby +section = PandaDoc::DocumentSection.create( + "document_uuid", + name: "Section Name", + file: file +) + +section.uuid # => "section_uuid" +section.name # => "Section Name" +section.status # => "uploaded" +``` + +#### Deleting a document section ([API](https://developers.pandadoc.com/reference/delete-section)) + +```ruby +PandaDoc::DocumentSection.delete("document_uuid", "section_uuid") +``` + #### Error handling If an error occurs during an API request it will be wrapped into a plain ruby diff --git a/lib/panda_doc/api_client.rb b/lib/panda_doc/api_client.rb index 5780ce2..ef476a2 100644 --- a/lib/panda_doc/api_client.rb +++ b/lib/panda_doc/api_client.rb @@ -54,6 +54,10 @@ def patch(path, data = {}) connection.patch(normalized_path(path), **data) end + def delete(path, data = {}) + connection.delete(normalized_path(path), **data) + end + private def normalized_path(path) diff --git a/lib/panda_doc/document_section.rb b/lib/panda_doc/document_section.rb index 46ad2ae..1c2fbff 100644 --- a/lib/panda_doc/document_section.rb +++ b/lib/panda_doc/document_section.rb @@ -4,6 +4,13 @@ module PandaDoc module DocumentSection extend self + def list(document_uuid, **options) + respond( + ApiClient.request(:get, "/documents/#{document_uuid}/sections", **options), + type: :document_sections_list + ) + end + def create(document_uuid, **data) respond( ApiClient.request(:post, "/documents/#{document_uuid}/sections/uploads", **data), @@ -11,6 +18,13 @@ def create(document_uuid, **data) ) end + def delete(document_uuid, section_uuid) + respond( + ApiClient.request(:delete, "/documents/#{document_uuid}/sections/#{section_uuid}"), + type: :empty + ) + end + private def respond(response, type: :document) diff --git a/lib/panda_doc/objects/document_sections_list.rb b/lib/panda_doc/objects/document_sections_list.rb new file mode 100644 index 0000000..abb2392 --- /dev/null +++ b/lib/panda_doc/objects/document_sections_list.rb @@ -0,0 +1,12 @@ +# frozen_string_literal: true + +module PandaDoc + module Objects + # Represents a list of document sections + class DocumentSectionsList < Base + attribute :results, Types::Array.of(Objects::DocumentSection).default([].freeze) + + alias_method :document_sections, :results + end + end +end diff --git a/spec/document_section_spec.rb b/spec/document_section_spec.rb index 03845e5..3c9926d 100644 --- a/spec/document_section_spec.rb +++ b/spec/document_section_spec.rb @@ -51,6 +51,35 @@ } end + describe ".list" do + subject { described_class.list(document_uuid) } + + before do + allow(PandaDoc::ApiClient).to receive(:request) + .with(:get, "/documents/#{document_uuid}/sections") + .and_return(response) + end + + context "with failed response" do + let(:response) { failed_response } + + it_behaves_like "a document section failure result" + end + + context "with successful response" do + let(:response) { successful_response } + let(:body) { { "results" => [document_body] } } + + it "returns an array of document sections" do + expect(subject.results).to all(be_a(PandaDoc::Objects::DocumentSection)) + end + + it "contains document sections" do + expect(subject.document_sections.first).to be_a(PandaDoc::Objects::DocumentSection) + end + end + end + describe ".create" do subject { described_class.create(document_uuid, name: "Foo") } @@ -73,4 +102,30 @@ it_behaves_like "a document section object interface" end end + + describe ".delete" do + let(:section_uuid) { "SECTION_UUID" } + subject { described_class.delete(document_uuid, section_uuid) } + + before do + allow(PandaDoc::ApiClient).to receive(:request) + .with(:delete, "/documents/#{document_uuid}/sections/#{section_uuid}") + .and_return(response) + end + + context "with failed response" do + let(:response) { failed_response } + + it_behaves_like "a document section failure result" + end + + context "with successful response" do + let(:response) { successful_response } + let(:body) { {} } + + it "returns a success result" do + expect(subject).to be_a(PandaDoc::SuccessResult) + end + end + end end