lib/g4s/headers/custom_header.rb in g4s_client-0.1.6 vs lib/g4s/headers/custom_header.rb in g4s_client-0.1.7
- old
+ new
@@ -1,10 +1,10 @@
require 'soap/header/simplehandler'
class CustomHeader < SOAP::Header::SimpleHandler
- NAMESPACE = 'http://tempuri.org'
+ NAMESPACE = 'http://WS.G4SI.COM/'
# def initialize(shipment_request)
# @shipment_request = shipment_request
# super(XSD::QName.new(NAMESPACE, 'ShipmentRequest'))
# end
@@ -14,26 +14,48 @@
# # {"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)
- var.gsub!('@','')
- var = capitalize_first_letter(var)
- result[var] = value
- #TODO: NESTING: some things like Address will need to result[var] = to_hash(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