Class: WebMerge::Document
- Inherits:
-
Object
- Object
- WebMerge::Document
- Includes:
- ActiveModel::Validations
- Defined in:
- lib/web_merge/document.rb
Instance Attribute Summary collapse
-
#active ⇒ Object
readonly
Returns the value of attribute active.
-
#file_path ⇒ Object
Returns the value of attribute file_path.
-
#flatten ⇒ Object
Returns the value of attribute flatten.
-
#id ⇒ Object
readonly
Returns the value of attribute id.
-
#key ⇒ Object
readonly
Returns the value of attribute key.
-
#name ⇒ Object
Returns the value of attribute name.
-
#notification ⇒ Object
Returns the value of attribute notification.
-
#output ⇒ Object
Returns the value of attribute output.
-
#output_name ⇒ Object
Returns the value of attribute output_name.
-
#size ⇒ Object
readonly
Returns the value of attribute size.
-
#size_height ⇒ Object
Returns the value of attribute size_height.
-
#size_width ⇒ Object
Returns the value of attribute size_width.
-
#type ⇒ Object
Returns the value of attribute type.
-
#url ⇒ Object
readonly
Returns the value of attribute url.
Class Method Summary collapse
- .all(client: required(:client)) ⇒ Object
- .each ⇒ Object
- .find(doc_id, client: required(:client)) ⇒ Object
Instance Method Summary collapse
- #as_form_data ⇒ Object
- #delete ⇒ Object
- #field_names ⇒ Object
- #fields ⇒ Object
- #html? ⇒ Boolean
-
#initialize(client: required(:client), name: required(:name), type: required(:type), output: WebMerge::Constants::PDF, file_path: nil, options: {}) ⇒ Document
constructor
A new instance of Document.
- #merge(field_mappings, options = {}, &block) ⇒ Object
- #new_document? ⇒ Boolean
- #reload ⇒ Object
- #save ⇒ Object
- #save! ⇒ Object
Constructor Details
#initialize(client: required(:client), name: required(:name), type: required(:type), output: WebMerge::Constants::PDF, file_path: nil, options: {}) ⇒ Document
Returns a new instance of Document.
16 17 18 19 20 21 22 23 24 25 |
# 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 = [:output_name] @size_width = [:size_width] @size_height = [:size_height] end |
Instance Attribute Details
#active ⇒ Object
Returns the value of attribute active.
9 10 11 |
# File 'lib/web_merge/document.rb', line 9 def active @active end |
#file_path ⇒ Object
Returns the value of attribute file_path.
8 9 10 |
# File 'lib/web_merge/document.rb', line 8 def file_path @file_path end |
#flatten ⇒ Object
Returns the value of attribute flatten.
8 9 10 |
# File 'lib/web_merge/document.rb', line 8 def flatten @flatten end |
#id ⇒ Object
Returns the value of attribute id.
9 10 11 |
# File 'lib/web_merge/document.rb', line 9 def id @id end |
#key ⇒ Object
Returns the value of attribute key.
9 10 11 |
# File 'lib/web_merge/document.rb', line 9 def key @key end |
#name ⇒ Object
Returns the value of attribute name.
8 9 10 |
# File 'lib/web_merge/document.rb', line 8 def name @name end |
#notification ⇒ Object
Returns the value of attribute notification.
8 9 10 |
# File 'lib/web_merge/document.rb', line 8 def notification @notification end |
#output ⇒ Object
Returns the value of attribute output.
8 9 10 |
# File 'lib/web_merge/document.rb', line 8 def output @output end |
#output_name ⇒ Object
Returns the value of attribute output_name.
8 9 10 |
# File 'lib/web_merge/document.rb', line 8 def output_name @output_name end |
#size ⇒ Object
Returns the value of attribute size.
9 10 11 |
# File 'lib/web_merge/document.rb', line 9 def size @size end |
#size_height ⇒ Object
Returns the value of attribute size_height.
8 9 10 |
# File 'lib/web_merge/document.rb', line 8 def size_height @size_height end |
#size_width ⇒ Object
Returns the value of attribute size_width.
8 9 10 |
# File 'lib/web_merge/document.rb', line 8 def size_width @size_width end |
#type ⇒ Object
Returns the value of attribute type.
8 9 10 |
# File 'lib/web_merge/document.rb', line 8 def type @type end |
#url ⇒ Object
Returns the value of attribute url.
9 10 11 |
# File 'lib/web_merge/document.rb', line 9 def url @url end |
Class Method Details
.all(client: required(:client)) ⇒ Object
39 40 41 42 43 44 45 |
# 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 ⇒ Object
33 34 35 36 37 |
# 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)) ⇒ Object
27 28 29 30 31 |
# 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 |
Instance Method Details
#as_form_data ⇒ Object
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
# 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 ⇒ Object
79 80 81 82 83 |
# 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 ⇒ Object
90 91 92 |
# File 'lib/web_merge/document.rb', line 90 def field_names fields.map { |field| field["name"] } end |
#fields ⇒ Object
85 86 87 88 |
# 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? ⇒ Boolean
47 48 49 |
# File 'lib/web_merge/document.rb', line 47 def html? type == WebMerge::Constants::HTML end |
#merge(field_mappings, options = {}, &block) ⇒ Object
94 95 96 97 |
# File 'lib/web_merge/document.rb', line 94 def merge(field_mappings, = {}, &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, , &block) end |
#new_document? ⇒ Boolean
51 52 53 |
# File 'lib/web_merge/document.rb', line 51 def new_document? id.blank? end |
#reload ⇒ Object
72 73 74 75 76 77 |
# 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 ⇒ Object
55 56 57 58 59 60 61 62 63 64 65 66 |
# 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! ⇒ Object
68 69 70 |
# File 'lib/web_merge/document.rb', line 68 def save! raise "Document contains errors: #{errors..join(", ")}" unless save end |