Class: SimpleWS

Inherits:
SOAP::RPC::StandaloneServer
  • Object
show all
Defined in:
lib/simplews.rb

Direct Known Subclasses

Jobs

Defined Under Namespace

Classes: Jobs

Constant Summary collapse

VERSION =
"1.3.3"

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name = "WS", description = "", host = "localhost", port = "1984", *args) ⇒ SimpleWS

Creates an instance of a Server. The parameter name specifies the namespace used in the WSDL, description is the description also included in the WSDL. The parameters host and port, specify where the server will be listening, they are parameters of the super class SOAP::RPC::StandaloneServer, they are made explicit here because they are included in the WSDL as well.



84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/simplews.rb', line 84

def initialize(name="WS", description="", host="localhost", port="1984", *args)
  super(description, "urn:#{ name }", host, port, *args)
  @host        = host
  @port        = port
  @name        = name
  @description = description
  @messages    = []
  @operations  = []
  @bindings    = []

  serve :wsdl, %w(),  :return => :string
end

Class Method Details

.get_driver(url, name) ⇒ Object

Builds on the get_wsdl function to provide the client with a reference to the driver. Again, only works with SimpleWS servers.



65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/simplews.rb', line 65

def self.get_driver(url, name)
  require 'soap/wsdlDriver'
  require 'fileutils'

  tmp = File.open("/tmp/simpleWS.wsdl",'w')
  tmp.write SimpleWS::get_wsdl(url, name)
  tmp.close
  driver = SOAP::WSDLDriverFactory.new( "/tmp/simpleWS.wsdl"  ).create_rpc_driver
  FileUtils.rm "/tmp/simpleWS.wsdl"

  driver
end

.get_wsdl(url, name) ⇒ Object

This is a helper function for clients. Given the url where the server is listening, as well as the name of the server, it can manually access the wsdl function and retrieve the complete WSDL file. This works only in web servers of class SimpleWS, not on the general SOAP::RPC::StandaloneServer or any other type of web server.



56
57
58
59
60
61
# File 'lib/simplews.rb', line 56

def self.get_wsdl(url, name)
   require 'soap/rpc/driver'
   driver = SOAP::RPC::Driver.new(url, "urn:#{ name }")
   driver.add_method('wsdl')
   driver.wsdl
end

Instance Method Details

#serve(name, args = [], types = {}, &block) ⇒ Object

This method tells the server to provide a method named by the name parameter, with arguments listed in the args parameter. The types parameter specifies the types of the arguments as will be described in the WSDL file (see the TYPES2WSDL constant). The actual method called will be the one with the same name. Optionally a block can be provided, this block will be used to define a function named as in name.

If the method returns something, then the type of the return value must be specified in the types parameter as :return. If not value for :return parameter is specified in the types parameter the method is taken to return no value. Other than that, if a parameter type is omitted it is taken to be :string.



111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/simplews.rb', line 111

def serve(name, args=[], types={}, &block)
  
  if block
    inline_name = "_inline_" + name.to_s
    add_to_ruby(inline_name, &block)
    add_method_as(self,inline_name, name.to_s,*args)
  else
    add_method(self,name.to_s,*args)
  end

  add_to_wsdl(name, args, types)

  nil
end

#wsdl(filename = nil) ⇒ Object

If filename is specified it saves the WSDL file in that file. If no filename is specified it returns a string containing the WSDL. The no parameter version is served by default, so that clients can use this method to access the complete WSDL file, as used in get_wsdl and get_driver.



131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/simplews.rb', line 131

def wsdl(filename = nil)
  wsdl = WSDL_STUB.dup
  wsdl.gsub!(/\$\{MESSAGES\}/m,@messages.join("\n"))
  wsdl.gsub!(/\$\{OPERATIONS\}/m,@operations.join("\n"))
  wsdl.gsub!(/\$\{BINDINGS\}/m,@bindings.join("\n"))
  wsdl.gsub!(/\$\{NAME\}/,@name)
  wsdl.gsub!(/\$\{DESCRIPTION\}/,@description)
  wsdl.gsub!(/\$\{LOCATION\}/,"http://#{ @host }:#{ @port }")
  if filename
    fwsdl = File.open(filename,'w')
    fwsdl.write(wsdl)
    fwsdl.close
    nil
  else
    wsdl
  end
end