class WebMerge::Document
Attributes
active[RW]
file_path[RW]
flatten[RW]
id[RW]
key[RW]
name[RW]
notification[RW]
output[RW]
output_name[RW]
size[RW]
size_height[RW]
size_width[RW]
type[RW]
url[RW]
Public Class Methods
all(client: required(:client))
click to toggle source
# File lib/web_merge/document.rb, line 39 def self.all(client: required(:client)) client.get_documents.map do |doc_hash| instance = empty_instance(client) instance.send(:update_instance, doc_hash) instance end end
each() { |doc| ... }
click to toggle source
# File lib/web_merge/document.rb, line 33 def self.each all.each do |doc| yield doc if block_given? end end
find(doc_id, client: required(:client))
click to toggle source
# File lib/web_merge/document.rb, line 27 def self.find(doc_id, client: required(:client)) instance = empty_instance(client) instance.send(:id=, doc_id) instance.reload end
new(client: required(:client), name: required(:name), type: required(:type), output: WebMerge::Constants::PDF, file_path: nil, options: {})
click to toggle source
# File lib/web_merge/document.rb, line 16 def initialize(client: required(:client), name: required(:name), type: required(:type), output: WebMerge::Constants::PDF, file_path: nil, options: {}) @client = client @name = name @type = type @output = output @file_path = file_path @output_name = options[:output_name] @size_width = options[:size_width] @size_height = options[:size_height] end
Private Class Methods
empty_instance(client)
click to toggle source
# File lib/web_merge/document.rb, line 120 def self.empty_instance(client) new(client: client, name: "", type: "", output: "") end
Public Instance Methods
as_form_data()
click to toggle source
# File lib/web_merge/document.rb, line 99 def as_form_data request_params = { name: name, type: type, output: output } request_params["settings[flatten]"] = flatten if flatten [:output_name, :size_width, :size_height].each do |key| value = send(key) request_params.merge!(key => value) if value.present? end merge_file_contents!(request_params) if file_path.present? merge_notification!(request_params) if notification.present? request_params end
delete()
click to toggle source
# File lib/web_merge/document.rb, line 79 def delete delete = false @client.delete_document(id) { |response| delete = JSON(response.body)["success"] } unless new_document? delete end
field_names()
click to toggle source
# File lib/web_merge/document.rb, line 90 def field_names fields.map { |field| field["name"] } end
fields()
click to toggle source
# File lib/web_merge/document.rb, line 85 def fields raise "Cannot fetch fields for an unpersisted document, perhaps you'd like to call `save' first?" if new_document? @fields ||= @client.get_document_fields(id) end
html?()
click to toggle source
# File lib/web_merge/document.rb, line 47 def html? type == WebMerge::Constants::HTML end
merge(field_mappings, options = {}, &block)
click to toggle source
# File lib/web_merge/document.rb, line 94 def merge(field_mappings, options = {}, &block) raise "Cannot merge an unpersisted document, perhaps you'd like to call `save' first?" if new_document? @client.merge_document(id, key, field_mappings, options, &block) end
new_document?()
click to toggle source
# File lib/web_merge/document.rb, line 51 def new_document? id.blank? end
reload()
click to toggle source
# File lib/web_merge/document.rb, line 72 def reload raise "Cannot reload a new document, perhaps you'd like to call `save' first?" if new_document? response = @client.get_document(id) update_instance(response) self end
save()
click to toggle source
# File lib/web_merge/document.rb, line 55 def save return false unless valid? response = if new_document? @client.create_document(as_form_data) else @client.update_document(id, as_form_data) end raise WebMerge::DocumentError.new(response['error']) if response['error'].present? update_instance(response.symbolize_keys) true end
save!()
click to toggle source
# File lib/web_merge/document.rb, line 68 def save! raise "Document contains errors: #{errors.full_messages.join(", ")}" unless save end
Private Instance Methods
merge_file_contents!(request_params)
click to toggle source
# File lib/web_merge/document.rb, line 130 def merge_file_contents!(request_params) if html? html_string = IO.binread(file_path) request_params.merge!(html: html_string) else encoded = Base64.encode64(IO.binread(file_path)) request_params.merge!(file_contents: encoded) end request_params end
merge_notification!(request_params)
click to toggle source
# File lib/web_merge/document.rb, line 141 def merge_notification!(request_params) notification.as_form_data.each_pair do |key, value| request_params.merge!("notification[#{key}]" => value) end end
update_instance(response)
click to toggle source
# File lib/web_merge/document.rb, line 124 def update_instance(response) response.each_pair do |key, value| send("#{key}=".to_sym, value) if respond_to?("#{key}=".to_sym, true) end end