class Transip::TransipStruct
Following subclasses are actually not needed (as you can also do the same by just creating hashes..).
Public Class Methods
from_hash(hash)
click to toggle source
# File lib/transip.rb, line 88 def self.from_hash(hash) begin result = get_type(hash).new rescue return hash end hash.each do |key, value| next if key[0] == '@' result.send(:"#{key}=", from_soap(value)) end result end
from_soap(input)
click to toggle source
# File lib/transip.rb, line 101 def self.from_soap(input) if input.is_a? Array result = input.map {|value| from_soap(value)} elsif input.is_a? Hash if input.keys.first == :item result = from_soap(input[:item]) elsif input[:'@xsi:type'] == 'xsd:string' result = '' else result = TransipStruct.from_hash(input) end # this is not a transip struct if result.is_a? Hash result.each do |key, value| result[key] = from_soap(value) end end else result = input end result end
get_type(hash)
click to toggle source
# File lib/transip.rb, line 80 def self.get_type(hash) type = hash[:'@xsi:type'].split(":").last raise "No type definition found in hash" if type.nil? klass = Transip.const_get(type) rescue nil raise "Invalid transipStruct #{type}" unless klass < TransipStruct klass end
Public Instance Methods
class_name_to_sym()
click to toggle source
Converts Transip::DnsEntry
into :dns_entry
# File lib/transip.rb, line 54 def class_name_to_sym self.underscore(self.class.name.split('::').last).to_sym end
member_name_to_camel(name)
click to toggle source
# File lib/transip.rb, line 64 def member_name_to_camel(name) parts = name.to_s.split("_") parts.map{|p|p.capitalize!} parts[0].downcase! parts.join end
members_to_hash()
click to toggle source
See what happens here: snippets.dzone.com/posts/show/302
# File lib/transip.rb, line 72 def members_to_hash Hash[*members.collect {|m| [member_name_to_camel(m), self.send(m)]}.flatten(1)] end
to_hash()
click to toggle source
# File lib/transip.rb, line 76 def to_hash { self.class_name_to_sym => self.members_to_hash } end
to_s()
click to toggle source
Gyoku.xml (see: github.com/rubiii/gyoku) is used by Savon. It calls to_s
on unknown Objects. We use it to convert
# File lib/transip.rb, line 60 def to_s Gyoku.xml(self.members_to_hash) end
underscore(string)
click to toggle source
See Rails' underscore method.
# File lib/transip.rb, line 46 def underscore(string) string.gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2'). gsub(/([a-z\d])([A-Z])/,'\1_\2'). tr("-", "_"). downcase end