require 'soap/header/simplehandler' class CustomHeader < SOAP::Header::SimpleHandler NAMESPACE = 'http://WS.G4SI.COM/' # def initialize(shipment_request) # @shipment_request = shipment_request # super(XSD::QName.new(NAMESPACE, 'ShipmentRequest')) # end # # def on_simple_outbound # to_hash(@shipment_request) # # {"Username" => @username, "Password" => @password, "AccessKey"=>@accessKey} # end protected def primitive_types [ NilClass, TrueClass, FalseClass, Numeric, Time, Date, DateTime, String, Symbol ] end # Transform a object into a hash of its instance variables def to_hash(object) return {} unless object result = {} #puts "to_hash #{object.class}:#{object}" #puts "#{y object.instance_variables.sort}" object.instance_variables.each do |var| value = object.instance_variable_get(var) result[instance_var_name_to_tag(var)] = format_value(value) #TODO: NESTING end result end def capitalize_first_letter(string) return nil unless string.is_a?(String) string[0].chr.capitalize + string[1, string.size] end def instance_var_name_to_tag(var) var.gsub!('@','') var = capitalize_first_letter(var) var end def format_value(value) if primitive_types.any?{|type| value.is_a?(type)} if value.is_a?(Time) return value.strftime("%Y-%m-%dT%H:%M:%S") # YYYY-MM-DDTHH:MM:SS end return value else #puts "non primitive value found: #{value.class}:#{value}" return to_hash(value) # recurse for non-"primitive" types such as Address. end end end