From 19b252313bff6e8d9ac1a76065127063f6dfd880 Mon Sep 17 00:00:00 2001 From: mrb Date: Thu, 30 Apr 2015 16:39:00 -0400 Subject: [PATCH] Extract error creation and reduce duplication --- README.md | 2 ++ lib/fakes3/xml_adapter.rb | 35 +++++++++++++++-------------------- 2 files changed, 17 insertions(+), 20 deletions(-) diff --git a/README.md b/README.md index d1c88769..1fabe341 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,5 @@ +[![Code Climate](https://codeclimate.com/github/jubos/fake-s3/badges/gpa.svg)](https://codeclimate.com/github/jubos/fake-s3) + ## Introduction FakeS3 is a lightweight server that responds to the same calls Amazon S3 responds to. It is extremely useful for testing of S3 in a sandbox environment without actually diff --git a/lib/fakes3/xml_adapter.rb b/lib/fakes3/xml_adapter.rb index f575df02..f16b309d 100644 --- a/lib/fakes3/xml_adapter.rb +++ b/lib/fakes3/xml_adapter.rb @@ -1,5 +1,6 @@ require 'builder' require 'time' +require 'ostruct' module FakeS3 class XmlAdapter @@ -46,29 +47,19 @@ def self.error(error) # # def self.error_no_such_bucket(name) - output = "" - xml = Builder::XmlMarkup.new(:target => output) - xml.instruct! :xml, :version=>"1.0", :encoding=>"UTF-8" - xml.Error { |err| - err.Code("NoSuchBucket") - err.Message("The resource you requested does not exist") - err.Resource(name) - err.RequestId(1) - } - output + self.error( + make_error("NoSuchBucket", + "The resource you requested does not exist", + name) + ) end def self.error_bucket_not_empty(name) - output = "" - xml = Builder::XmlMarkup.new(:target => output) - xml.instruct! :xml, :version=>"1.0", :encoding=>"UTF-8" - xml.Error { |err| - err.Code("BucketNotEmpty") - err.Message("The bucket you tried to delete is not empty.") - err.Resource(name) - err.RequestId(1) - } - output + self.error( + make_error("BucketNotEmpty", + "The bucket you tried to delete is not empty.", + name) + ) end def self.error_no_such_key(name) @@ -218,5 +209,9 @@ def self.complete_multipart_result(object) } output end + + def self.make_error(error, message, name) + OpenStruct.new(error: error, message: message, name: name) + end end end