Class: TrivialSoap

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

Direct Known Subclasses

RbVmomi::Soap

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts) ⇒ TrivialSoap

Returns a new instance of TrivialSoap.



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/trivial_soap.rb', line 22

def initialize opts
  fail unless opts.is_a? Hash
  @opts = opts
  return unless @opts[:host] # for testcases
  @http = Net::HTTP.new(@opts[:host], @opts[:port], @opts[:proxyHost], @opts[:proxyPort])
  if @opts[:ssl]
    require 'net/https'
    @http.use_ssl = true
    @http.verify_mode = OpenSSL::SSL::VERIFY_NONE # XXX
    @http.cert = OpenSSL::X509::Certificate.new(@opts[:cert]) if @opts[:cert]
    @http.key = OpenSSL::PKey::RSA.new(@opts[:key]) if @opts[:key]
  end
  @http.set_debug_output(STDERR) if $DEBUG
  @http.read_timeout = 60
  @http.open_timeout = 5
  @http.start
  @debug = @opts[:debug]
  @cookie = nil
  @lock = Mutex.new
end

Instance Attribute Details

Returns the value of attribute cookie.



19
20
21
# File 'lib/trivial_soap.rb', line 19

def cookie
  @cookie
end

#debugObject

Returns the value of attribute debug.



19
20
21
# File 'lib/trivial_soap.rb', line 19

def debug
  @debug
end

#httpObject (readonly)

Returns the value of attribute http.



20
21
22
# File 'lib/trivial_soap.rb', line 20

def http
  @http
end

Instance Method Details

#request(action, &b) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/trivial_soap.rb', line 56

def request action, &b
  headers = { 'content-type' => 'text/xml; charset=utf-8', 'SOAPAction' => action }
  headers['cookie'] = @cookie if @cookie
  body = soap_envelope(&b).target!
  
  if @debug
    $stderr.puts "Request:"
    $stderr.puts body
    $stderr.puts
  end

  start_time = Time.now
  response = @lock.synchronize { profile(:post) { @http.request_post(@opts[:path], body, headers) } }
  end_time = Time.now

  @cookie = response['set-cookie'] if response.key? 'set-cookie'

  nk = Nokogiri(response.body)

  if @debug
    $stderr.puts "Response (in #{'%.3f' % (end_time - start_time)} s)"
    $stderr.puts nk
    $stderr.puts
  end

  nk.xpath('//soapenv:Body/*').select(&:element?).first
end

#soap_envelopeObject



43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/trivial_soap.rb', line 43

def soap_envelope
  xsd = 'http://www.w3.org/2001/XMLSchema'
  env = 'http://schemas.xmlsoap.org/soap/envelope/'
  xsi = 'http://www.w3.org/2001/XMLSchema-instance'
  xml = Builder::XmlMarkup.new :indent => 0
  xml.tag!('env:Envelope', 'xmlns:xsd' => xsd, 'xmlns:env' => env, 'xmlns:xsi' => xsi) do
    xml.tag!('env:Body') do
      yield xml if block_given?
    end
  end
  xml
end