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.
-
#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
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.
13 14 15 16 17 18 19 20 21 22 |
# File 'lib/web_merge/document.rb', line 13 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.
6 7 8 |
# File 'lib/web_merge/document.rb', line 6 def active @active end |
#file_path ⇒ Object
Returns the value of attribute file_path.
5 6 7 |
# File 'lib/web_merge/document.rb', line 5 def file_path @file_path end |
#id ⇒ Object
Returns the value of attribute id.
6 7 8 |
# File 'lib/web_merge/document.rb', line 6 def id @id end |
#key ⇒ Object
Returns the value of attribute key.
6 7 8 |
# File 'lib/web_merge/document.rb', line 6 def key @key end |
#name ⇒ Object
Returns the value of attribute name.
5 6 7 |
# File 'lib/web_merge/document.rb', line 5 def name @name end |
#notification ⇒ Object
Returns the value of attribute notification.
5 6 7 |
# File 'lib/web_merge/document.rb', line 5 def notification @notification end |
#output ⇒ Object
Returns the value of attribute output.
5 6 7 |
# File 'lib/web_merge/document.rb', line 5 def output @output end |
#output_name ⇒ Object
Returns the value of attribute output_name.
5 6 7 |
# File 'lib/web_merge/document.rb', line 5 def output_name @output_name end |
#size ⇒ Object
Returns the value of attribute size.
6 7 8 |
# File 'lib/web_merge/document.rb', line 6 def size @size end |
#size_height ⇒ Object
Returns the value of attribute size_height.
5 6 7 |
# File 'lib/web_merge/document.rb', line 5 def size_height @size_height end |
#size_width ⇒ Object
Returns the value of attribute size_width.
5 6 7 |
# File 'lib/web_merge/document.rb', line 5 def size_width @size_width end |
#type ⇒ Object
Returns the value of attribute type.
5 6 7 |
# File 'lib/web_merge/document.rb', line 5 def type @type end |
#url ⇒ Object
Returns the value of attribute url.
6 7 8 |
# File 'lib/web_merge/document.rb', line 6 def url @url end |
Class Method Details
.all ⇒ Object
36 37 38 39 40 41 42 |
# File 'lib/web_merge/document.rb', line 36 def self.all @client.get_documents.map do |doc_hash| instance = empty_instance instance.send(:update_instance, doc_hash) instance end end |
.each ⇒ Object
30 31 32 33 34 |
# File 'lib/web_merge/document.rb', line 30 def self.each all.each do |doc| yield doc if block_given? end end |
.find(doc_id) ⇒ Object
24 25 26 27 28 |
# File 'lib/web_merge/document.rb', line 24 def self.find(doc_id) instance = empty_instance instance.send(:id=, doc_id) instance.reload end |
Instance Method Details
#as_form_data ⇒ Object
94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/web_merge/document.rb', line 94 def as_form_data request_params = { name: name, type: type, output: output } [: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) merge_notification!(request_params) if notification.present? request_params end |
#delete ⇒ Object
74 75 76 77 78 |
# File 'lib/web_merge/document.rb', line 74 def delete delete = false @client.delete_document(id) { |response| delete = JSON(response.body)["success"] } unless new_document? delete end |
#field_names ⇒ Object
85 86 87 |
# File 'lib/web_merge/document.rb', line 85 def field_names fields.map { |field| field["name"] } end |
#fields ⇒ Object
80 81 82 83 |
# File 'lib/web_merge/document.rb', line 80 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
44 45 46 |
# File 'lib/web_merge/document.rb', line 44 def html? type == WebMerge::Constants::HTML end |
#merge(field_mappings, options = {}, &block) ⇒ Object
89 90 91 92 |
# File 'lib/web_merge/document.rb', line 89 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
48 49 50 |
# File 'lib/web_merge/document.rb', line 48 def new_document? id.blank? end |
#reload ⇒ Object
67 68 69 70 71 72 |
# File 'lib/web_merge/document.rb', line 67 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
52 53 54 55 56 57 58 59 60 61 |
# File 'lib/web_merge/document.rb', line 52 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 update_instance(response.symbolize_keys) true end |
#save! ⇒ Object
63 64 65 |
# File 'lib/web_merge/document.rb', line 63 def save! raise "Document contains errors: #{errors.full_messages.join(", ")}" unless save end |