labelmaker: Stop calling back into spejstore, rename API methods
parent
11615a2bc2
commit
81552ceff2
|
@ -58,4 +58,4 @@ DEPENDENCIES
|
|||
sinatra
|
||||
|
||||
BUNDLED WITH
|
||||
1.17.2
|
||||
2.1.4
|
||||
|
|
|
@ -44,10 +44,6 @@ SPEJSTORE_BASE_URL = ENV['SPEJSTORE_BASE_URL'] || 'https://inventory.waw.hackers
|
|||
QR_BASE_URL = ENV['QR_BASE_URL'] || 'HTTP://I/'
|
||||
LPR_CMD_PATH = ENV['LPR_CMD_PATH'] || 'lpr'
|
||||
|
||||
def api(uri)
|
||||
Excon.get(SPEJSTORE_BASE_URL + uri + "/").json!
|
||||
end
|
||||
|
||||
def render_identicode(data, id, extent)
|
||||
pts = [[0, 0], [0, 1], [1, 1], [1, 0], [0, 0]]
|
||||
|
||||
|
@ -75,9 +71,7 @@ DYMO_11354_LABEL_SIZE = [32, 57]
|
|||
DYMO_LABEL_SIZE = [89, 36]
|
||||
ZEBRA_LABEL_SIZE = [100, 60]
|
||||
|
||||
def render_label(item_or_label_id, size: DYMO_LABEL_SIZE)
|
||||
item = api("items/#{item_or_label_id}")
|
||||
|
||||
def render_label(name, short_id, metadata_text, size: DYMO_LABEL_SIZE)
|
||||
pdf = Prawn::Document.new(page_size: size.map { |x| mm2pt(x) },
|
||||
margin: [2, 2, 2, 6].map { |x| mm2pt(x) }) do
|
||||
base_dir = File.dirname(__FILE__)
|
||||
|
@ -95,20 +89,17 @@ def render_label(item_or_label_id, size: DYMO_LABEL_SIZE)
|
|||
|
||||
# Right side
|
||||
bounding_box([bounds.right - qr_size, bounds.top], width: qr_size) do
|
||||
print_qr_code QR_BASE_URL + item['short_id'], stroke: false,
|
||||
print_qr_code QR_BASE_URL + short_id, stroke: false,
|
||||
foreground_color: '000000',
|
||||
extent: bounds.width, margin: 0, pos: bounds.top_left
|
||||
|
||||
owner_text = item["owner"] ? "owner: #{item['owner']}\n\n" : ""
|
||||
metadata_text = owner_text # todo: creation date?
|
||||
|
||||
text_box metadata_text,
|
||||
at: [bounds.right - qr_size, -7], size: 8, align: :right, overflow: :shrink_to_fit
|
||||
end
|
||||
|
||||
# Left side
|
||||
bounding_box(bounds.top_left, width: bounds.width - qr_size) do
|
||||
text_box item['name'],
|
||||
text_box name,
|
||||
size: 40, align: :center, valign: :center, width: bounds.width-10,
|
||||
inline_format: true, overflow: :shrink_to_fit, disable_wrap_by_char: true
|
||||
end
|
||||
|
@ -119,24 +110,34 @@ end
|
|||
|
||||
set :bind, '0.0.0.0'
|
||||
|
||||
get '/api/1/preview/:id.pdf' do
|
||||
headers["Content-Type"] = "application/pdf; charset=utf8"
|
||||
render_label params["id"]
|
||||
before do
|
||||
request.body.rewind
|
||||
@json_payload = JSON.parse request.body.read
|
||||
|
||||
# Validate and make available from all handlers
|
||||
@name = @json_payload["name"] || halt(400, "Request JSON needs a 'name' field")
|
||||
@short_id = @json_payload["short_id"] || halt(400, "Request JSON needs a 'short_id' field")
|
||||
@meta_text = @json_payload["meta_text"] || ""
|
||||
end
|
||||
|
||||
get '/api/1/preview/:id.png' do
|
||||
post '/api/1/render/pdf' do
|
||||
|
||||
headers["Content-Type"] = "application/pdf; charset=utf8"
|
||||
render_label(@name, @short_id, @meta_text)
|
||||
end
|
||||
|
||||
post '/api/1/render/png' do
|
||||
headers["Content-Type"] = "image/png"
|
||||
img = Magick::ImageList.new()
|
||||
img = img.from_blob(render_label(params["id"])){ self.density = 200 }.first
|
||||
img = img.from_blob(render_label(@name, @short_id, @meta_text)){ self.density = 200 }.first
|
||||
img.format = 'png'
|
||||
img.background_color = 'white'
|
||||
img.to_blob
|
||||
end
|
||||
|
||||
post '/api/1/print/:id' do
|
||||
puts "dupa a także cycki"
|
||||
post '/api/1/print' do
|
||||
temp = Tempfile.new('labelmaker')
|
||||
temp.write(render_label(params["id"]))
|
||||
temp.write(render_label(@name, @short_id, @meta_text))
|
||||
temp.close
|
||||
cmd = "#{LPR_CMD_PATH} -P DYMO_LabelWriter_450 '#{temp.path}'"
|
||||
system(cmd) || puts("Bruh moment (not printing) - running `#{cmd}` failed")
|
||||
|
|
Loading…
Reference in New Issue