Class: UberS3::Response

Inherits:
Object
  • Object
show all
Defined in:
lib/uber-s3/response.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Response

Returns a new instance of Response.



6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/uber-s3/response.rb', line 6

def initialize(options={})
  if !([:status, :header, :body, :raw] - options.keys).empty?
    raise "Expecting keys :status, :header, :body and :raw"
  end
  
  self.status = options[:status]
  self.header = options[:header]
  self.body   = options[:body]
  self.raw    = options[:raw]
  
  success?
end

Instance Attribute Details

#bodyObject

Returns the value of attribute body.



4
5
6
# File 'lib/uber-s3/response.rb', line 4

def body
  @body
end

#error_keyObject

Returns the value of attribute error_key.



4
5
6
# File 'lib/uber-s3/response.rb', line 4

def error_key
  @error_key
end

#error_messageObject

Returns the value of attribute error_message.



4
5
6
# File 'lib/uber-s3/response.rb', line 4

def error_message
  @error_message
end

#headerObject

Returns the value of attribute header.



4
5
6
# File 'lib/uber-s3/response.rb', line 4

def header
  @header
end

#rawObject

Returns the value of attribute raw.



4
5
6
# File 'lib/uber-s3/response.rb', line 4

def raw
  @raw
end

#statusObject

Returns the value of attribute status.



4
5
6
# File 'lib/uber-s3/response.rb', line 4

def status
  @status
end

Instance Method Details

#errorObject



41
42
43
44
45
# File 'lib/uber-s3/response.rb', line 41

def error
  if !error_key.nil?
    "#{error_key}: #{error_message}"
  end
end

#success?Boolean

TODO: can/should we normalize the keys..? downcase.. etc.? def header=(header) end

Returns:

  • (Boolean)


23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/uber-s3/response.rb', line 23

def success?
  return if status < 400 || body.to_s.empty?
  
  # Errors are XML
  doc = Util::XmlDocument.new(body)
  
  self.error_key      = doc.xpath('//Error/Code').first.text
  self.error_message  = doc.xpath('//Error/Message').first.text
  
  error_klass = instance_eval("Error::#{error_key}") rescue nil
  
  if error_klass.nil?
    raise Error::Unknown, "HTTP Response: #{status}, Body: #{body}"
  else
    raise error_klass.new(error_key, error_message)
  end
end