change api to take label contents instead of calling back spejstore
parent
52f4a3a77e
commit
4690cd2cf8
1
Gemfile
1
Gemfile
|
@ -3,6 +3,5 @@ source 'https://rubygems.org'
|
||||||
gem 'prawn'
|
gem 'prawn'
|
||||||
gem 'prawn-qrcode'
|
gem 'prawn-qrcode'
|
||||||
gem 'sinatra'
|
gem 'sinatra'
|
||||||
gem 'excon'
|
|
||||||
gem 'json'
|
gem 'json'
|
||||||
gem 'webrick'
|
gem 'webrick'
|
||||||
|
|
|
@ -3,7 +3,6 @@ GEM
|
||||||
specs:
|
specs:
|
||||||
base64 (0.2.0)
|
base64 (0.2.0)
|
||||||
chunky_png (1.4.0)
|
chunky_png (1.4.0)
|
||||||
excon (0.109.0)
|
|
||||||
json (2.7.1)
|
json (2.7.1)
|
||||||
mustermann (3.0.0)
|
mustermann (3.0.0)
|
||||||
ruby2_keywords (~> 0.0.1)
|
ruby2_keywords (~> 0.0.1)
|
||||||
|
@ -36,7 +35,6 @@ PLATFORMS
|
||||||
ruby
|
ruby
|
||||||
|
|
||||||
DEPENDENCIES
|
DEPENDENCIES
|
||||||
excon
|
|
||||||
json
|
json
|
||||||
prawn
|
prawn
|
||||||
prawn-qrcode
|
prawn-qrcode
|
||||||
|
|
13
README.md
13
README.md
|
@ -7,16 +7,7 @@ bundle exec ruby main.rb
|
||||||
|
|
||||||
try it out:
|
try it out:
|
||||||
|
|
||||||
GET http://localhost:4567/api/1/preview/:label.png
|
GET http://localhost:4567/api/2/preview.pdf?id=abcdef&name=ItemName&owner=OptionalOwner
|
||||||
GET http://localhost:4567/api/1/preview/:label.pdf
|
POST http://localhost:4567/api/2/print?id=abcdef&name=ItemName&owner=OptionalOwner
|
||||||
POST http://localhost:4567/api/1/print/:label
|
|
||||||
|
|
||||||
where :label is a `spejstore` label.id or item.short_id
|
|
||||||
|
|
||||||
to test without spejstore running locally, pass:
|
|
||||||
|
|
||||||
```sh
|
|
||||||
LABELMAKER_DEBUG_JSON='{"short_id":"abcdef","name":"Some long test item","owner":"testowner"}' bundle exec ruby main.rb
|
|
||||||
```
|
|
||||||
|
|
||||||
Make sure to pass either `LABELMAKER_LOCAL_PRINTER_NAME` or `LABELMAKER_IPP_PRINTER_URL`. See top of `main.rb` for all env variables available.
|
Make sure to pass either `LABELMAKER_LOCAL_PRINTER_NAME` or `LABELMAKER_IPP_PRINTER_URL`. See top of `main.rb` for all env variables available.
|
||||||
|
|
36
main.rb
36
main.rb
|
@ -3,13 +3,11 @@ require 'sinatra'
|
||||||
require 'prawn'
|
require 'prawn'
|
||||||
require 'prawn/measurements'
|
require 'prawn/measurements'
|
||||||
require 'prawn/qrcode'
|
require 'prawn/qrcode'
|
||||||
require 'excon'
|
|
||||||
require 'json'
|
require 'json'
|
||||||
require 'shellwords'
|
require 'shellwords'
|
||||||
|
|
||||||
include Prawn::Measurements
|
include Prawn::Measurements
|
||||||
|
|
||||||
BACKEND_URL = ENV.fetch('LABELMAKER_BACKEND_URL', 'https://inventory.hackerspace.pl/api/1/')
|
|
||||||
CODE_PREFIX = ENV.fetch('LABELMAKER_CODE_PREFIX', 'https://inventory.hackerspace.pl/')
|
CODE_PREFIX = ENV.fetch('LABELMAKER_CODE_PREFIX', 'https://inventory.hackerspace.pl/')
|
||||||
|
|
||||||
# NOTE:
|
# NOTE:
|
||||||
|
@ -20,20 +18,18 @@ LABEL_SIZE = JSON.parse(ENV.fetch('LABELMAKER_LABEL_SIZE', '[89, 36]'))
|
||||||
# NOTE: You can use either local printer or IPP printer, but not both
|
# NOTE: You can use either local printer or IPP printer, but not both
|
||||||
LOCAL_PRINTER_NAME = ENV.fetch('LABELMAKER_LOCAL_PRINTER_NAME', 'DYMO_LabelWriter_450')
|
LOCAL_PRINTER_NAME = ENV.fetch('LABELMAKER_LOCAL_PRINTER_NAME', 'DYMO_LabelWriter_450')
|
||||||
IPP_PRINTER_URL = ENV.fetch('LABELMAKER_IPP_PRINTER_URL', '')
|
IPP_PRINTER_URL = ENV.fetch('LABELMAKER_IPP_PRINTER_URL', '')
|
||||||
DEBUG_JSON = ENV["LABELMAKER_DEBUG_JSON"]
|
|
||||||
|
|
||||||
def api(uri)
|
def render_label()
|
||||||
if DEBUG_JSON
|
short_id = params[:id]
|
||||||
JSON.parse(DEBUG_JSON)
|
name = params[:name]
|
||||||
else
|
owner = params[:owner]
|
||||||
JSON.parse(Excon.get(BACKEND_URL + uri + "/"))
|
|
||||||
|
if short_id.nil? or name.nil?
|
||||||
|
status 400
|
||||||
|
return "Missing required parameters ?id= and ?name="
|
||||||
end
|
end
|
||||||
end
|
|
||||||
|
|
||||||
def render_label(item_or_label_id, size: LABEL_SIZE)
|
pdf = Prawn::Document.new(page_size: LABEL_SIZE.map { |x| mm2pt(x) },
|
||||||
item = api("items/#{item_or_label_id}")
|
|
||||||
|
|
||||||
pdf = Prawn::Document.new(page_size: size.map { |x| mm2pt(x) },
|
|
||||||
margin: [2, 2, 2, 6].map { |x| mm2pt(x) }) do
|
margin: [2, 2, 2, 6].map { |x| mm2pt(x) }) do
|
||||||
font_families.update("DejaVuSans" => {
|
font_families.update("DejaVuSans" => {
|
||||||
normal: "fonts/DejaVuSans.ttf",
|
normal: "fonts/DejaVuSans.ttf",
|
||||||
|
@ -49,11 +45,11 @@ def render_label(item_or_label_id, size: LABEL_SIZE)
|
||||||
|
|
||||||
# Right side
|
# Right side
|
||||||
bounding_box([bounds.right - qr_size, bounds.top], width: qr_size) do
|
bounding_box([bounds.right - qr_size, bounds.top], width: qr_size) do
|
||||||
print_qr_code CODE_PREFIX + item['short_id'], stroke: false,
|
print_qr_code CODE_PREFIX + short_id, stroke: false,
|
||||||
foreground_color: '000000',
|
foreground_color: '000000',
|
||||||
extent: bounds.width, margin: 0, pos: bounds.top_left
|
extent: bounds.width, margin: 0, pos: bounds.top_left
|
||||||
|
|
||||||
owner_text = item["owner"] ? "owner: #{item['owner']}\n\n" : ""
|
owner_text = owner && !owner.empty? ? "owner: #{owner}\n\n" : ""
|
||||||
metadata_text = owner_text # todo: creation date?
|
metadata_text = owner_text # todo: creation date?
|
||||||
|
|
||||||
text_box metadata_text,
|
text_box metadata_text,
|
||||||
|
@ -62,7 +58,7 @@ def render_label(item_or_label_id, size: LABEL_SIZE)
|
||||||
|
|
||||||
# Left side
|
# Left side
|
||||||
bounding_box(bounds.top_left, width: bounds.width - qr_size) do
|
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,
|
size: 40, align: :center, valign: :center, width: bounds.width-10,
|
||||||
inline_format: true, overflow: :shrink_to_fit, disable_wrap_by_char: true
|
inline_format: true, overflow: :shrink_to_fit, disable_wrap_by_char: true
|
||||||
end
|
end
|
||||||
|
@ -73,14 +69,14 @@ end
|
||||||
|
|
||||||
set :bind, '0.0.0.0'
|
set :bind, '0.0.0.0'
|
||||||
|
|
||||||
get '/api/1/preview/:id.pdf' do
|
get '/api/2/preview.pdf' do
|
||||||
headers["Content-Type"] = "application/pdf; charset=utf8"
|
headers["Content-Type"] = "application/pdf; charset=utf8"
|
||||||
render_label params["id"]
|
render_label
|
||||||
end
|
end
|
||||||
|
|
||||||
post '/api/1/print/:id' do
|
post '/api/1/print' do
|
||||||
temp = Tempfile.new('labelmaker')
|
temp = Tempfile.new('labelmaker')
|
||||||
temp.write(render_label(params["id"]))
|
temp.write(render_label)
|
||||||
temp.close
|
temp.close
|
||||||
|
|
||||||
if not LOCAL_PRINTER_NAME.empty?
|
if not LOCAL_PRINTER_NAME.empty?
|
||||||
|
|
Loading…
Reference in New Issue