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



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

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



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

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



72
73
74
75
76
77
78
# File 'lib/transip.rb', line 72

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



53
54
55
# File 'lib/transip.rb', line 53

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



64
65
66
# File 'lib/transip.rb', line 64

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

#to_hashObject



68
69
70
# File 'lib/transip.rb', line 68

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



59
60
61
# File 'lib/transip.rb', line 59

def to_s
  Gyoku.xml(self.members_to_hash)
end

#underscore(string) ⇒ Object

See Rails’ underscore method.



45
46
47
48
49
50
# File 'lib/transip.rb', line 45

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