From c501f73fa6fa3bab8bb35a9cc5dea32cb626ab56 Mon Sep 17 00:00:00 2001 From: Jiri Stransky Date: Fri, 1 Feb 2013 12:04:52 +0100 Subject: [PATCH] Move code and ensure proper requires General things concerning AeolusClient namespace are moved to aeolus_client.rb. Logging configuration that is done by requiring aeolus_client/base.rb is marked as FIXME for removal when configuration refactoring is done. Require aeolus_client/base.rb for resource classes that inherit from it. --- lib/aeolus_client.rb | 24 +++++++++++++++++++++ lib/aeolus_client/base.rb | 30 ++------------------------- lib/aeolus_client/provider.rb | 2 ++ lib/aeolus_client/provider_account.rb | 2 ++ lib/aeolus_client/provider_type.rb | 2 ++ 5 files changed, 32 insertions(+), 28 deletions(-) create mode 100644 lib/aeolus_client.rb diff --git a/lib/aeolus_client.rb b/lib/aeolus_client.rb new file mode 100644 index 0000000..b85f150 --- /dev/null +++ b/lib/aeolus_client.rb @@ -0,0 +1,24 @@ +require 'active_resource' + +module AeolusClient +end + +module ActiveResource + class Errors < ActiveModel::Errors + # Grabs errors from an XML response. + def from_xml(xml, save_cache = false) + array = Array.wrap(Hash.from_xml(xml)['errors']['error']) + # The below is added because the default behaviour (the above + # line) only works fine if the error string is not buried in + # another layer of xml. + # + # from_array (in this Errors class) is brittle and will blow up + # if it doesn't get an array of strings. + if array.size >= 1 && array[0].is_a?(Hash) && + array[0].has_key?('message') + array.map! {|error| error['message']} + end + from_array array, save_cache + end + end +end diff --git a/lib/aeolus_client/base.rb b/lib/aeolus_client/base.rb index 681e934..f985cdf 100644 --- a/lib/aeolus_client/base.rb +++ b/lib/aeolus_client/base.rb @@ -1,36 +1,10 @@ -require 'active_resource' -require 'aeolus_cli' +require 'aeolus_client' require 'logger' +# FIXME: get rid of these when refactoring config loading: ActiveResource::Base.logger = Logger.new(STDOUT) ActiveResource::Base.logger.level = Logger::INFO -module ActiveResource - class Errors - # Grabs errors from an XML response. - def from_xml(xml, save_cache = false) - array = Array.wrap(Hash.from_xml(xml)['errors']['error']) - # The below is added because the default behaviour (the above - # line) only works fine if the error string is not buried in - # another layer of xml. - # - # from_array (in this Errors class) is brittle and will blow up - # if it doesn't get an array of strings. - if array.size >= 1 && array[0].is_a?(Hash) && - array[0].has_key?('message') - array.map! {|error| error['message']} - end - from_array array, save_cache - end - end -end - -# declaring the namespace but for style reasons not including the Base -# class definition within the block (saving as much excessive -# indentation as possible) -module AeolusClient -end - class AeolusClient::Base < ActiveResource::Base self.timeout = 600 self.format = :xml diff --git a/lib/aeolus_client/provider.rb b/lib/aeolus_client/provider.rb index f34ae44..0b1f5b8 100644 --- a/lib/aeolus_client/provider.rb +++ b/lib/aeolus_client/provider.rb @@ -1,3 +1,5 @@ +require 'aeolus_client/base' + class AeolusClient::ProviderXMLFormat include ActiveResource::Formats::XmlFormat diff --git a/lib/aeolus_client/provider_account.rb b/lib/aeolus_client/provider_account.rb index dca82d1..d5846f3 100644 --- a/lib/aeolus_client/provider_account.rb +++ b/lib/aeolus_client/provider_account.rb @@ -1,3 +1,5 @@ +require 'aeolus_client/base' + class AeolusClient::ProviderAccountXMLFormat include ActiveResource::Formats::XmlFormat diff --git a/lib/aeolus_client/provider_type.rb b/lib/aeolus_client/provider_type.rb index 59c8b82..c90f378 100644 --- a/lib/aeolus_client/provider_type.rb +++ b/lib/aeolus_client/provider_type.rb @@ -1,3 +1,5 @@ +require 'aeolus_client/base' + class AeolusClient::ProviderTypeXMLFormat include ActiveResource::Formats::XmlFormat