Sha256: 09592326c35cbdd15e91999c0e614bc85bf5d3f9556b362faec3db8073ead11a

Contents?: true

Size: 1.03 KB

Versions: 2

Compression:

Stored size: 1.03 KB

Contents

module Labelary
  class Label
    def self.render(**args)
      self.new(**args).render
    end

    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

    # http://labelary.com/service.html
    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

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

    private

    def config
      @config ||= Labelary.configuration
    end

  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
labelary-0.5.0 lib/labelary/label.rb
labelary-0.4.0 lib/labelary/label.rb