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(http, 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(http, globals, locals)
  @http            = http
  @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

#httpObject (readonly)

Returns the value of attribute http.



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

def http
  @http
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



95
96
97
98
99
100
101
102
# File 'lib/savon/response.rb', line 95

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

#bodyObject Also known as: to_hash



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

def body
  find('Body')
end

#docObject



80
81
82
# File 'lib/savon/response.rb', line 80

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

#find(*path) ⇒ Object



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

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



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

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

#hashObject



59
60
61
62
# File 'lib/savon/response.rb', line 59

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



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

def header
  find('Header')
end

#http_error?Boolean

Returns:

  • (Boolean)


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

def http_error?
  HTTPError.present? @http
end

#multipart?Boolean

Returns:

  • (Boolean)


104
105
106
# File 'lib/savon/response.rb', line 104

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

#soap_fault?Boolean

Returns:

  • (Boolean)


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

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

#success?Boolean Also known as: successful?

Returns:

  • (Boolean)


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

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

#to_array(*path) ⇒ Object



49
50
51
52
53
54
55
56
57
# File 'lib/savon/response.rb', line 49

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



68
69
70
71
72
73
74
75
# File 'lib/savon/response.rb', line 68

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

#xpath(path, namespaces = nil) ⇒ Object



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

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