Class: Labelary::Image

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path: nil, mime_type:, filename: nil, file_io: nil) ⇒ Image

Returns a new instance of Image.



7
8
9
10
11
12
13
14
15
# 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

Class Method Details

.encode(**args) ⇒ Object



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

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

Instance Method Details

#encodeObject



18
19
20
21
22
23
# 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