Class: Barcoder::PDF

Inherits:
Base
  • Object
show all
Defined in:
lib/barcoder/pdf.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#attributes=, #initialize

Constructor Details

This class inherits a constructor from Barcoder::Base

Instance Attribute Details

#bucketObject

Returns the value of attribute bucket.



3
4
5
# File 'lib/barcoder/pdf.rb', line 3

def bucket
  @bucket
end

#dataObject

Returns the value of attribute data.



3
4
5
# File 'lib/barcoder/pdf.rb', line 3

def data
  @data
end

#idObject

Returns the value of attribute id.



3
4
5
# File 'lib/barcoder/pdf.rb', line 3

def id
  @id
end

#messagesObject

Messages is an array of barcoded messages that are encoded onto each page as a barcode.



45
46
47
# File 'lib/barcoder/pdf.rb', line 45

def messages
  @messages
end

#missing_pages_countObject

Returns the value of attribute missing_pages_count.



3
4
5
# File 'lib/barcoder/pdf.rb', line 3

def missing_pages_count
  @missing_pages_count
end

#pagesObject

Returns the value of attribute pages.



3
4
5
# File 'lib/barcoder/pdf.rb', line 3

def pages
  @pages
end

#pdf_contentObject

Returns the value of attribute pdf_content.



3
4
5
# File 'lib/barcoder/pdf.rb', line 3

def pdf_content
  @pdf_content
end

#tokenObject

Returns the value of attribute token.



3
4
5
# File 'lib/barcoder/pdf.rb', line 3

def token
  @token
end

#total_pages_countObject

Returns the value of attribute total_pages_count.



3
4
5
# File 'lib/barcoder/pdf.rb', line 3

def total_pages_count
  @total_pages_count
end

Class Method Details

.barcode_uriObject



8
9
10
# File 'lib/barcoder/pdf.rb', line 8

def self.barcode_uri
  "#{Barcoder::Config.host}/barcode"
end

.decode_from_file(bucket, filename) ⇒ Object



26
27
28
# File 'lib/barcoder/pdf.rb', line 26

def self.decode_from_file(bucket, filename)
  decode_from_string(bucket, File.read(filename))
end

.decode_from_string(bucket, string) ⇒ Object



30
31
32
33
34
35
36
# File 'lib/barcoder/pdf.rb', line 30

def self.decode_from_string(bucket, string)
  encoded = Base64.encode64(string)

  opt = {body: {pdf_content: encoded, token: bucket.token}, method: :post}
  response = Typhoeus::Request.new(decode_uri, opt).run
  response.success?
end

.decode_uriObject



12
13
14
# File 'lib/barcoder/pdf.rb', line 12

def self.decode_uri
  "#{Barcoder::Config.host}/decode"
end

.get(bucket, id) ⇒ Object



21
22
23
24
# File 'lib/barcoder/pdf.rb', line 21

def self.get(bucket, id)
  response = get_barcode_request(bucket, id).run
  Barcoder::PDF.new(response_to_attr(response))
end

.new_from_file(filename, attr = {}) ⇒ Object



16
17
18
19
# File 'lib/barcoder/pdf.rb', line 16

def self.new_from_file(filename, attr={})
  pdf_attr = attr.merge(pdf_content: read_file(filename))
  Barcoder::PDF.new pdf_attr
end

Instance Method Details

#barcode!Object

Send file to be barcoded.

Updates pdf_content with the barcoded version of the PDF.



56
57
58
59
60
61
62
# File 'lib/barcoder/pdf.rb', line 56

def barcode!
  return false unless valid?

  barcode_request.run

  true
end

#queue_barcodeObject

Queues the barcode request on hydra.

Queued reqeusts are run in parallel by calling:

Barcoder::Config.hydra.run


68
69
70
71
72
73
74
# File 'lib/barcoder/pdf.rb', line 68

def queue_barcode
  return false unless valid?

  Barcoder::Config.hydra.queue barcode_request

  true
end

#write_to_file(filename) ⇒ Object

Writes PDF content to filename TODO: test



78
79
80
81
82
83
# File 'lib/barcoder/pdf.rb', line 78

def write_to_file(filename)
  f = File.new(filename, 'w', encoding: 'ascii-8bit')
  f.write pdf_content
  f.close
  true
end