Class: Stamps::Response

Inherits:
Object
  • Object
show all
Defined in:
lib/stamps/response.rb

Overview

Stamps::Response

Represents the response and contains the HTTP response.

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#codeObject

Returns the value of attribute code.



19
20
21
# File 'lib/stamps/response.rb', line 19

def code
  @code
end

#errorsObject

Returns the value of attribute errors.



19
20
21
# File 'lib/stamps/response.rb', line 19

def errors
  @errors
end

#hashObject

Returns the value of attribute hash.



19
20
21
# File 'lib/stamps/response.rb', line 19

def hash
  @hash
end

#httpObject

Returns the value of attribute http.



19
20
21
# File 'lib/stamps/response.rb', line 19

def http
  @http
end

#savonObject

Returns the value of attribute savon.



19
20
21
# File 'lib/stamps/response.rb', line 19

def savon
  @savon
end

#validObject

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_faultsObject

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_errorsObject

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
  message =  '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}): #{message}"
  when 401
    raise Unauthorized, "(#{http.code}): #{message}"
  when 403
    raise Forbidden, "(#{http.code}): #{message}"
  when 404
    raise NotFound, "(#{http.code}): #{message}"
  when 406
    raise NotAcceptable, "(#{http.code}): #{message}"
  when 500
    raise InternalServerError, "Stamps.com had an internal error. (#{http.code}): #{message}"
  when 502..503
    raise ServiceUnavailable, "(#{http.code}): #{message}"
  end
end

#to_hashObject

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

Returns:

  • (Boolean)


31
32
33
# File 'lib/stamps/response.rb', line 31

def valid?
  self.valid
end