Class: Deliveries::Labels

Inherits:
Object
  • Object
show all
Includes:
LabelUtils
Defined in:
lib/deliveries/labels.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from LabelUtils

image2pdf, merge_pdfs

Constructor Details

#initialize(raw: nil, url: nil) ⇒ Labels

Returns a new instance of Labels.



7
8
9
10
11
# File 'lib/deliveries/labels.rb', line 7

def initialize(raw: nil, url: nil)
  @raw = raw
  @url = url
  @labels = []
end

Instance Attribute Details

#urlObject (readonly)

Returns the value of attribute url.



5
6
7
# File 'lib/deliveries/labels.rb', line 5

def url
  @url
end

Instance Method Details

#<<(label) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/deliveries/labels.rb', line 23

def <<(label)
  raise 'Cannot add labels when raw or url are already set' unless @raw.nil? && @url.nil?

  case label
  when Label
    @labels << label
  when %r{\Ahttps?://}
    @labels << Label.new(url: label)
  when String
    @labels << Label.new(raw: label)
  else
    raise "Cannot cast #{label.class.name} to Label"
  end

  self
end

#rawObject



13
14
15
16
17
18
19
20
21
# File 'lib/deliveries/labels.rb', line 13

def raw
  if @raw
    @raw
  elsif @url
    @raw = URI.parse(@url).read.force_encoding('binary')
  elsif !@labels.empty?
    merge_pdfs @labels.map(&:raw)
  end
end