Skip to content

DRY ActiveResource decode methods #40

@jistr

Description

@jistr

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions