Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions lib/panda_doc/api_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
14 changes: 14 additions & 0 deletions lib/panda_doc/document_section.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,27 @@ 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),
type: :document_section
)
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)
Expand Down
12 changes: 12 additions & 0 deletions lib/panda_doc/objects/document_sections_list.rb
Original file line number Diff line number Diff line change
@@ -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
55 changes: 55 additions & 0 deletions spec/document_section_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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") }

Expand All @@ -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
Loading