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



88
89
90
91
92
93
94
95
96
97
98
99
# 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) ⇒ Object



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# 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) ⇒ Object



80
81
82
83
84
85
86
# 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

Instance Method Details

#class_name_to_symObject

Converts Transip::DnsEntry into :dns_entry



54
55
56
# 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) ⇒ Object



64
65
66
67
68
69
# 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_hashObject

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



72
73
74
# 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_hashObject



76
77
78
# File 'lib/transip.rb', line 76

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



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

def to_s
  Gyoku.xml(self.members_to_hash)
end

#underscore(string) ⇒ Object

See Rails’ underscore method.



46
47
48
49
50
51
# 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