Class: UPS::Parsers::RatesParser

Inherits:
ParserBase
  • Object
show all
Defined in:
lib/ups/parsers/rates_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

#element_tracker_switch, #success?, #switch_active?

Constructor Details

#initializeRatesParser

Returns a new instance of RatesParser.


8
9
10
11
12
# File 'lib/ups/parsers/rates_parser.rb', line 8

def initialize
  super
  self.rated_shipments = []
  @current_rate = {}
end

Instance Attribute Details

#rated_shipmentsObject

Returns the value of attribute rated_shipments


6
7
8
# File 'lib/ups/parsers/rates_parser.rb', line 6

def rated_shipments
  @rated_shipments
end

Instance Method Details

#end_element(name) ⇒ Object


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

def end_element(name)
  super
  return unless name == :RatedShipment
  rated_shipments << @current_rate.tap do |c|
    if c.key? :negotiated_rate
      c[:total] = c[:negotiated_rate]
      c.delete :negotiated_rate
    end
  end
  @current_rate = {}
end

#parse_negotiated_rate(value) ⇒ Object


41
42
43
# File 'lib/ups/parsers/rates_parser.rb', line 41

def parse_negotiated_rate(value)
  @current_rate[:negotiated_rate] = value.as_s
end

#parse_service_code(value) ⇒ Object


45
46
47
48
# File 'lib/ups/parsers/rates_parser.rb', line 45

def parse_service_code(value)
  @current_rate[:service_code] = value.as_s
  @current_rate[:service_name] = UPS::SERVICES[value.as_s]
end

#parse_total_charges(value) ⇒ Object


50
51
52
# File 'lib/ups/parsers/rates_parser.rb', line 50

def parse_total_charges(value)
  @current_rate[:total] = value.as_s
end

#start_element(name) ⇒ Object


14
15
16
# File 'lib/ups/parsers/rates_parser.rb', line 14

def start_element(name)
  super
end

#value(value) ⇒ Object


30
31
32
33
34
35
36
37
38
39
# File 'lib/ups/parsers/rates_parser.rb', line 30

def value(value)
  super
  if switch_active?(:RatedShipment, :Service, :Code)
    parse_service_code value
  elsif switch_active?(:RatedShipment, :TotalCharges)
    parse_total_charges value
  elsif switch_active?(:RatedShipment, :NegotiatedRates, :MonetaryValue)
    parse_negotiated_rate value
  end
end