summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorPiotr Dobrowolski <admin@tastycode.pl>2017-05-28 02:53:46 +0200
committerPiotr Dobrowolski <admin@tastycode.pl>2017-05-28 02:53:46 +0200
commite6c4ebee5d250eb3f4267127be18d9f348a8edf3 (patch)
treee7667f102cd652bdb7a7f3eafb14593250f7f4a3
downloadspejstore-labelmaker-e6c4ebee5d250eb3f4267127be18d9f348a8edf3.tar.gz
spejstore-labelmaker-e6c4ebee5d250eb3f4267127be18d9f348a8edf3.tar.bz2
spejstore-labelmaker-e6c4ebee5d250eb3f4267127be18d9f348a8edf3.zip
Initial commit
-rw-r--r--.editorconfig12
-rw-r--r--.gitignore2
-rw-r--r--Gemfile10
-rw-r--r--Gemfile.lock50
-rw-r--r--main.rb109
5 files changed, 183 insertions, 0 deletions
diff --git a/.editorconfig b/.editorconfig
new file mode 100644
index 0000000..235f3d9
--- /dev/null
+++ b/.editorconfig
@@ -0,0 +1,12 @@
+# top-most EditorConfig file
+root = true
+
+# Unix-style newlines with a newline ending every file
+[*]
+end_of_line = lf
+insert_final_newline = true
+charset = utf-8
+
+[*.rb]
+indent_style = spaces
+indent_size = 2
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..180bf07
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,2 @@
+.bundle
+vendor
diff --git a/Gemfile b/Gemfile
new file mode 100644
index 0000000..07bd885
--- /dev/null
+++ b/Gemfile
@@ -0,0 +1,10 @@
+source 'https://rubygems.org'
+
+gem 'prawn'
+gem 'prawn-qrcode'
+gem 'prawn-svg'
+gem 'sinatra'
+gem 'color'
+gem 'excon'
+gem 'rmagick'
+gem 'json'
diff --git a/Gemfile.lock b/Gemfile.lock
new file mode 100644
index 0000000..628de18
--- /dev/null
+++ b/Gemfile.lock
@@ -0,0 +1,50 @@
+GEM
+ remote: https://rubygems.org/
+ specs:
+ addressable (2.5.0)
+ public_suffix (~> 2.0, >= 2.0.2)
+ chunky_png (1.3.8)
+ color (1.8)
+ css_parser (1.4.9)
+ addressable
+ excon (0.55.0)
+ json (1.8.3)
+ pdf-core (0.6.1)
+ prawn (2.1.0)
+ pdf-core (~> 0.6.1)
+ ttfunk (~> 1.4.0)
+ prawn-qrcode (0.3.0)
+ prawn (>= 1)
+ rqrcode (>= 0.4.1)
+ prawn-svg (0.26.0)
+ css_parser (~> 1.3)
+ prawn (>= 0.11.1, < 3)
+ public_suffix (2.0.5)
+ rack (1.6.5)
+ rack-protection (1.5.3)
+ rack
+ rmagick (2.16.0)
+ rqrcode (0.10.1)
+ chunky_png (~> 1.0)
+ sinatra (1.4.8)
+ rack (~> 1.5)
+ rack-protection (~> 1.4)
+ tilt (>= 1.3, < 3)
+ tilt (2.0.6)
+ ttfunk (1.4.0)
+
+PLATFORMS
+ ruby
+
+DEPENDENCIES
+ color
+ excon
+ json
+ prawn
+ prawn-qrcode
+ prawn-svg
+ rmagick
+ sinatra
+
+BUNDLED WITH
+ 1.11.2
diff --git a/main.rb b/main.rb
new file mode 100644
index 0000000..c69a89f
--- /dev/null
+++ b/main.rb
@@ -0,0 +1,109 @@
+require 'rubygems'
+require 'sinatra'
+require 'prawn'
+require 'prawn/measurements'
+require 'prawn/qrcode'
+require 'prawn-svg'
+require 'color'
+require 'excon'
+require 'rmagick'
+require 'json'
+require 'zlib'
+
+include Prawn::Measurements
+
+module Excon
+ class Response
+ def json!()
+ puts body
+ JSON.parse(body)
+ end
+ end
+end
+
+BACKEND_URL = 'http://127.0.0.1:8000/api/1/'
+CODE_PREFIX = "HTTP://I/"
+
+def api(uri)
+ Excon.get(BACKEND_URL + uri + "/").json!
+end
+
+def render_identicode(data, id, extent)
+ pts = [[0, 0], [0, 1], [1, 1], [1, 0], [0, 0]]
+
+ 4.times do |n|
+ color = Color::HSL.from_fraction((id % 6) / 6.0, 1.0, 0.3).html[1..6]
+ id /= 6
+
+ save_graphics_state do
+ soft_mask do
+ fill_color 'ffffff'
+ polygon = [pts[n], [0.5, 0.5], pts[n+1]].map{ |v| [v[0]*bounds.height, v[1]*bounds.height] }
+ fill_polygon(*(polygon))
+ end
+
+ print_qr_code data, stroke: false,
+ extent: extent, foreground_color: color,
+ pos: [bounds.left, bounds.top]
+ end
+ end
+
+ fill_color '000000'
+end
+
+def render_label(label)
+ margin = 2
+
+ label = api("labels/#{label}")
+
+ pdf = Prawn::Document.new(page_size: [89, 36].map { |x| mm2pt(x) },
+ margin: mm2pt(margin)) 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'
+
+ svg IO.read("hsyrenka.svg"),
+ position: :right, vposition: :bottom,
+ height: 0.5*bounds.height
+
+ print_qr_code CODE_PREFIX + label['id'], stroke: false,
+ extent: mm2pt(36-2*margin), foreground_color: color,
+ pos: [bounds.left, bounds.top]
+
+ text_box label['item']['name'],
+ size: 30, align: :center, valign: :center,
+ inline_format: true,
+ width: bounds.width - bounds.height - 8,
+ height: bounds.height - 10,
+ at: [bounds.left+bounds.height, bounds.top - 5],
+ overflow: :shrink_to_fit
+ end
+
+ pdf.render
+end
+
+get '/api/1/preview/:label.pdf' do
+ headers["Content-Type"] = "application/pdf; charset=utf8"
+ render_label params["label"]
+end
+
+get '/api/1/preview/:label.png' do
+ headers["Content-Type"] = "image/png"
+ img = Magick::ImageList.new()
+ img = img.from_blob(render_label(params["label"])){ self.density = 200 }.first
+ img.format = 'png'
+ img.background_color = 'white'
+ img.to_blob
+end
+
+post '/api/1/print/:label' do
+ temp = Tempfile.new('labelmaker')
+ temp.write(render_label(params["label"]))
+ temp.close
+ system("lpr -P DYMO_LabelWriter_450 #{temp.path}")
+end