Add read-only access to Envelopes
This commit is contained in:
parent
48f37cc15b
commit
9848fc617f
@ -10,6 +10,7 @@ require "docurest/base"
|
|||||||
|
|
||||||
require "docurest/envelope"
|
require "docurest/envelope"
|
||||||
require "docurest/envelope/document"
|
require "docurest/envelope/document"
|
||||||
|
require "docurest/envelope/email_settings"
|
||||||
require "docurest/envelope/recipient"
|
require "docurest/envelope/recipient"
|
||||||
require "docurest/envelope/recipient/tab"
|
require "docurest/envelope/recipient/tab"
|
||||||
|
|
||||||
|
21
lib/docurest/base.rb
Normal file
21
lib/docurest/base.rb
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
module Docurest
|
||||||
|
class Base
|
||||||
|
extend Docurest::Field
|
||||||
|
|
||||||
|
def initialize(attributes = {})
|
||||||
|
self.attributes = attributes
|
||||||
|
end
|
||||||
|
|
||||||
|
def persisted?
|
||||||
|
!guid.nil?
|
||||||
|
end
|
||||||
|
|
||||||
|
private
|
||||||
|
|
||||||
|
def attributes=(attributes)
|
||||||
|
attributes.each do |key, value|
|
||||||
|
send "#{key}=", value if respond_to?("#{key}=")
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
54
lib/docurest/envelope.rb
Normal file
54
lib/docurest/envelope.rb
Normal file
@ -0,0 +1,54 @@
|
|||||||
|
module Docurest
|
||||||
|
class Envelope < Docurest::Base
|
||||||
|
def self.get(guid)
|
||||||
|
new(guid: guid).fetch
|
||||||
|
end
|
||||||
|
|
||||||
|
field :guid, :envelopeId
|
||||||
|
field :status
|
||||||
|
field :subject, :emailSubject
|
||||||
|
field :body, :emailBlurb
|
||||||
|
|
||||||
|
field :created_at, :createdDateTime, :date_time
|
||||||
|
field :updated_at, :lastModifiedDateTime, :date_time
|
||||||
|
field :delivered_at, :deliveredDateTime, :date_time
|
||||||
|
field :initial_sent_at, :initialSentDateTime, :date_time
|
||||||
|
field :sent_at, :sentDateTime, :date_time
|
||||||
|
field :completed_at, :completedDateTime, :date_time
|
||||||
|
field :status_changed_at, :statusChangedDateTime, :date_time
|
||||||
|
|
||||||
|
field :custom_fields, :customFields
|
||||||
|
|
||||||
|
field :email_settings, :emailSettings, ->(value) { Docurest::Envelope::EmailSettings.new value }
|
||||||
|
|
||||||
|
association(:recipients) { Docurest::Envelope::Recipient.list(guid) }
|
||||||
|
association(:documents) { Docurest::Envelope::Document.list(guid) }
|
||||||
|
|
||||||
|
def fetch
|
||||||
|
self.attributes = Docurest.client.get "/envelopes/#{guid}" if persisted?
|
||||||
|
self
|
||||||
|
end
|
||||||
|
|
||||||
|
def download(file, document_id = :combined)
|
||||||
|
Docurest::Envelope::Document.new(id: document_id, envelope_id: guid).download(file)
|
||||||
|
end
|
||||||
|
|
||||||
|
def void(reason = 'No reason provided.')
|
||||||
|
Docurest.client.put "/envelopes/#{guid}", {status: :voided, voidedReason: reason}
|
||||||
|
end
|
||||||
|
|
||||||
|
def to_h
|
||||||
|
{
|
||||||
|
status: status,
|
||||||
|
emailSubject: emailSubject,
|
||||||
|
emailBlurb: emailBlurb,
|
||||||
|
emailSettings: emailSettings.to_h,
|
||||||
|
documents: documents.map(&:to_h),
|
||||||
|
recipients: recipients.each_with_object({}) do |recipient, hash|
|
||||||
|
hash[recipient.type] = recipient.to_h
|
||||||
|
end,
|
||||||
|
customFields: customFields || []
|
||||||
|
}
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
46
lib/docurest/envelope/document.rb
Normal file
46
lib/docurest/envelope/document.rb
Normal file
@ -0,0 +1,46 @@
|
|||||||
|
module Docurest
|
||||||
|
class Envelope::Document < Docurest::Base
|
||||||
|
def self.list(envelope_id)
|
||||||
|
result = Docurest.client.get "/envelopes/#{envelope_id}/documents"
|
||||||
|
result['envelopeDocuments'].map do |doc|
|
||||||
|
new doc.merge(envelope_id: envelope_id)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def self.delete(envelope_id, document_ids)
|
||||||
|
document_ids = document_ids.map { |id| {documentId: id} }
|
||||||
|
Docurest.client.delete "/envelopes/#{envelope_id}/documents", {documents: document_ids}
|
||||||
|
end
|
||||||
|
|
||||||
|
field :id, :documentId
|
||||||
|
field :envelope_id
|
||||||
|
field :name
|
||||||
|
field :content
|
||||||
|
field :order
|
||||||
|
field :pages
|
||||||
|
|
||||||
|
attr_writer :file
|
||||||
|
|
||||||
|
def delete
|
||||||
|
self.class.delete(envelope_id, [id])
|
||||||
|
end
|
||||||
|
|
||||||
|
def file
|
||||||
|
@file ||= download(Tempfile.new(%w{docurest .pdf}))
|
||||||
|
end
|
||||||
|
|
||||||
|
def download(file)
|
||||||
|
body = Docurest.client.get("/envelopes/#{envelope_id}/documents/#{id}", parse_json: false)
|
||||||
|
|
||||||
|
if file.is_a?(String)
|
||||||
|
File.open(file, 'wb') { |io| io << body }
|
||||||
|
else
|
||||||
|
file << body
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def to_h
|
||||||
|
{documentId: documentId}
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
14
lib/docurest/envelope/email_settings.rb
Normal file
14
lib/docurest/envelope/email_settings.rb
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
module Docurest
|
||||||
|
class Envelope::EmailSettings < Docurest::Base
|
||||||
|
field :name, :replyEmailNameOverride
|
||||||
|
field :email, :replyEmailAddressOverride
|
||||||
|
field :bcc, :bccEmailAddresses
|
||||||
|
|
||||||
|
def to_h
|
||||||
|
{
|
||||||
|
replyEmailAddressOverride: replyEmailNameOverride,
|
||||||
|
replyEmailNameOverride: replyEmailNameOverride
|
||||||
|
}
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
34
lib/docurest/envelope/recipient.rb
Normal file
34
lib/docurest/envelope/recipient.rb
Normal file
@ -0,0 +1,34 @@
|
|||||||
|
module Docurest
|
||||||
|
class Envelope::Recipient < Docurest::Base
|
||||||
|
TYPES = %w{signers agents editors intermediaries carbonCopies certifiedDeliveries inPersonSigners}
|
||||||
|
|
||||||
|
def self.list(envelope_id)
|
||||||
|
result = Docurest.client.get "/envelopes/#{envelope_id}/recipients"
|
||||||
|
TYPES.each_with_object([]) do |type, recipients|
|
||||||
|
next unless result.key?(type)
|
||||||
|
recipients.concat(result[type].map do |recipient|
|
||||||
|
new recipient.merge(type: type, envelope_id: envelope_id)
|
||||||
|
end)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
field :id, :recipientId, :integer
|
||||||
|
field :guid, :recipientIdGuid
|
||||||
|
field :envelope_id
|
||||||
|
field :name
|
||||||
|
field :email
|
||||||
|
field :custom_fields, :customFields
|
||||||
|
field :routing_order, :routingOrder, :integer
|
||||||
|
field :status
|
||||||
|
field :note
|
||||||
|
field :type
|
||||||
|
field :signed_at, :signedDateTime, :date_time
|
||||||
|
field :delivered_at, :deliveredDateTime, :date_time
|
||||||
|
|
||||||
|
association(:tabs) { Docurest::Envelope::Recipient::Tab.list(envelope_id, guid) }
|
||||||
|
|
||||||
|
def to_h
|
||||||
|
{recipientId: recipientId}
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
28
lib/docurest/envelope/recipient/tab.rb
Normal file
28
lib/docurest/envelope/recipient/tab.rb
Normal file
@ -0,0 +1,28 @@
|
|||||||
|
module Docurest
|
||||||
|
class Envelope::Recipient::Tab < Docurest::Base
|
||||||
|
def self.list(envelope_id, recipient_id)
|
||||||
|
result = Docurest.client.get "/envelopes/#{envelope_id}/recipients/#{recipient_id}/tabs"
|
||||||
|
|
||||||
|
result.each_with_object([]) do |(type, tabs), all_tabs|
|
||||||
|
all_tabs.concat(tabs.map { |tab| new tab.merge(type: type) })
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
field :guid
|
||||||
|
field :name
|
||||||
|
field :tab_label, :tabLabel
|
||||||
|
field :scale_value, :scaleValue, :float
|
||||||
|
field :optional, nil, :boolean
|
||||||
|
field :document_id, :documentId
|
||||||
|
field :page_number, :pageNumber, :integer
|
||||||
|
field :recipient_guid, :recipientId
|
||||||
|
|
||||||
|
field :x, :xPosition
|
||||||
|
field :y, :yPosition
|
||||||
|
field :anchor, :anchorString
|
||||||
|
field :anchor_x_offset, :anchorXOffset
|
||||||
|
field :anchor_y_offset, :anchorYOffset
|
||||||
|
field :anchor_units, :anchorUnits
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
24
lib/docurest/login.rb
Normal file
24
lib/docurest/login.rb
Normal file
@ -0,0 +1,24 @@
|
|||||||
|
module Docurest
|
||||||
|
class Login < Docurest::Base
|
||||||
|
def self.information(api_password: false, guid: true, settings: :none)
|
||||||
|
result = Docurest.client.get '/login_information',
|
||||||
|
{api_password: api_password, include_account_id_guid: guid, login_settings: settings},
|
||||||
|
use_base_url: false
|
||||||
|
|
||||||
|
{
|
||||||
|
api_password: result['apiPassword'],
|
||||||
|
accounts: result['loginAccounts'].map { |login| new login }
|
||||||
|
}
|
||||||
|
end
|
||||||
|
|
||||||
|
field :id, :accountId
|
||||||
|
field :guid, :accountIdGuid
|
||||||
|
field :user_id, :userId
|
||||||
|
field :name
|
||||||
|
field :username, :userName
|
||||||
|
field :email
|
||||||
|
field :description, :siteDescription
|
||||||
|
field :url, :baseUrl
|
||||||
|
field :default, :isDefault, :boolean
|
||||||
|
end
|
||||||
|
end
|
Loading…
x
Reference in New Issue
Block a user