Class: EndiciaLabelServer::Parsers::PostageLabelParser

Inherits:
ParserBase
  • Object
show all
Defined in:
lib/endicia_label_server/parsers/postage_label_parser.rb

Instance Attribute Summary collapse

Attributes inherited from ParserBase

#current_attributes, #error_description, #status_code, #switches

Instance Method Summary collapse

Methods inherited from ParserBase

#attr, #element_tracker_switch, #end_element, #initialize, #reset_current_attributes!, #success?, #switch_active?

Constructor Details

This class inherits a constructor from EndiciaLabelServer::Parsers::ParserBase

Instance Attribute Details

#cost_centerObject

Returns the value of attribute cost_center.



6
7
8
# File 'lib/endicia_label_server/parsers/postage_label_parser.rb', line 6

def cost_center
  @cost_center
end

#customs_numberObject

Returns the value of attribute customs_number.



6
7
8
# File 'lib/endicia_label_server/parsers/postage_label_parser.rb', line 6

def customs_number
  @customs_number
end

#final_postageObject

Returns the value of attribute final_postage.



6
7
8
# File 'lib/endicia_label_server/parsers/postage_label_parser.rb', line 6

def final_postage
  @final_postage
end

#labelObject

Returns the value of attribute label.



6
7
8
# File 'lib/endicia_label_server/parsers/postage_label_parser.rb', line 6

def label
  @label
end

#picObject

Returns the value of attribute pic.



6
7
8
# File 'lib/endicia_label_server/parsers/postage_label_parser.rb', line 6

def pic
  @pic
end

#postage_balanceObject

Returns the value of attribute postage_balance.



6
7
8
# File 'lib/endicia_label_server/parsers/postage_label_parser.rb', line 6

def postage_balance
  @postage_balance
end

#postmark_dateObject

Returns the value of attribute postmark_date.



6
7
8
# File 'lib/endicia_label_server/parsers/postage_label_parser.rb', line 6

def postmark_date
  @postmark_date
end

#requester_idObject

Returns the value of attribute requester_id.



6
7
8
# File 'lib/endicia_label_server/parsers/postage_label_parser.rb', line 6

def requester_id
  @requester_id
end

#tracking_numberObject

Returns the value of attribute tracking_number.



6
7
8
# File 'lib/endicia_label_server/parsers/postage_label_parser.rb', line 6

def tracking_number
  @tracking_number
end

#transaction_date_timeObject

Returns the value of attribute transaction_date_time.



6
7
8
# File 'lib/endicia_label_server/parsers/postage_label_parser.rb', line 6

def transaction_date_time
  @transaction_date_time
end

#transaction_idObject

Returns the value of attribute transaction_id.



6
7
8
# File 'lib/endicia_label_server/parsers/postage_label_parser.rb', line 6

def transaction_id
  @transaction_id
end

Instance Method Details

#label_switch_active?Boolean

Returns:

  • (Boolean)


36
37
38
# File 'lib/endicia_label_server/parsers/postage_label_parser.rb', line 36

def label_switch_active?
  switch_active?(:Base64LabelImage) || (switch_active?(:Label, :Image) && current_attributes[:PartNumber] == '1')
end

#parse_label(encoded_label) ⇒ Object



48
49
50
51
52
53
54
# File 'lib/endicia_label_server/parsers/postage_label_parser.rb', line 48

def parse_label(encoded_label)
  label_file = Tempfile.new(['endicia', '.png'])
  label_file.binmode
  label_file.write(Base64.decode64(encoded_label))
  label_file.rewind
  self.label = label_file
end

#start_element(name) ⇒ Object



18
19
20
21
# File 'lib/endicia_label_server/parsers/postage_label_parser.rb', line 18

def start_element(name)
  super
  @current_element = name.to_s
end

#underscore(value) ⇒ Object



40
41
42
43
44
45
46
# File 'lib/endicia_label_server/parsers/postage_label_parser.rb', line 40

def underscore(value)
  value.gsub(/::/, '/')
    .gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2')
    .gsub(/([a-z\d])([A-Z])/, '\1_\2')
    .tr('-', '_')
    .downcase
end

#value(value) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/endicia_label_server/parsers/postage_label_parser.rb', line 23

def value(value)
  super

  element = underscore(@current_element)
  string_value = value.as_s
  if label_switch_active?
    parse_label(string_value)
  else
    function_name = "#{element}="
    send(function_name, string_value) if respond_to?(function_name)
  end
end