Skip to content

Commit

Permalink
Add Document.move_to_draft + Document.update (#43)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
andrewvy authored Jul 12, 2024
1 parent f464514 commit db23857
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/panda_doc.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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"

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 @@ -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)
Expand Down
11 changes: 11 additions & 0 deletions lib/panda_doc/document.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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),
Expand Down
1 change: 1 addition & 0 deletions lib/panda_doc/objects/document.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 10 additions & 0 deletions lib/panda_doc/objects/empty.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# frozen_string_literal: true

module PandaDoc
module Objects
class Empty
def initialize(_response_body)
end
end
end
end
46 changes: 46 additions & 0 deletions spec/document_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit db23857

Please sign in to comment.