Compare commits

...

6 Commits

Author SHA1 Message Date
radex 97000ad207
fix typo, add health check 2024-01-31 09:24:57 +01:00
radex 4690cd2cf8
change api to take label contents instead of calling back spejstore 2024-01-31 00:04:55 +01:00
radex 52f4a3a77e
bump to ruby 3 and bundler 2 2024-01-30 23:51:23 +01:00
radex d4e219efc6
bump deps 2024-01-30 23:45:58 +01:00
radex ba53281bcb
bump some dependencies 2024-01-30 23:45:22 +01:00
radex bc2d8c3f62
clean up old junk 2024-01-30 23:42:10 +01:00
6 changed files with 55 additions and 97 deletions

1
.ruby-version Normal file
View File

@ -0,0 +1 @@
3.0.2

View File

@ -1,4 +1,4 @@
FROM ruby:2.7
FROM ruby:3.0
RUN apt-get update && \
apt-get install -y cups-ipp-utils && \
@ -6,7 +6,6 @@ RUN apt-get update && \
# throw errors if Gemfile has been modified since Gemfile.lock
RUN bundle config --global frozen 1
RUN gem install bundler:1.17.2
WORKDIR /usr/src/app

View File

@ -1,10 +1,7 @@
source 'https://rubygems.org'
gem 'prawn'
gem 'prawn-qrcode', :git => 'https://github.com/jabbrwcky/prawn-qrcode'
gem 'prawn-svg'
gem 'prawn-qrcode'
gem 'sinatra'
gem 'color'
gem 'excon'
gem 'rmagick'
gem 'json'
gem 'webrick'

View File

@ -1,61 +1,45 @@
GIT
remote: https://github.com/jabbrwcky/prawn-qrcode
revision: 08e457af2d6661ede3346a3a3dc3b99a9df9fd9b
specs:
prawn-qrcode (0.5.1)
prawn (>= 1)
rqrcode (>= 1.0.0)
GEM
remote: https://rubygems.org/
specs:
addressable (2.7.0)
public_suffix (>= 2.0.2, < 5.0)
chunky_png (1.3.11)
color (1.8)
css_parser (1.7.1)
addressable
excon (0.73.0)
json (2.3.0)
mustermann (1.1.1)
base64 (0.2.0)
chunky_png (1.4.0)
json (2.7.1)
mustermann (3.0.0)
ruby2_keywords (~> 0.0.1)
pdf-core (0.7.0)
prawn (2.2.2)
pdf-core (~> 0.7.0)
ttfunk (~> 1.5)
prawn-svg (0.30.0)
css_parser (~> 1.6)
prawn (>= 0.11.1, < 3)
public_suffix (4.0.5)
rack (2.2.2)
rack-protection (2.0.8.1)
rack
rmagick (4.1.2)
rqrcode (1.1.2)
pdf-core (0.9.0)
prawn (2.4.0)
pdf-core (~> 0.9.0)
ttfunk (~> 1.7)
prawn-qrcode (0.5.2)
prawn (>= 1)
rqrcode (>= 1.0.0)
rack (2.2.8)
rack-protection (3.2.0)
base64 (>= 0.1.0)
rack (~> 2.2, >= 2.2.4)
rqrcode (2.2.0)
chunky_png (~> 1.0)
rqrcode_core (~> 0.1)
rqrcode_core (0.1.2)
ruby2_keywords (0.0.2)
sinatra (2.0.8.1)
mustermann (~> 1.0)
rack (~> 2.0)
rack-protection (= 2.0.8.1)
rqrcode_core (~> 1.0)
rqrcode_core (1.2.0)
ruby2_keywords (0.0.5)
sinatra (3.2.0)
mustermann (~> 3.0)
rack (~> 2.2, >= 2.2.4)
rack-protection (= 3.2.0)
tilt (~> 2.0)
tilt (2.0.10)
ttfunk (1.6.2.1)
tilt (2.3.0)
ttfunk (1.7.0)
webrick (1.8.1)
PLATFORMS
ruby
DEPENDENCIES
color
excon
json
prawn
prawn-qrcode!
prawn-svg
rmagick
prawn-qrcode
sinatra
webrick
BUNDLED WITH
1.17.2
2.2.22

View File

@ -7,16 +7,7 @@ bundle exec ruby main.rb
try it out:
GET http://localhost:4567/api/1/preview/:label.png
GET http://localhost:4567/api/1/preview/:label.pdf
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
```
GET http://localhost:4567/api/2/preview.pdf?id=abcdef&name=ItemName&owner=OptionalOwner
POST http://localhost:4567/api/2/print?id=abcdef&name=ItemName&owner=OptionalOwner
Make sure to pass either `LABELMAKER_LOCAL_PRINTER_NAME` or `LABELMAKER_IPP_PRINTER_URL`. See top of `main.rb` for all env variables available.

54
main.rb
View File

@ -1,20 +1,13 @@
require 'rubygems'
require 'sinatra'
require 'rqrcode'
require 'prawn'
require 'prawn/measurements'
require 'prawn/qrcode'
require 'prawn-svg'
require 'color'
require 'excon'
require 'rmagick'
require 'json'
require 'zlib'
require 'shellwords'
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/')
# NOTE:
@ -25,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
LOCAL_PRINTER_NAME = ENV.fetch('LABELMAKER_LOCAL_PRINTER_NAME', 'DYMO_LabelWriter_450')
IPP_PRINTER_URL = ENV.fetch('LABELMAKER_IPP_PRINTER_URL', '')
DEBUG_JSON = ENV["LABELMAKER_DEBUG_JSON"]
def api(uri)
if DEBUG_JSON
JSON.parse(DEBUG_JSON)
else
JSON.parse(Excon.get(BACKEND_URL + uri + "/"))
def render_label()
short_id = params[:id]
name = params[:name]
owner = params[:owner]
if short_id.nil? or name.nil?
status 400
return "Missing required parameters ?id= and ?name="
end
end
def render_label(item_or_label_id, size: LABEL_SIZE)
item = api("items/#{item_or_label_id}")
pdf = Prawn::Document.new(page_size: size.map { |x| mm2pt(x) },
pdf = Prawn::Document.new(page_size: LABEL_SIZE.map { |x| mm2pt(x) },
margin: [2, 2, 2, 6].map { |x| mm2pt(x) }) do
font_families.update("DejaVuSans" => {
normal: "fonts/DejaVuSans.ttf",
@ -54,11 +45,11 @@ def render_label(item_or_label_id, size: LABEL_SIZE)
# Right side
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',
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?
text_box metadata_text,
@ -67,7 +58,7 @@ def render_label(item_or_label_id, size: LABEL_SIZE)
# 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
@ -78,23 +69,18 @@ end
set :bind, '0.0.0.0'
get '/api/1/preview/:id.pdf' do
get '/api/2/health' do
"I'm cool"
end
get '/api/2/preview.pdf' do
headers["Content-Type"] = "application/pdf; charset=utf8"
render_label params["id"]
render_label
end
get '/api/1/preview/:id.png' do
headers["Content-Type"] = "image/png"
img = Magick::ImageList.new()
img = img.from_blob(render_label(params["id"])){ self.density = 200 }.first
img.format = 'png'
img.background_color = 'white'
img.to_blob
end
post '/api/1/print/:id' do
post '/api/2/print' do
temp = Tempfile.new('labelmaker')
temp.write(render_label(params["id"]))
temp.write(render_label)
temp.close
if not LOCAL_PRINTER_NAME.empty?