Class: SOAP::Request

Inherits:
Object
  • Object
show all
Defined in:
lib/soap/lc/request.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(wsdl, binding) ⇒ Request

:nodoc:



10
11
12
13
14
# File 'lib/soap/lc/request.rb', line 10

def initialize( wsdl, binding ) #:nodoc:
  @wsdl = wsdl
  @binding = binding
  @request = nil
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(id, *args) ⇒ Object

Call a method for the current Request and get a SOAP::Response

Example:

wsdl = SOAP::LC.new( ).wsdl( "http://..." )
request = wsdl.request( )
response = request.myMethod( :param1 => "hello" )
  # => #<SOAP::Response:0xNNNNNN>


23
24
25
# File 'lib/soap/lc/request.rb', line 23

def method_missing( id, *args ) 
  call( id.id2name, args[0] )
end

Class Method Details

.request(envelope, uri, headers = {}) ⇒ Object

Create a new SOAP::Request with the given envelope, uri and headers

Example:

e = '<SOAP-ENV:Envelope 
                 xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
                 xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/"
                 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
                 xmlns:xsd="http://www.w3.org/2001/XMLSchema"
                 SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
       <SOAP-ENV:Header/>
         <SOAP-ENV:Body>
           <HelloWorld xmlns="urn:MyWebService">
             <from>Greg</from>
           </HelloWorld>
         </SOAP-ENV:Body>
       </SOAP-ENV:Envelope>'
r = SOAP::Request.request( e, "http://localhost:3000/hello/wsdl", "SOAPAction" => "my.soap.action" )


44
45
46
47
48
# File 'lib/soap/lc/request.rb', line 44

def self.request( envelope, uri, headers = {} )
  req = new( nil, nil )
  req.r( envelope, uri, headers )
  return req
end

Instance Method Details

#call(methodName, args) ⇒ Object

Call a method for the current Request

Example:

wsdl = SOAP::LC.new( ).wsdl( "http://..." )
request = wsdl.request( )
response = request.call( "myMethod", :param1 => "hello" )
  # => #<SOAP::Response:0xNNNNNN>


73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/soap/lc/request.rb', line 73

def call( methodName, args )
  args = (args || {}).keys_to_sym!

  # Get Binding
  binding = @wsdl.bindings.getBindingForOperationName( @binding, methodName )
  if binding.size == 0
    raise SOAP::LCNoMethodError, "Undefined method `#{methodName}'"
  elsif binding.size > 1
    raise SOAP::LCError, "Ambigous method name `#{methodName}', please, specify a binding name"
  else
    binding = binding[0]
    @binding = binding.name
  end

  # Get Binding Operation
  binding_operation = binding.operations[methodName]

  # Get PortType
  portType = @wsdl.portTypes[binding.type.nns]
  portType_operation = portType.operations[methodName]

  # Get message for input operation
  input_message = @wsdl.messages[portType_operation[:input][:message].nns]

  # Create method
  soap_method = "<#{methodName} xmlns=\"#{@wsdl.targetNamespace}\">\n"
  input_message.parts.each do |_, attrs|
    case attrs[:mode]
      when :type
        if SOAP::XSD::ANY_SIMPLE_TYPE.include?( attrs[attrs[:mode]].nns )
          # Part refer to a builtin SimpleType
          soap_method << SOAP::XSD.displayBuiltinType( attrs[:name], args, 1, 1 )
        else
          # Part refer to an XSD simpleType or complexType defined in types
          element = @wsdl.types[attrs[attrs[:mode]].nns][:value]
          case element[:type]
            when :simpleType
              soap_method << "<#{attrs[:name]}>\n#{element.display( @wsdl.types, args )}\n</#{attrs[:name]}>\n" # MAYBE ########## 
            when :complexType
              soap_method << "<#{attrs[:name]}>\n#{element.display( @wsdl.types, args )}\n</#{attrs[:name]}>\n" # MAYBE ########## 
            else
              raise SOAP::LCWSDLError, "Malformated part #{attrs[:name]}"
          end
        end
      when :element
        # Part refer to an XSD element
        element = @wsdl.types[attrs[attrs[:mode]].nns][:value]
        case element[:type]
          when :simpleType
            soap_method << element[element[:type]].display( @wsdl.types, args )
          when :complexType
            soap_method << element[element[:type]].display( @wsdl.types, args )
          else
            raise SOAL::LCWSDLError, "Malformated element `#{attrs[attrs[:mode]]}'"
        end
    
        ## TODO ---------- USE element[:key]
      else 
        raise SOAP::LCWSDLError, "Malformated part #{attrs[:name]}"
    end
  end
  soap_method += "</#{methodName}>\n"

  # Create SOAP Envelope
  envelope = soap_envelop do 
    soap_header + soap_body( soap_method )
  end

  # Create headers
  headers = Hash.new
  # Add SOAPAction to headers (if exist)
  action = begin
    binding_operation[:soapAction]
  rescue
    nil
  end
  headers['SOAPAction'] = action unless action.nil? or action.length == 0

  # Search URI
  service_port = @wsdl.services.getServicePortForBindingName( binding.name )
  address = service_port[:address]

  # Complete request
  @request = {
    :headers  => make_header( envelope, headers ),
    :envelope => envelope,
    :uri      => address,
    :wsdl     => @wsdl,
    :response => @wsdl.messages[portType_operation[:output][:message].nns].name,
    :binding  => @binding,
    :method   => methodName
  }
  
  return self
end

#envelopeObject

Get the SOAP Envelope for the request



181
182
183
# File 'lib/soap/lc/request.rb', line 181

def envelope( )
  @request[:envelope] || nil
end

#headersObject

Get request headers



186
187
188
# File 'lib/soap/lc/request.rb', line 186

def headers( )
  @request[:headers] || nil
end

#operationsObject

Return available SOAP actions



62
63
64
# File 'lib/soap/lc/request.rb', line 62

def operations
  @wsdl.bindings.getOperations( @binding )
end

#r(envelope, uri, headers) ⇒ Object

:nodoc:



49
50
51
52
53
54
55
56
57
58
59
# File 'lib/soap/lc/request.rb', line 49

def r( envelope, uri, headers ) #:nodoc:
  @request = {
    :headers  => make_header( envelope, headers ),
    :envelope => envelope,
    :uri      => uri,
    :wsdl     => nil,
    :response => nil,
    :binding  => @binding,
    :method   => nil
  }
end

#responseObject Also known as: result

Send request to the server and get a response (SOAP::Response)



175
176
177
# File 'lib/soap/lc/request.rb', line 175

def response( )
  return( SOAP::Response.new( @request ) )
end

#soap_body(soap_method) ⇒ Object

Get the SOAP Body for the request



170
171
172
# File 'lib/soap/lc/request.rb', line 170

def soap_body( soap_method )
  "<SOAP-ENV:Body>\n" + soap_method + "</SOAP-ENV:Body>\n"
end

#uriObject

Get request URI



191
192
193
# File 'lib/soap/lc/request.rb', line 191

def uri()
  @request[:uri] || nil
end