summaryrefslogtreecommitdiffstats
path: root/main.rb
blob: 837d3329d6342c1a963cef1912dffb681937bee7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
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://inventory.waw.hackerspace.pl/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

set :bind, '0.0.0.0'

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