class Barcoder::PDF
Attributes
bucket[RW]
data[RW]
id[RW]
messages[RW]
missing_pages_count[RW]
pages[RW]
pdf_content[RW]
token[RW]
total_pages_count[RW]
Public Class Methods
barcode_uri()
click to toggle source
# File lib/barcoder/pdf.rb, line 8 def self.barcode_uri "#{Barcoder::Config.host}/barcode" end
decode_from_file(bucket, filename)
click to toggle source
# 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)
click to toggle source
# 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_uri()
click to toggle source
# File lib/barcoder/pdf.rb, line 12 def self.decode_uri "#{Barcoder::Config.host}/decode" end
get(bucket, id)
click to toggle source
# 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={})
click to toggle source
# 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
Private Class Methods
get_barcode_request(bucket, id)
click to toggle source
# File lib/barcoder/pdf.rb, line 99 def self.get_barcode_request(bucket, id) opt = {body: {id: id, token: bucket.token}} req = Typhoeus::Request.new(barcode_uri, opt) req end
read_file(filename)
click to toggle source
# File lib/barcoder/pdf.rb, line 109 def self.read_file(filename) f = File.open(filename, 'r', encoding: 'ascii-8bit') content = f.read f.close content end
Public Instance Methods
barcode!()
click to toggle source
Send file to be barcoded.
Updates pdf_content
with the barcoded version of the PDF
.
# File lib/barcoder/pdf.rb, line 56 def barcode! return false unless valid? barcode_request.run true end
queue_barcode()
click to toggle source
Queues the barcode request on hydra.
Queued reqeusts are run in parallel by calling:
Barcoder::Config.hydra.run
# 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)
click to toggle source
Writes PDF
content to filename TODO: test
# 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
Private Instance Methods
barcode_request()
click to toggle source
# File lib/barcoder/pdf.rb, line 87 def barcode_request opt = {body: save_attr.merge(token: token), method: :post} req = Typhoeus::Request.new(self.class.barcode_uri, opt) req.on_success do |response| process_save_response response end req end
process_save_response(response)
click to toggle source
# File lib/barcoder/pdf.rb, line 116 def process_save_response(response) data = response_to_json(response).with_indifferent_access data[:pdf_content] = Base64.decode64(data[:pdf_content]) self.attributes = data end
save_attr()
click to toggle source
# File lib/barcoder/pdf.rb, line 105 def save_attr {data: data, pdf_content: Base64.encode64(pdf_content)} end