Skip to content

Instantly share code, notes, and snippets.

@codyeatworld
Last active November 18, 2015 12:07
Show Gist options
  • Save codyeatworld/7921583bbbf8b9c6924d to your computer and use it in GitHub Desktop.
Save codyeatworld/7921583bbbf8b9c6924d to your computer and use it in GitHub Desktop.
easypost_printnode_slack.rb
# app/services/add_file_to_printnode_queue.rb
require 'printnode'
class AddFileToPrintnodeQueue
def self.call(file_url, title, printer_name, file_type='pdf_uri')
# Connect to the API and setup a Client object.
auth = PrintNode::Auth.new(ENV['printnode_api_key'])
client = PrintNode::Client.new(auth)
begin
if client.printers.any?
printer = client.printers.find { |printer| printer.name == printer_name }
if printer.nil?
raise "No printer found with name '#{printer_name}'. Available printer names are: #{client.printers.map(&:name).join(', ')}."
end
else
raise 'No printers available.'
end
rescue
# PrintNode will return a json response if the api key is invalid.
raise 'Invalid credentials or timed out.'
end
# Setup a new print job object.
setup = PrintNode::PrintJob.new(
printer.id, # Printer id
title, # A title to give the PrintJob (appears in print queue)
file_type, # Type of file, pdf_uri or raw_uri for .zpl's
file_url, # URL to file
ENV['app_name'] # Source
)
# Send print job to queue.
print_job = client.create_printjob(setup, { expireAfter: 120000 })
# View status of print job.
client.printjobs(print_job)
end
end
# app/controllers/admin/printnode/invoices_controller.rb
class Admin::Printnode::InvoicesController < Admin::AdminController
def print
@invoice = CreatePrintnodeService.call(
invoice_admin_order_fulfillment_url(@order, @fulfillment, format: :pdf),
"FUL##{@fulfillment.guid.split('-')[0]}".upcase,
ENV['invoice_printer']
)
rescue => e
flash.now[:error] = e
end
end
# app/controllers/admin/printnode/labels_controller.rb
class Admin::Printnode::LabelsController < Admin::AdminController
def print
@label = CreatePrintnodeService.call(
@fulfillment.label_zpl_url,
@fulfillment.shipment_id,
ENV['label_printer'],
'raw_uri'
)
rescue => e
flash.now[:error] = e
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment