Class: Savon::Response

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

Constant Summary collapse

CRLF =
/\r\n/
WSP =
/[#{%Q|\x9\x20|}]/

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(http, globals, locals) ⇒ Response

Returns a new instance of Response.



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

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.



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

def globals
  @globals
end

#httpObject (readonly)

Returns the value of attribute http.



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

def http
  @http
end

#http_errorObject (readonly)

Returns the value of attribute http_error.



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

def http_error
  @http_error
end

#localsObject (readonly)

Returns the value of attribute locals.



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

def locals
  @locals
end

#soap_faultObject (readonly)

Returns the value of attribute soap_fault.



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

def soap_fault
  @soap_fault
end

Instance Method Details

#attachmentsObject



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

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

#bodyObject Also known as: to_hash



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

def body
  find('Body')
end

#docObject



73
74
75
# File 'lib/savon/response.rb', line 73

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

#find(*path) ⇒ Object



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

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



57
58
59
# File 'lib/savon/response.rb', line 57

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

#headerObject



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

def header
  find('Header')
end

#http_error?Boolean

Returns:

  • (Boolean)


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

def http_error?
  HTTPError.present? @http
end

#multipart?Boolean

Returns:

  • (Boolean)


97
98
99
# File 'lib/savon/response.rb', line 97

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

#soap_fault?Boolean

Returns:

  • (Boolean)


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

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

#success?Boolean Also known as: successful?

Returns:

  • (Boolean)


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

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

#to_array(*path) ⇒ Object



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

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

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

#xmlObject Also known as: to_xml, to_s



61
62
63
64
65
66
67
68
# File 'lib/savon/response.rb', line 61

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

#xpath(path, namespaces = nil) ⇒ Object



77
78
79
# File 'lib/savon/response.rb', line 77

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