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.



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

Instance Attribute Details

#activeObject

Returns the value of attribute active.



9
10
11
# File 'lib/web_merge/document.rb', line 9

def active
  @active
end

#file_pathObject

Returns the value of attribute file_path.



8
9
10
# File 'lib/web_merge/document.rb', line 8

def file_path
  @file_path
end

#flattenObject

Returns the value of attribute flatten.



8
9
10
# File 'lib/web_merge/document.rb', line 8

def flatten
  @flatten
end

#idObject

Returns the value of attribute id.



9
10
11
# File 'lib/web_merge/document.rb', line 9

def id
  @id
end

#keyObject

Returns the value of attribute key.



9
10
11
# File 'lib/web_merge/document.rb', line 9

def key
  @key
end

#nameObject

Returns the value of attribute name.



8
9
10
# File 'lib/web_merge/document.rb', line 8

def name
  @name
end

#notificationObject

Returns the value of attribute notification.



8
9
10
# File 'lib/web_merge/document.rb', line 8

def notification
  @notification
end

#outputObject

Returns the value of attribute output.



8
9
10
# File 'lib/web_merge/document.rb', line 8

def output
  @output
end

#output_nameObject

Returns the value of attribute output_name.



8
9
10
# File 'lib/web_merge/document.rb', line 8

def output_name
  @output_name
end

#sizeObject

Returns the value of attribute size.



9
10
11
# File 'lib/web_merge/document.rb', line 9

def size
  @size
end

#size_heightObject

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_widthObject

Returns the value of attribute size_width.



8
9
10
# File 'lib/web_merge/document.rb', line 8

def size_width
  @size_width
end

#typeObject

Returns the value of attribute type.



8
9
10
# File 'lib/web_merge/document.rb', line 8

def type
  @type
end

#urlObject

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

.eachObject



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_dataObject



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

#deleteObject



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_namesObject



90
91
92
# File 'lib/web_merge/document.rb', line 90

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

#fieldsObject



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

Returns:

  • (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, 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)


51
52
53
# File 'lib/web_merge/document.rb', line 51

def new_document?
  id.blank?
end

#reloadObject



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

#saveObject



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.full_messages.join(", ")}" unless save
end