A lot of the XML decode methods [1] will probably repeat itself across models, so we could extract it as common behavior.
[1] https://github.com/aeolus-incubator/thor-cli/blob/master/lib/aeolus_cli/model/provider.rb#L4-L29
EDIT after pblaho's comment:
Here's a permalink:
|
def decode(xml) |
|
if xml == nil or xml.size == 0 |
|
return nil |
|
end |
|
if xml.start_with?('<providers>') |
|
h = ActiveResource::Formats.remove_root(Hash.from_xml(xml)) |
|
# h is a hash of 'provider' => array of provider hashes |
|
if h['provider'].is_a?(Hash) |
|
# Having just one provider causes the value of 'provider' to |
|
# not exist in a array, so we wrap it in an array to prevent |
|
# an error like: |
|
# ...active_resource/base.rb:929:in `instantiate_collection': |
|
# undefined method `collect!' for #<Hash:0x0000000102e800> (NoMethodError) |
|
[h['provider']] |
|
else |
|
# return value is an array of hashes |
|
h['provider'] |
|
end |
|
elsif xml.start_with?('<provider ') or xml.start_with?('<provider>') |
|
Hash.from_xml(xml) |
|
else |
|
# TODO raise an appropriate error that we didn't get <providers> |
|
# or <provider> xml |
|
nil |
|
end |
|
end |
A lot of the XML decode methods [1] will probably repeat itself across models, so we could extract it as common behavior.
[1] https://github.com/aeolus-incubator/thor-cli/blob/master/lib/aeolus_cli/model/provider.rb#L4-L29
EDIT after pblaho's comment:
Here's a permalink:
thor-cli/lib/aeolus/client/provider.rb
Lines 6 to 31 in e7b65db