Class: WebMerge::Document

Inherits:
Object
  • Object
show all
Includes:
ActiveModel::Validations
Defined in:
lib/web_merge/document.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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 = options[:output_name]
  @size_width = options[:size_width]
  @size_height = options[:size_height]
end

Instance Attribute Details

#activeObject

Returns the value of attribute active.



6
7
8
# File 'lib/web_merge/document.rb', line 6

def active
  @active
end

#file_pathObject

Returns the value of attribute file_path.



5
6
7
# File 'lib/web_merge/document.rb', line 5

def file_path
  @file_path
end

#flattenObject

Returns the value of attribute flatten.



5
6
7
# File 'lib/web_merge/document.rb', line 5

def flatten
  @flatten
end

#idObject

Returns the value of attribute id.



6
7
8
# File 'lib/web_merge/document.rb', line 6

def id
  @id
end

#keyObject

Returns the value of attribute key.



6
7
8
# File 'lib/web_merge/document.rb', line 6

def key
  @key
end

#nameObject

Returns the value of attribute name.



5
6
7
# File 'lib/web_merge/document.rb', line 5

def name
  @name
end

#notificationObject

Returns the value of attribute notification.



5
6
7
# File 'lib/web_merge/document.rb', line 5

def notification
  @notification
end

#outputObject

Returns the value of attribute output.



5
6
7
# File 'lib/web_merge/document.rb', line 5

def output
  @output
end

#output_nameObject

Returns the value of attribute output_name.



5
6
7
# File 'lib/web_merge/document.rb', line 5

def output_name
  @output_name
end

#sizeObject

Returns the value of attribute size.



6
7
8
# File 'lib/web_merge/document.rb', line 6

def size
  @size
end

#size_heightObject

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_widthObject

Returns the value of attribute size_width.



5
6
7
# File 'lib/web_merge/document.rb', line 5

def size_width
  @size_width
end

#typeObject

Returns the value of attribute type.



5
6
7
# File 'lib/web_merge/document.rb', line 5

def type
  @type
end

#urlObject

Returns the value of attribute url.



6
7
8
# File 'lib/web_merge/document.rb', line 6

def url
  @url
end

Class Method Details

.allObject



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

.eachObject



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_dataObject



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/web_merge/document.rb', line 94

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

#deleteObject



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_namesObject



85
86
87
# File 'lib/web_merge/document.rb', line 85

def field_names
  fields.map { |field| field["name"] }
end

#fieldsObject



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

Returns:

  • (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, 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?Boolean

Returns:

  • (Boolean)


48
49
50
# File 'lib/web_merge/document.rb', line 48

def new_document?
  id.blank?
end

#reloadObject



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

#saveObject



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