Class: Transip::TransipStruct

Inherits:
Struct
  • Object
show all
Defined in:
lib/transip.rb

Overview

Following subclasses are actually not needed (as you can also do the same by just creating hashes..).

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.from_hash(hash) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/transip.rb', line 78

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) ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/transip.rb', line 91

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) ⇒ Object



70
71
72
73
74
75
76
# File 'lib/transip.rb', line 70

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

Instance Method Details

#class_name_to_symObject

Converts Transip::DnsEntry into :dns_entry



51
52
53
# File 'lib/transip.rb', line 51

def class_name_to_sym
  self.underscore(self.class.name.split('::').last).to_sym
end

#members_to_hashObject

See what happens here: snippets.dzone.com/posts/show/302



62
63
64
# File 'lib/transip.rb', line 62

def members_to_hash
  Hash[*members.collect {|m| [m, self.send(m)]}.flatten]
end

#to_hashObject



66
67
68
# File 'lib/transip.rb', line 66

def to_hash
  { self.class_name_to_sym => self.members_to_hash }
end

#to_sObject

Gyoku.xml (see: github.com/rubiii/gyoku) is used by Savon. It calls to_s on unknown Objects. We use it to convert



57
58
59
# File 'lib/transip.rb', line 57

def to_s
  Gyoku.xml(self.members_to_hash)
end

#underscore(string) ⇒ Object

See Rails’ underscore method.



43
44
45
46
47
48
# File 'lib/transip.rb', line 43

def underscore(string)
  string.gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
    gsub(/([a-z\d])([A-Z])/,'\1_\2').
    tr("-", "_").
    downcase
end