Class: Stamps::Response
- Inherits:
-
Object
- Object
- Stamps::Response
- Defined in:
- lib/stamps/response.rb
Overview
Stamps::Response
Represents the response and contains the HTTP response.
Instance Attribute Summary collapse
-
#code ⇒ Object
Returns the value of attribute code.
-
#errors ⇒ Object
Returns the value of attribute errors.
-
#hash ⇒ Object
Returns the value of attribute hash.
-
#http ⇒ Object
Returns the value of attribute http.
-
#savon ⇒ Object
Returns the value of attribute savon.
-
#valid ⇒ Object
Returns the value of attribute valid.
Instance Method Summary collapse
-
#format_soap_faults ⇒ Object
Include any errors in the response.
-
#initialize(response) ⇒ Response
constructor
Expects an
Savon::SOAP::Response
and handles errors. -
#raise_errors ⇒ Object
Process any errors we get back from the service.
-
#to_hash ⇒ Object
Returns the SOAP response body as a Hash.
-
#valid? ⇒ Boolean
Um, there’s gotta be a better way.
Constructor Details
#initialize(response) ⇒ Response
Expects an Savon::SOAP::Response
and handles errors.
10 11 12 13 14 15 16 17 |
# File 'lib/stamps/response.rb', line 10 def initialize(response) self.errors = [] self.valid = true self.savon = response self.http = response.http self.hash = self.savon.to_hash raise_errors end |
Instance Attribute Details
#code ⇒ Object
Returns the value of attribute code.
19 20 21 |
# File 'lib/stamps/response.rb', line 19 def code @code end |
#errors ⇒ Object
Returns the value of attribute errors.
19 20 21 |
# File 'lib/stamps/response.rb', line 19 def errors @errors end |
#hash ⇒ Object
Returns the value of attribute hash.
19 20 21 |
# File 'lib/stamps/response.rb', line 19 def hash @hash end |
#http ⇒ Object
Returns the value of attribute http.
19 20 21 |
# File 'lib/stamps/response.rb', line 19 def http @http end |
#savon ⇒ Object
Returns the value of attribute savon.
19 20 21 |
# File 'lib/stamps/response.rb', line 19 def savon @savon end |
#valid ⇒ Object
Returns the value of attribute valid.
19 20 21 |
# File 'lib/stamps/response.rb', line 19 def valid @valid end |
Instance Method Details
#format_soap_faults ⇒ Object
Include any errors in the response
65 66 67 68 69 |
# File 'lib/stamps/response.rb', line 65 def format_soap_faults fault = self.hash.delete("soap:Fault") || self.hash.delete(:fault) self.errors << (fault[:faultstring] || fault["faultstring"]) self.valid = false end |
#raise_errors ⇒ Object
Process any errors we get back from the service. Wrap any internal errors (from Soap Faults) into an array so that clients can process the error messages as they wish
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/stamps/response.rb', line 39 def raise_errors = 'FIXME: Need to parse http for response message' return self.format_soap_faults if savon.soap_fault? case http.code.to_i when 200 return when 400 raise BadRequest, "(#{http.code}): #{}" when 401 raise Unauthorized, "(#{http.code}): #{}" when 403 raise Forbidden, "(#{http.code}): #{}" when 404 raise NotFound, "(#{http.code}): #{}" when 406 raise NotAcceptable, "(#{http.code}): #{}" when 500 raise InternalServerError, "Stamps.com had an internal error. (#{http.code}): #{}" when 502..503 raise ServiceUnavailable, "(#{http.code}): #{}" end end |
#to_hash ⇒ Object
Returns the SOAP response body as a Hash.
22 23 24 25 26 27 28 |
# File 'lib/stamps/response.rb', line 22 def to_hash self.hash.merge!(:errors => self.errors) self.hash.merge!(:valid? => self.valid) self.hash # binding.pry Stamps.format.to_s.downcase == 'hashie' ? Hashie::Trash.new(@hash) : self.hash end |
#valid? ⇒ Boolean
Um, there’s gotta be a better way
31 32 33 |
# File 'lib/stamps/response.rb', line 31 def valid? self.valid end |