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
  id
  meta
  payload
  preview_url
  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.



48
49
50
51
52
# File 'lib/pdfmonkey/document.rb', line 48

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.



30
31
32
# File 'lib/pdfmonkey/document.rb', line 30

def attributes
  @attributes
end

Class Method Details

.generate(template_id, payload) ⇒ Object



39
40
41
42
43
44
45
46
# File 'lib/pdfmonkey/document.rb', line 39

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

  document.send(:save)
end

.generate!(document_template_id, payload) ⇒ Object



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

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

Instance Method Details

#done?Boolean

Returns:

  • (Boolean)


54
55
56
# File 'lib/pdfmonkey/document.rb', line 54

def done?
  COMPLETE_STATUSES.include?(status)
end

#reload!Object



58
59
60
61
62
# File 'lib/pdfmonkey/document.rb', line 58

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

#to_jsonObject



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

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

  { document: attrs }.to_json
end