class Labelary::Image

Public Class Methods

encode(*args) click to toggle source
# File lib/labelary/image.rb, line 3
def self.encode(*args)
  self.new(*args).encode
end
new(path:nil, mime_type:, filename:nil, file_io:nil) click to toggle source
# File lib/labelary/image.rb, line 7
def initialize(path:nil, mime_type:, filename:nil, file_io:nil)
  if path.present?
    @file = Faraday::UploadIO.new path, mime_type
  elsif file_io.present? && filename.present?
    @file = Faraday::UploadIO.new file_io, mime_type, filename
  else
    raise 'Path to image and MIME type or an IO object, filename and MIME type must be specified.'
  end
end

Public Instance Methods

encode() click to toggle source

labelary.com/faq.html#image-conversion

# File lib/labelary/image.rb, line 18
def encode
  response = Labelary::Client.connection.post '/v1/graphics', { file: @file }, { Accept: 'application/json' }
  image = response.body

  return '^GFA,' + image['totalBytes'].to_s + ',' + image['totalBytes'].to_s + ',' + image['rowBytes'].to_s + ',' + image['data'] + '^FS'
end