Class: Savon::Request

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

Overview

Savon::Request

Handles both WSDL and SOAP HTTP requests.

Constant Summary collapse

ContentType =

Content-Types by SOAP version.

{ 1 => "text/xml", 2 => "application/soap+xml" }
@@log =

Whether to log HTTP requests.

true
@@logger =

The default logger.

Logger.new STDOUT
@@log_level =

The default log level.

:debug

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(endpoint, options = {}) ⇒ Request

Expects a SOAP endpoint String. Also accepts an optional Hash of options for specifying a proxy server.



52
53
54
55
# File 'lib/savon/request.rb', line 52

def initialize(endpoint, options = {})
  @endpoint = URI endpoint
  @proxy = options[:proxy] ? URI(options[:proxy]) : URI("")
end

Instance Attribute Details

#endpointObject (readonly)

Returns the endpoint URI.



58
59
60
# File 'lib/savon/request.rb', line 58

def endpoint
  @endpoint
end

#proxyObject (readonly)

Returns the proxy URI.



61
62
63
# File 'lib/savon/request.rb', line 61

def proxy
  @proxy
end

Class Method Details

.log=(log) ⇒ Object

Sets whether to log HTTP requests.



21
22
23
# File 'lib/savon/request.rb', line 21

def self.log=(log)
  @@log = log
end

.log?Boolean

Returns whether to log HTTP requests.

Returns:

  • (Boolean)


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

def self.log?
  @@log
end

.log_levelObject

Returns the log level.



46
47
48
# File 'lib/savon/request.rb', line 46

def self.log_level
  @@log_level
end

.log_level=(log_level) ⇒ Object

Sets the log level.



41
42
43
# File 'lib/savon/request.rb', line 41

def self.log_level=(log_level)
  @@log_level = log_level
end

.loggerObject

Returns the logger.



36
37
38
# File 'lib/savon/request.rb', line 36

def self.logger
  @@logger
end

.logger=(logger) ⇒ Object

Sets the logger.



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

def self.logger=(logger)
  @@logger = logger
end

Instance Method Details

#basic_auth(username, password) ⇒ Object

Sets the username and password for HTTP basic authentication.



74
75
76
# File 'lib/savon/request.rb', line 74

def basic_auth(username, password)
  @basic_auth = [username, password]
end

#headersObject

Returns the HTTP headers for a SOAP request.



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

def headers
  @headers ||= {}
end

#headers=(headers) ⇒ Object

Sets the HTTP headers for a SOAP request.



69
70
71
# File 'lib/savon/request.rb', line 69

def headers=(headers)
  @headers = headers if headers.kind_of? Hash
end

#httpObject

Returns the Net::HTTP object.



120
121
122
# File 'lib/savon/request.rb', line 120

def http
  @http ||= Net::HTTP::Proxy(@proxy.host, @proxy.port).new @endpoint.host, @endpoint.port
end

#ntlm_auth(username, password) ⇒ Object

Sets the username and password for NTLM authentication.



79
80
81
# File 'lib/savon/request.rb', line 79

def ntlm_auth(username, password)
  @ntlm_auth = [username, password, true]
end

#soap(soap) ⇒ Object

Executes a SOAP request using a given Savon::SOAP instance and returns the Net::HTTP response.



106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/savon/request.rb', line 106

def soap(soap)
  @soap = soap
  http.endpoint @soap.endpoint.host, @soap.endpoint.port
  http.use_ssl = @soap.endpoint.ssl?

  log_request
  @response = http.start do |h|
    h.request request(:soap) { |request| request.body = @soap.to_xml }
  end
  log_response
  @response
end

#wsdl(retries = 0) ⇒ Object

Retrieves WSDL document and returns the Net::HTTP response.



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/savon/request.rb', line 84

def wsdl(retries = 0)
  log "Retrieving WSDL from: #{@endpoint}"
  http.endpoint @endpoint.host, @endpoint.port
  http.use_ssl = @endpoint.ssl?
  response = http.start { |h| h.request request(:wsdl) }
  case response
  when Net::HTTPFound, Net::HTTPMovedPermanently
    puts "REDIRECT: #{@endpoint} -> #{response["location"]}"
    @endpoint = URI response["location"]
    if retries == 10
      raise Savon::HTTPError.new("Redirect too long", {})
    end
    wsdl(retries + 1)
  when Net::HTTPSuccess
    response
  else
    raise Savon::HTTPError.new(response.body.to_s, response.to_hash)
  end
end