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;charset=UTF-8", 2 => "application/soap+xml;charset=UTF-8" }
@@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.



102
103
104
# File 'lib/savon/request.rb', line 102

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

#soap(soap) ⇒ Object

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



88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/savon/request.rb', line 88

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

#wsdlObject

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



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

def wsdl
  log "Retrieving WSDL from: #{@endpoint}"
  http.endpoint @endpoint.host, @endpoint.port
  http.use_ssl = @endpoint.ssl?
  http.start { |h| h.request request(:wsdl) }
end