change api to take label contents instead of calling back spejstore
This commit is contained in:
parent
52f4a3a77e
commit
4690cd2cf8
4 changed files with 18 additions and 34 deletions
1
Gemfile
1
Gemfile
|
@ -3,6 +3,5 @@ source 'https://rubygems.org'
|
|||
gem 'prawn'
|
||||
gem 'prawn-qrcode'
|
||||
gem 'sinatra'
|
||||
gem 'excon'
|
||||
gem 'json'
|
||||
gem 'webrick'
|
||||
|
|
|
@ -3,7 +3,6 @@ GEM
|
|||
specs:
|
||||
base64 (0.2.0)
|
||||
chunky_png (1.4.0)
|
||||
excon (0.109.0)
|
||||
json (2.7.1)
|
||||
mustermann (3.0.0)
|
||||
ruby2_keywords (~> 0.0.1)
|
||||
|
@ -36,7 +35,6 @@ PLATFORMS
|
|||
ruby
|
||||
|
||||
DEPENDENCIES
|
||||
excon
|
||||
json
|
||||
prawn
|
||||
prawn-qrcode
|
||||
|
|
13
README.md
13
README.md
|
@ -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.
|
||||
|
|
36
main.rb
36
main.rb
|
@ -3,13 +3,11 @@ require 'sinatra'
|
|||
require 'prawn'
|
||||
require 'prawn/measurements'
|
||||
require 'prawn/qrcode'
|
||||
require 'excon'
|
||||
require 'json'
|
||||
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:
|
||||
|
@ -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
|
||||
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",
|
||||
|
@ -49,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,
|
||||
|
@ -62,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
|
||||
|
@ -73,14 +69,14 @@ end
|
|||
|
||||
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"
|
||||
render_label params["id"]
|
||||
render_label
|
||||
end
|
||||
|
||||
post '/api/1/print/:id' do
|
||||
post '/api/1/print' do
|
||||
temp = Tempfile.new('labelmaker')
|
||||
temp.write(render_label(params["id"]))
|
||||
temp.write(render_label)
|
||||
temp.close
|
||||
|
||||
if not LOCAL_PRINTER_NAME.empty?
|
||||
|
|
Loading…
Reference in a new issue