refactor
This commit is contained in:
parent
a2137e1f8b
commit
7e0be269af
129
main.rb
129
main.rb
|
@ -40,6 +40,20 @@ end
|
||||||
BACKEND_URL = 'http://192.168.75.155:8000/api/1/'
|
BACKEND_URL = 'http://192.168.75.155:8000/api/1/'
|
||||||
CODE_PREFIX = "HTTP://I/"
|
CODE_PREFIX = "HTTP://I/"
|
||||||
|
|
||||||
|
|
||||||
|
templates = {
|
||||||
|
"prywatne" => { "name" => "Ten przedmiot jest własnością prywatną", "image" => "assets/person.svg"},
|
||||||
|
"hsowe" => { "name" => "Ten przedmiot należy do HSWro", "image" => "assets/hswro.svg"},
|
||||||
|
"hackuj" => { "name" => "Hackuj ile dusza zapragnie", "image" => "assets/glider.svg"},
|
||||||
|
"zepsute" => { "name" => "Ten przedmiot jest zepsuty", "image" => "assets/dead.svg"},
|
||||||
|
"eksploatuje" => { "name" => "Ten przedmiot eksploatuje materiały", "image" => "assets/money.svg"},
|
||||||
|
"niehackuj" => { "name" => "Nie hackuj tego przedmiotu", "image" => "assets/glider.svg"},
|
||||||
|
|
||||||
|
# meme
|
||||||
|
"bhp" => { "name" => "Gdy ci smutno, gdy ci źle, użyj pasty BHP", "image" => "assets/hswro.svg"},
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
def api(uri)
|
def api(uri)
|
||||||
Excon.get(BACKEND_URL + uri + "/").json!
|
Excon.get(BACKEND_URL + uri + "/").json!
|
||||||
end
|
end
|
||||||
|
@ -70,7 +84,7 @@ end
|
||||||
DYMO_LABEL_SIZE = [89, 36]
|
DYMO_LABEL_SIZE = [89, 36]
|
||||||
ZEBRA_LABEL_SIZE = [50, 30]
|
ZEBRA_LABEL_SIZE = [50, 30]
|
||||||
|
|
||||||
def render_label(dict_or_api, is_qr, item_or_label_id, size: ZEBRA_LABEL_SIZE)
|
def render_test(dict_or_api, extra, item_or_label_id, size: ZEBRA_LABEL_SIZE)
|
||||||
if dict_or_api
|
if dict_or_api
|
||||||
item = item_or_label_id
|
item = item_or_label_id
|
||||||
else
|
else
|
||||||
|
@ -94,10 +108,13 @@ def render_label(dict_or_api, is_qr, item_or_label_id, size: ZEBRA_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
|
||||||
|
|
||||||
if is_qr
|
if extra == "qr"
|
||||||
print_qr_code CODE_PREFIX + item['short_id'], stroke: false,
|
print_qr_code CODE_PREFIX + item['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
|
||||||
|
elsif extra == "img"
|
||||||
|
image_size = [bounds.height / 2, 27].max
|
||||||
|
svg IO.read(item['image']), width: image_size, position: :right
|
||||||
end
|
end
|
||||||
|
|
||||||
owner_text = item["owner"] ? "owner: #{item['owner']}\n\n" : ""
|
owner_text = item["owner"] ? "owner: #{item['owner']}\n\n" : ""
|
||||||
|
@ -118,43 +135,18 @@ def render_label(dict_or_api, is_qr, item_or_label_id, size: ZEBRA_LABEL_SIZE)
|
||||||
pdf.render
|
pdf.render
|
||||||
end
|
end
|
||||||
|
|
||||||
def render_hack(data, size: ZEBRA_LABEL_SIZE)
|
|
||||||
pdf = Prawn::Document.new(page_size: size.map { |x| mm2pt(x) },
|
|
||||||
margin: [2, 2, 2, 6].map { |x| mm2pt(x) }) do
|
|
||||||
font_families.update("DejaVuSans" => {
|
|
||||||
normal: "fonts/DejaVuSans.ttf",
|
|
||||||
italic: "fonts/DejaVuSans-Oblique.ttf",
|
|
||||||
bold: "fonts/DejaVuSans-Bold.ttf",
|
|
||||||
bold_italic: "fonts/DejaVuSans-BoldOblique.ttf"
|
|
||||||
})
|
|
||||||
font 'DejaVuSans'
|
|
||||||
|
|
||||||
image_size = [bounds.height / 2, 27].max
|
|
||||||
svg IO.read(data['image']), width: image_size, position: :right
|
|
||||||
bounding_box(bounds.top_left, width: bounds.width - image_size) do
|
|
||||||
text_box data['text'],
|
|
||||||
size: 40, align: :center, valign: :center, width: bounds.width-10,
|
|
||||||
inline_format: true, overflow: :shrink_to_fit, disable_wrap_by_char: true
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
end
|
|
||||||
|
|
||||||
pdf.render
|
|
||||||
end
|
|
||||||
|
|
||||||
set :bind, '0.0.0.0'
|
set :bind, '0.0.0.0'
|
||||||
|
|
||||||
get '/api/1/preview/:id.pdf' do
|
get '/api/1/preview/:id.pdf' do
|
||||||
headers["Content-Type"] = "application/pdf; charset=utf8"
|
headers["Content-Type"] = "application/pdf; charset=utf8"
|
||||||
render_label(false,true,params["id"])
|
render_test(false,"qr",params["id"])
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
get '/api/1/preview/:id.png' do
|
get '/api/1/preview/:id.png' do
|
||||||
headers["Content-Type"] = "image/png"
|
headers["Content-Type"] = "image/png"
|
||||||
img = Magick::ImageList.new()
|
img = Magick::ImageList.new()
|
||||||
img = img.from_blob(render_label(false,true,params["id"])){ self.density = 200 }.first
|
img = img.from_blob(render_test(false,"qr",params["id"])){ self.density = 200 }.first
|
||||||
img.format = 'png'
|
img.format = 'png'
|
||||||
img.background_color = 'white'
|
img.background_color = 'white'
|
||||||
img.to_blob
|
img.to_blob
|
||||||
|
@ -162,41 +154,14 @@ end
|
||||||
|
|
||||||
post '/api/1/print/:id' do
|
post '/api/1/print/:id' do
|
||||||
temp = Tempfile.new('labelmaker')
|
temp = Tempfile.new('labelmaker')
|
||||||
temp.write(render_label(false,true, params["id"]))
|
temp.write(render_test(false,"qr", params["id"]))
|
||||||
temp.close
|
temp.close
|
||||||
system("lpr -P Zebra #{temp.path}")
|
system("lpr -P Zebra #{temp.path}")
|
||||||
end
|
end
|
||||||
|
|
||||||
get '/api/1/testprint' do
|
|
||||||
test = {"owner" => "grzegorz_brzęczyszczykiewicz", "short_id" => "wszczebrzeszyniechrzaszczbrzmiwtrzcinie", "name" => "pchnąć w tę łódź jeża lub ośm skrzyń fig"}
|
|
||||||
temp = Tempfile.new('labelmaker')
|
|
||||||
temp.write(render_label(true,true,test))
|
|
||||||
temp.close
|
|
||||||
system("lpr -P Zebra #{temp.path}")
|
|
||||||
end
|
|
||||||
|
|
||||||
get '/api/1/testshow' do
|
|
||||||
test = {"owner" => "grzegorz_brzęczyszczykiewicz", "short_id" => "wszczebrzeszyniechrzaszczbrzmiwtrzcinie", "name" => "pchnąć w tę łódź jeża lub ośm skrzyń fig"}
|
|
||||||
headers["Content-Type"] = "application/pdf; charset=utf8"
|
|
||||||
render_label(true, true, test)
|
|
||||||
end
|
|
||||||
|
|
||||||
templates = {
|
|
||||||
"prywatne" => { "text" => "Ten przedmiot jest własnością prywatną", "image" => "assets/person.svg"},
|
|
||||||
"hsowe" => { "text" => "Ten przedmiot należy do HSWro", "image" => "assets/hswro.svg"},
|
|
||||||
"hackuj" => { "text" => "Hackuj ile dusza zapragnie", "image" => "assets/glider.svg"},
|
|
||||||
"zepsute" => { "text" => "Ten przedmiot jest zepsuty", "image" => "assets/dead.svg"},
|
|
||||||
"eksploatuje" => { "text" => "Ten przedmiot eksploatuje materiały", "image" => "assets/money.svg"},
|
|
||||||
"niehackuj" => { "text" => "Nie hackuj tego przedmiotu", "image" => "assets/glider.svg"},
|
|
||||||
|
|
||||||
# meme
|
|
||||||
"bhp" => { "text" => "Gdy ci smutno, gdy ci źle, użyj pasty BHP", "image" => "assets/hswro.svg"},
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
get '/api/1/templates/preview/:id' do
|
get '/api/1/templates/preview/:id' do
|
||||||
headers["Content-Type"] = "application/pdf; charset=utf8"
|
headers["Content-Type"] = "application/pdf; charset=utf8"
|
||||||
render_hack(templates[params["id"]])
|
render_test(true, "img", templates[params["id"]])
|
||||||
end
|
end
|
||||||
|
|
||||||
get '/api/1/templates/print/:id' do
|
get '/api/1/templates/print/:id' do
|
||||||
|
@ -208,12 +173,13 @@ end
|
||||||
|
|
||||||
get '/api/1/nametag/preview/:id' do
|
get '/api/1/nametag/preview/:id' do
|
||||||
headers["Content-Type"] = "application/pdf; charset=utf8"
|
headers["Content-Type"] = "application/pdf; charset=utf8"
|
||||||
render_hack({"text" => params['id'], "image" => "assets/hswro.svg"})
|
|
||||||
|
render_test(true, "img",{"text" => params['id'], "image" => "assets/hswro.svg"})
|
||||||
end
|
end
|
||||||
|
|
||||||
get '/api/1/nametag/print/:id' do
|
get '/api/1/nametag/print/:id' do
|
||||||
temp = Tempfile.new('labelmaker')
|
temp = Tempfile.new('labelmaker')
|
||||||
temp.write(render_hack({"text" => params['id'], "image" => "assets/hswro.svg"}))
|
temp.write(render_test(true, "img",{"text" => params['id'], "image" => "assets/hswro.svg"}))
|
||||||
temp.close
|
temp.close
|
||||||
system("lpr -P Zebra #{temp.path}")
|
system("lpr -P Zebra #{temp.path}")
|
||||||
end
|
end
|
||||||
|
@ -221,14 +187,49 @@ end
|
||||||
get '/api/1/customitem/preview/:owner/:name' do
|
get '/api/1/customitem/preview/:owner/:name' do
|
||||||
test = {"owner" => params["owner"], "short_id" => "test", "name" => params["name"]}
|
test = {"owner" => params["owner"], "short_id" => "test", "name" => params["name"]}
|
||||||
headers["Content-Type"] = "application/pdf; charset=utf8"
|
headers["Content-Type"] = "application/pdf; charset=utf8"
|
||||||
render_label(true,false,test)
|
render_test(true,"blank",test)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
get '/api/1/customitem/print/:owner/:name' do
|
get '/api/1/customitem/print/:owner/:name' do
|
||||||
test = {"owner" => params["owner"], "short_id" => "test", "name" => params["name"]}
|
test = {"owner" => params["owner"], "short_id" => "test", "name" => params["name"]}
|
||||||
temp = Tempfile.new('labelmaker')
|
temp = Tempfile.new('labelmaker')
|
||||||
temp.write(render_label(true,false,test))
|
temp.write(render_test(true,"blank",test))
|
||||||
|
temp.close
|
||||||
|
system("lpr -P Zebra #{temp.path}")
|
||||||
|
end
|
||||||
|
|
||||||
|
# TESTY
|
||||||
|
|
||||||
|
get '/api/1/dupatest/preview' do
|
||||||
|
test = {"owner" => "test", "short_id" => "test", "name" => "test", "image" => "assets/glider.svg"}
|
||||||
|
headers["Content-Type"] = "application/pdf; charset=utf8"
|
||||||
|
render_test(true,"img",test)
|
||||||
|
end
|
||||||
|
|
||||||
|
get '/api/1/testgift/preview' do
|
||||||
|
test = {"name" => "Przedmiot podarował: lynx, małpa, franek", "image" => "assets/gift.svg"}
|
||||||
|
headers["Content-Type"] = "application/pdf; charset=utf8"
|
||||||
|
render_test(true,"img",test)
|
||||||
|
end
|
||||||
|
|
||||||
|
get '/api/1/testgift/print' do
|
||||||
|
test = {"name" => "Przedmiot podarował: lynx, małpa, franek", "image" => "assets/gift.svg"}
|
||||||
|
temp = Tempfile.new('labelmaker')
|
||||||
|
temp.write(render_test(true, "img",test))
|
||||||
|
temp.close
|
||||||
|
system("lpr -P Zebra #{temp.path}")
|
||||||
|
end
|
||||||
|
|
||||||
|
get '/api/1/testshow' do
|
||||||
|
test = {"owner" => "grzegorz_brzęczyszczykiewicz", "short_id" => "wszczebrzeszyniechrzaszczbrzmiwtrzcinie", "name" => "pchnąć w tę łódź jeża lub ośm skrzyń fig"}
|
||||||
|
headers["Content-Type"] = "application/pdf; charset=utf8"
|
||||||
|
render_test(true, "qr", test)
|
||||||
|
end
|
||||||
|
|
||||||
|
get '/api/1/testprint' do
|
||||||
|
test = {"owner" => "grzegorz_brzęczyszczykiewicz", "short_id" => "wszczebrzeszyniechrzaszczbrzmiwtrzcinie", "name" => "pchnąć w tę łódź jeża lub ośm skrzyń fig"}
|
||||||
|
temp = Tempfile.new('labelmaker')
|
||||||
|
temp.write(render_test(true,"qr",test))
|
||||||
temp.close
|
temp.close
|
||||||
system("lpr -P Zebra #{temp.path}")
|
system("lpr -P Zebra #{temp.path}")
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in New Issue