Class: UPS::Parsers::ShipAcceptParser

Inherits:
ParserBase
  • Object
show all
Defined in:
lib/ups/parsers/ship_accept_parser.rb

Instance Attribute Summary collapse

Attributes inherited from ParserBase

#error_description, #status_code, #status_description, #switches

Instance Method Summary collapse

Methods inherited from ParserBase

#build_switch_path, #element_tracker_switch, #end_element, #initialize, #start_element, #success?, #switch_active?

Constructor Details

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

Instance Attribute Details

#form_graphic_extensionObject

Returns the value of attribute form_graphic_extension.



7
8
9
# File 'lib/ups/parsers/ship_accept_parser.rb', line 7

def form_graphic_extension
  @form_graphic_extension
end

#form_graphic_imageObject

Returns the value of attribute form_graphic_image.



7
8
9
# File 'lib/ups/parsers/ship_accept_parser.rb', line 7

def form_graphic_image
  @form_graphic_image
end

#form_root_pathObject

Returns the value of attribute form_root_path.



7
8
9
# File 'lib/ups/parsers/ship_accept_parser.rb', line 7

def form_root_path
  @form_root_path
end

#graphic_extensionObject

Returns the value of attribute graphic_extension.



7
8
9
# File 'lib/ups/parsers/ship_accept_parser.rb', line 7

def graphic_extension
  @graphic_extension
end

#graphic_imageObject

Returns the value of attribute graphic_image.



7
8
9
# File 'lib/ups/parsers/ship_accept_parser.rb', line 7

def graphic_image
  @graphic_image
end

#html_imageObject

Returns the value of attribute html_image.



7
8
9
# File 'lib/ups/parsers/ship_accept_parser.rb', line 7

def html_image
  @html_image
end

#label_graphic_extensionObject

Returns the value of attribute label_graphic_extension.



7
8
9
# File 'lib/ups/parsers/ship_accept_parser.rb', line 7

def label_graphic_extension
  @label_graphic_extension
end

#label_graphic_imageObject

Returns the value of attribute label_graphic_image.



7
8
9
# File 'lib/ups/parsers/ship_accept_parser.rb', line 7

def label_graphic_image
  @label_graphic_image
end

#label_html_imageObject

Returns the value of attribute label_html_image.



7
8
9
# File 'lib/ups/parsers/ship_accept_parser.rb', line 7

def label_html_image
  @label_html_image
end

#label_root_pathObject

Returns the value of attribute label_root_path.



7
8
9
# File 'lib/ups/parsers/ship_accept_parser.rb', line 7

def label_root_path
  @label_root_path
end

#tracking_numberObject

Returns the value of attribute tracking_number.



7
8
9
# File 'lib/ups/parsers/ship_accept_parser.rb', line 7

def tracking_number
  @tracking_number
end

Instance Method Details

#base64_to_file(contents, type) ⇒ Object



73
74
75
76
77
78
79
80
81
82
# File 'lib/ups/parsers/ship_accept_parser.rb', line 73

def base64_to_file(contents, type)
  file_config = ['ups', self.send("#{type}_graphic_extension".to_sym)]
  Tempfile.new(file_config, nil, encoding: 'ascii-8bit').tap do |file|
    begin
      file.write Base64.decode64(contents)
    ensure
      file.rewind
    end
  end
end

#initialize_document_root_pathsObject



29
30
31
32
# File 'lib/ups/parsers/ship_accept_parser.rb', line 29

def initialize_document_root_paths
  self.label_root_path = [:ShipmentResults, :PackageResults, :LabelImage]
  self.form_root_path  = [:ShipmentResults, :Form, :Image]
end

#parse_document_data(value, type) ⇒ Object



34
35
36
37
38
39
40
# File 'lib/ups/parsers/ship_accept_parser.rb', line 34

def parse_document_data(value, type)
  root_path = self.send("#{type}_root_path")

  parse_graphic_extension(root_path, value, type)
  parse_graphic_image(root_path, value, type)
  parse_html_image(root_path, value, type)
end

#parse_graphic_extension(root_path, value, type) ⇒ Object



58
59
60
61
62
63
64
65
66
# File 'lib/ups/parsers/ship_accept_parser.rb', line 58

def parse_graphic_extension(root_path, value, type)
  graphic_extension_subpath = (type == 'label' ? :LabelImageFormat : :ImageFormat)

  switch_path = build_switch_path(root_path, graphic_extension_subpath, :Code)
  return unless switch_active?(switch_path)

  self.send("#{type}_graphic_extension=".to_sym, ".#{value.as_s.downcase}")
  self.send("graphic_extension=".to_sym, ".#{value.as_s.downcase}") if type == 'label'
end

#parse_graphic_image(root_path, value, type) ⇒ Object



42
43
44
45
46
47
48
# File 'lib/ups/parsers/ship_accept_parser.rb', line 42

def parse_graphic_image(root_path, value, type)
  switch_path = build_switch_path(root_path, :GraphicImage)
  return unless switch_active?(switch_path)

  self.send("#{type}_graphic_image=".to_sym, base64_to_file(value.as_s, type))
  self.send("graphic_image=".to_sym, base64_to_file(value.as_s, type)) if type == 'label'
end

#parse_html_image(root_path, value, type) ⇒ Object



50
51
52
53
54
55
56
# File 'lib/ups/parsers/ship_accept_parser.rb', line 50

def parse_html_image(root_path, value, type)
  switch_path = build_switch_path(root_path, :HTMLImage)
  return unless switch_active?(switch_path)

  self.send("#{type}_html_image=".to_sym, base64_to_file(value.as_s, type))
  self.send("html_image=".to_sym, base64_to_file(value.as_s, type)) if type == 'label'
end

#parse_tracking_number(value) ⇒ Object



68
69
70
71
# File 'lib/ups/parsers/ship_accept_parser.rb', line 68

def parse_tracking_number(value)
  return unless switch_active?(:ShipmentIdentificationNumber)
  self.tracking_number = value.as_s
end

#value(value) ⇒ Object



19
20
21
22
23
24
25
26
27
# File 'lib/ups/parsers/ship_accept_parser.rb', line 19

def value(value)
  initialize_document_root_paths

  parse_document_data(value, 'label')
  parse_document_data(value, 'form')
  parse_tracking_number(value)

  super
end