Class: Pdfmonkey::Document

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/pdfmonkey/document.rb

Constant Summary collapse

ATTRIBUTES =
%i[
  app_id
  checksum
  created_at
  document_template_id
  download_url
  errors
  failure_cause
  filename
  generation_logs
  id
  meta
  payload
  preview_url
  public_share_link
  status
  updated_at
].freeze
COMPLETE_STATUSES =
%w[error failure success].freeze
COLLECTION =
'documents'
MEMBER =
'document'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(adapter: Pdfmonkey::Adapter.new, **attributes) ⇒ Document

Returns a new instance of Document.



61
62
63
64
65
# File 'lib/pdfmonkey/document.rb', line 61

def initialize(adapter: Pdfmonkey::Adapter.new, **attributes)
  @adapter = adapter
  @attributes = OpenStruct.new(ATTRIBUTES.zip([]).to_h)
  update(attributes)
end

Instance Attribute Details

#attributesObject (readonly)

Returns the value of attribute attributes.



34
35
36
# File 'lib/pdfmonkey/document.rb', line 34

def attributes
  @attributes
end

Class Method Details

.delete(document_id) ⇒ Object



37
38
39
# File 'lib/pdfmonkey/document.rb', line 37

def self.delete(document_id)
  new(id: document_id).delete!
end

.fetch(document_id) ⇒ Object



41
42
43
# File 'lib/pdfmonkey/document.rb', line 41

def self.fetch(document_id)
  new(id: document_id).reload!
end

.generate(template_id, payload, meta = {}) ⇒ Object



51
52
53
54
55
56
57
58
59
# File 'lib/pdfmonkey/document.rb', line 51

def self.generate(template_id, payload, meta = {})
  document = new(
    document_template_id: template_id,
    meta: meta.to_json,
    payload: payload.to_json,
    status: 'pending')

  document.send(:save)
end

.generate!(document_template_id, payload, meta = {}) ⇒ Object



45
46
47
48
49
# File 'lib/pdfmonkey/document.rb', line 45

def self.generate!(document_template_id, payload, meta = {})
  document = generate(document_template_id, payload, meta)
  document.reload! until document.done?
  document
end

Instance Method Details

#delete!Object



67
68
69
# File 'lib/pdfmonkey/document.rb', line 67

def delete!
  adapter.call(:delete, self)
end

#done?Boolean

Returns:

  • (Boolean)


71
72
73
# File 'lib/pdfmonkey/document.rb', line 71

def done?
  COMPLETE_STATUSES.include?(status)
end

#reload!Object



75
76
77
78
79
# File 'lib/pdfmonkey/document.rb', line 75

def reload!
  attributes = adapter.call(:get, self)
  update(attributes)
  self
end

#to_jsonObject



81
82
83
84
85
86
# File 'lib/pdfmonkey/document.rb', line 81

def to_json
  attrs = attributes.to_h
  attrs.delete(:errors)

  { document: attrs }.to_json
end