Class: Labelary::Label

Inherits:
Object
  • Object
show all
Defined in:
lib/labelary/label.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(dpmm: nil, width: nil, height: nil, index: nil, zpl:, content_type: nil, font: nil) ⇒ Label

Returns a new instance of Label.



7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/labelary/label.rb', line 7

def initialize(dpmm: nil, width: nil, height: nil, index: nil, zpl:, content_type: nil, font: nil)
  @zpl    ||= zpl
  @dpmm   ||= dpmm   || config.dpmm
  @width  ||= width  || config.width
  @height ||= height || config.height
  @index  ||= index  || config.index
  @content_type ||= content_type || config.content_type
  @font ||= font || config.font

  raise 'Invalid dpmm'   if @dpmm.nil?
  raise 'Invalid width'  if @width.nil?
  raise 'Invalid height' if @height.nil?
end

Class Method Details

.render(**args) ⇒ Object



3
4
5
# File 'lib/labelary/label.rb', line 3

def self.render(**args)
  self.new(**args).render
end

Instance Method Details

#font_stringObject



28
29
30
# File 'lib/labelary/label.rb', line 28

def font_string
  @font.present? ? @font.to_s : ''
end

#renderObject



22
23
24
25
26
# File 'lib/labelary/label.rb', line 22

def render
  payload = font_string + @zpl
  response = Labelary::Client.connection.post "/v1/printers/#{@dpmm}dpmm/labels/#{@width}x#{@height}/#{@index}/", payload, { Accept: @content_type }
  return response.body
end