Class: Savon::Response

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

Constant Summary collapse

CRLF =
/\r\n/
WSP =
/[\t ]/

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(transport_response, globals, locals) ⇒ Response

Returns a new instance of Response.



12
13
14
15
16
17
18
19
20
21
22
# File 'lib/savon/response.rb', line 12

def initialize(transport_response, globals, locals)
  @transport_response = transport_response
  @globals            = globals
  @locals             = locals
  @attachments        = []
  @xml                = ''
  @has_parsed_body    = false

  build_soap_and_http_errors!
  raise_soap_and_http_errors! if @globals[:raise_errors]
end

Instance Attribute Details

#globalsObject (readonly)

Returns the value of attribute globals.



24
25
26
# File 'lib/savon/response.rb', line 24

def globals
  @globals
end

#http_errorObject (readonly)

Returns the value of attribute http_error.



24
25
26
# File 'lib/savon/response.rb', line 24

def http_error
  @http_error
end

#localsObject (readonly)

Returns the value of attribute locals.



24
25
26
# File 'lib/savon/response.rb', line 24

def locals
  @locals
end

#soap_faultObject (readonly)

Returns the value of attribute soap_fault.



24
25
26
# File 'lib/savon/response.rb', line 24

def soap_fault
  @soap_fault
end

Instance Method Details

#attachmentsObject



99
100
101
102
103
104
105
106
# File 'lib/savon/response.rb', line 99

def attachments
  if multipart?
    parse_body unless @has_parsed_body
    @attachments
  else
    []
  end
end

#bodyObject Also known as: to_hash



47
48
49
# File 'lib/savon/response.rb', line 47

def body
  find('Body')
end

#docObject



84
85
86
# File 'lib/savon/response.rb', line 84

def doc
  @doc ||= Nokogiri.XML(xml)
end

#find(*path) ⇒ Object



92
93
94
95
96
97
# File 'lib/savon/response.rb', line 92

def find(*path)
  envelope = nori.find(full_hash, 'Envelope')
  raise_invalid_response_error! unless envelope.is_a?(Hash)

  nori.find(envelope, *path)
end

#full_hashObject



68
69
70
# File 'lib/savon/response.rb', line 68

def full_hash
  @full_hash ||= nori.parse(xml)
end

#hashObject



63
64
65
66
# File 'lib/savon/response.rb', line 63

def hash
  warn "Savon::Response#hash is deprecated and will be removed in version 3 - use #full_hash instead", uplevel: 1
  full_hash
end

#headerObject



43
44
45
# File 'lib/savon/response.rb', line 43

def header
  find('Header')
end

#httpObject



26
27
28
# File 'lib/savon/response.rb', line 26

def http
  @transport_response
end

#http_error?Boolean

Returns:

  • (Boolean)


39
40
41
# File 'lib/savon/response.rb', line 39

def http_error?
  HTTPError.present? http
end

#multipart?Boolean

Returns:

  • (Boolean)


108
109
110
# File 'lib/savon/response.rb', line 108

def multipart?
  !(http.headers['content-type'] =~ /^multipart/im).nil?
end

#soap_fault?Boolean

Returns:

  • (Boolean)


35
36
37
# File 'lib/savon/response.rb', line 35

def soap_fault?
  SOAPFault.present?(http, xml)
end

#success?Boolean Also known as: successful?

Returns:

  • (Boolean)


30
31
32
# File 'lib/savon/response.rb', line 30

def success?
  !soap_fault? && !http_error?
end

#to_array(*path) ⇒ Object



53
54
55
56
57
58
59
60
61
# File 'lib/savon/response.rb', line 53

def to_array(*path)
  result = path.inject(body) { |memo, key|
    return [] if memo[key].nil?

    memo[key]
  }

  result.is_a?(Array) ? result.compact : [result].compact
end

#xmlObject Also known as: to_xml, to_s



72
73
74
75
76
77
78
79
# File 'lib/savon/response.rb', line 72

def xml
  if multipart?
    parse_body unless @has_parsed_body
    @xml
  else
    http.body
  end
end

#xpath(path, namespaces = nil) ⇒ Object



88
89
90
# File 'lib/savon/response.rb', line 88

def xpath(path, namespaces = nil)
  doc.xpath(path, namespaces || xml_namespaces)
end