diff --git a/lib/docurest.rb b/lib/docurest.rb index 2bb0149..915afc8 100644 --- a/lib/docurest.rb +++ b/lib/docurest.rb @@ -10,6 +10,7 @@ require "docurest/base" require "docurest/envelope" require "docurest/envelope/document" +require "docurest/envelope/email" require "docurest/envelope/email_settings" require "docurest/envelope/recipient" require "docurest/envelope/recipient/tab" diff --git a/lib/docurest/base.rb b/lib/docurest/base.rb index 8e2d6d6..9786064 100644 --- a/lib/docurest/base.rb +++ b/lib/docurest/base.rb @@ -2,17 +2,24 @@ module Docurest class Base extend Docurest::Field + def self.hash_by_type(array) + array.each_with_object(Hash.new { |h,k| h[k] = [] }) do |value, hash| + hash[value.type.to_sym] << value.to_h + end + end + def initialize(attributes = {}) self.attributes = attributes end def persisted? - !guid.nil? + @persisted || (respond_to?(:guid) && guid) end private def attributes=(attributes) + return unless attributes attributes.each do |key, value| send "#{key}=", value if respond_to?("#{key}=") end diff --git a/lib/docurest/envelope.rb b/lib/docurest/envelope.rb index 2f69440..f2a7f56 100644 --- a/lib/docurest/envelope.rb +++ b/lib/docurest/envelope.rb @@ -1,5 +1,17 @@ module Docurest class Envelope < Docurest::Base + def self.list_status(from_date:, status: 'Any') + result = Docurest.client.get "/envelopes/status", + from_date: from_date, status: 'Any' + result['envelopes'].map { |envelope| new envelope } + end + + def self.list(from_date:, status: 'Any') + result = Docurest.client.get "/envelopes", + from_date: from_date, status: status + result['envelopes'].map { |envelope| new envelope } + end + def self.get(guid) new(guid: guid).fetch end @@ -15,14 +27,27 @@ module Docurest field :initial_sent_at, :initialSentDateTime, :date_time field :sent_at, :sentDateTime, :date_time field :completed_at, :completedDateTime, :date_time + field :status_at, :statusDateTime, :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) } + association(:recipients, :envelope_id) { Docurest::Envelope::Recipient.list(guid) } + def save_recipients + Docurest::Envelope::Recipient.save recipients + end + + association(:documents, :envelope_id) { Docurest::Envelope::Document.list(guid) } + + def audit_event + Docurest.client.get "/envelopes/#{guid}/audit_events" + end + + def email_settings + @email_settings ||= Docurest::Envelope::EmailSettings.new(envelope_id: guid).fetch + end def fetch self.attributes = Docurest.client.get "/envelopes/#{guid}" if persisted? @@ -37,6 +62,15 @@ module Docurest Docurest.client.put "/envelopes/#{guid}", {status: :voided, voidedReason: reason} end + def save + result = if persisted? + Docurest.client.put "/envelopes/#{guid}", to_h + else + Docurest.client.post "/envelopes", to_h + end + self.attributes = result + end + def to_h { status: status, @@ -44,11 +78,15 @@ module Docurest 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 || [] + recipients: Docurest::Base.hash_by_type(recipients), + files: files, } end + + private + + def files + Hash[documents.map(&:upload)] + end end end diff --git a/lib/docurest/envelope/document.rb b/lib/docurest/envelope/document.rb index b3480ed..b289cb6 100644 --- a/lib/docurest/envelope/document.rb +++ b/lib/docurest/envelope/document.rb @@ -2,6 +2,8 @@ module Docurest class Envelope::Document < Docurest::Base def self.list(envelope_id) result = Docurest.client.get "/envelopes/#{envelope_id}/documents" + return [] unless result.key?('envelopeDocuments') + result['envelopeDocuments'].map do |doc| new doc.merge(envelope_id: envelope_id) end @@ -21,6 +23,11 @@ module Docurest attr_writer :file + def initialize(attribute = {}) + @content = 'application/pdf' + super + end + def delete self.class.delete(envelope_id, [id]) end @@ -39,8 +46,12 @@ module Docurest end end + def upload + ["file#{id}", UploadIO.new(file, content, name, 'Content-Disposition' => "file; documentid=#{id}")] + end + def to_h - {documentId: documentId} + {documentId: documentId, name: name} end end end diff --git a/lib/docurest/envelope/email.rb b/lib/docurest/envelope/email.rb new file mode 100644 index 0000000..8c70aef --- /dev/null +++ b/lib/docurest/envelope/email.rb @@ -0,0 +1,11 @@ +module Docurest + class Envelope::Email < Docurest::Base + field :subject, :emailSubject + field :body, :emailBody + field :language, :supportedLanguage + + def to_h + {emailSubject: emailSubject, emailBody: emailBody, supportedLanguage: supportedLanguage} + end + end +end diff --git a/lib/docurest/envelope/email_settings.rb b/lib/docurest/envelope/email_settings.rb index b8171a4..c641b1a 100644 --- a/lib/docurest/envelope/email_settings.rb +++ b/lib/docurest/envelope/email_settings.rb @@ -1,14 +1,48 @@ module Docurest class Envelope::EmailSettings < Docurest::Base + field :envelope_id field :name, :replyEmailNameOverride field :email, :replyEmailAddressOverride field :bcc, :bccEmailAddresses + field :persisted + + def delete + return unless envelope_id + Docurest.client.delete url, parse_json: false + end + + def fetch + return self unless envelope_id + result = Docurest.client.get url + + unless result.key?("errorCode") + self.attributes = result + self.persisted = true + end + + self + end + + def save + return unless envelope_id + if persisted? + Docurest.client.put url, to_h + else + Docurest.client.post url, to_h + end + end def to_h { - replyEmailAddressOverride: replyEmailNameOverride, + replyEmailAddressOverride: replyEmailAddressOverride, replyEmailNameOverride: replyEmailNameOverride } end + + private + + def url + "/envelopes/#{envelope_id}/email_settings" + end end end diff --git a/lib/docurest/envelope/recipient.rb b/lib/docurest/envelope/recipient.rb index 2ebbbf8..47060d6 100644 --- a/lib/docurest/envelope/recipient.rb +++ b/lib/docurest/envelope/recipient.rb @@ -12,6 +12,13 @@ module Docurest end end + def self.save(recipients) + Array(recipients).group_by(&:envelope_id).each do |envelope_id, array| + next unless envelope_id + Docurest.client.put "/envelopes/#{envelope_id}/recipients", Docurest::Base.hash_by_type(array) + end + end + field :id, :recipientId, :integer field :guid, :recipientIdGuid field :envelope_id @@ -24,11 +31,26 @@ module Docurest field :type field :signed_at, :signedDateTime, :date_time field :delivered_at, :deliveredDateTime, :date_time + field :email_notification, :emailNotification, ->(value) { Docurest::Envelope::Email.new value } association(:tabs) { Docurest::Envelope::Recipient::Tab.list(envelope_id, guid) } + def initialize(attributes = {}) + @type = :signers + super + end + def to_h - {recipientId: recipientId} + { + recipientId: recipientId, + name: name, + email: email, + customFields: customFields, + emailNotification: email_notification.to_h, + routingOrder: routingOrder, + note: note, + tabs: Docurest::Base.hash_by_type(tabs), + } end end end diff --git a/lib/docurest/envelope/recipient/tab.rb b/lib/docurest/envelope/recipient/tab.rb index 68364b9..5ad8bc5 100644 --- a/lib/docurest/envelope/recipient/tab.rb +++ b/lib/docurest/envelope/recipient/tab.rb @@ -10,9 +10,11 @@ module Docurest field :guid field :name + field :type field :tab_label, :tabLabel field :scale_value, :scaleValue, :float field :optional, nil, :boolean + field :required, nil, :boolean field :document_id, :documentId field :page_number, :pageNumber, :integer field :recipient_guid, :recipientId @@ -23,6 +25,24 @@ module Docurest field :anchor_x_offset, :anchorXOffset field :anchor_y_offset, :anchorYOffset field :anchor_units, :anchorUnits + + def to_h + { + name: name, + tabLabel: tabLabel, + scaleValue: scaleValue, + optional: optional, + required: required, + documentId: documentId, + pageNumber: pageNumber, + xPosition: xPosition, + yPosition: yPosition, + anchorString: anchorString, + anchorXOffset: anchorXOffset, + anchorYOffset: anchorYOffset, + anchorUnits: anchorUnits, + } + end end end diff --git a/lib/docurest/field.rb b/lib/docurest/field.rb index e4ccad4..6e9423b 100644 --- a/lib/docurest/field.rb +++ b/lib/docurest/field.rb @@ -30,11 +30,23 @@ module Docurest end end - def association(klass, &block) + def association(klass, parent_field = nil, &block) + singular = klass[-1] == 's' ? klass[0..-2] : klass + define_method(klass) do instance_variable_get(:"@#{klass}") || instance_variable_set(:"@#{klass}", persisted? ? instance_eval(&block) : []) end + define_method("#{klass}=") do |values| + values.each { |value| send "add_#{singular}", value } + end + define_method("add_#{singular}") do |value| + values = send(klass) + + value.send("#{parent_field}=", guid) if parent_field + value.id = values.length + 1 unless value.id + values << value + end end end end diff --git a/lib/docurest/login.rb b/lib/docurest/login.rb index 3e23387..9adfd42 100644 --- a/lib/docurest/login.rb +++ b/lib/docurest/login.rb @@ -1,9 +1,8 @@ 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 + result = Docurest.client.get '/login_information', use_base_url: false, + api_password: api_password, include_account_id_guid: guid, login_settings: settings { api_password: result['apiPassword'],