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

INHERITED_METHODS =

Saves method defined in the class to be served by the instances

{}
@@last_description =

Add a description for the next method served.

nil
@@last_param_description =
nil

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name = nil, description = nil, host = nil, port = nil, *args, &block) ⇒ 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.



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
# File 'lib/simplews.rb', line 88

def initialize(name=nil, description=nil, host=nil, port=nil, *args, &block)
  name ||= self.class.to_s
  description ||= "Web Server for #{ name }"
  host ||= "localhost"
  port ||= "1984"

  super(description, "urn:#{ name }", host, port, *args)

  @host        = host
  @port        = port
  @name        = name
  @description = description
  @messages    = []
  @operations  = []
  @bindings    = []
  @method_descriptions = {}
  @method_order        = []

  if block_given?
    instance_eval &block
  end

  puts "Server #{ name } at #{ host }:#{ port }"
  
 
  desc "Return the WSDL describing the web server"
  param_desc :return => "WSDL description"
  serve :wsdl, %w(),  :return => :string

  desc "Return HMLT table with documentation for the web service"
  param_desc :return => "HTML table with documentation"
  serve :documentation, %w(),  :return => :string

  INHERITED_METHODS.each{|name, info|
    @@last_description = info[:description]
    @@last_param_description = info[:param_descriptions]
    serve name, info[:args], info[:types], &info[:block]
  }
end

Instance Attribute Details

#descriptionObject

Returns the value of attribute description.



80
81
82
# File 'lib/simplews.rb', line 80

def description
  @description
end

Class Method Details

.desc(text = "") ⇒ Object

Add a description for the next method served defined in at the class level



148
149
150
# File 'lib/simplews.rb', line 148

def self.desc(text = "")
  @@last_description = text
end

.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.



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

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.



58
59
60
61
62
63
# File 'lib/simplews.rb', line 58

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

.param_desc(param_descriptions = {}) ⇒ Object

Add descriptions for the parameters of the next method served at the class level



154
155
156
157
# File 'lib/simplews.rb', line 154

def self.param_desc(param_descriptions = {})
  @@last_param_description = {} 
  param_descriptions.each{|param, description| @@last_param_description[param.to_s] = description}
end

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

Saves the method to be served by the instances. The initialization of an instance check if there where any methods declared to be served in the class and add them.



197
198
199
200
201
202
# File 'lib/simplews.rb', line 197

def self.serve(name, args=[], types={}, &block)
  INHERITED_METHODS[name] = {:args => args, :types => types, :block => block, 
    :description => @@last_description, :param_descriptions => @@last_param_description}
  @@last_description = nil
  @@last_param_description = nil
end

Instance Method Details

#desc(text = "") ⇒ Object



137
138
139
# File 'lib/simplews.rb', line 137

def desc(text = "")
  @@last_description = text
end

#documentation(filename = nil) ⇒ Object



228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
# File 'lib/simplews.rb', line 228

def documentation(filename = nil)
  html_table =  Builder::XmlMarkup.new(:indent => 2).table :class => "WS_documentation" do |xml|
    xml.thead do 
      xml.tr do
        xml.th "Operation", :class => "WS_operation" 
        xml.th "Parameters", :class => "WS_parameters"
        xml.th "Documentation", :class => "WS_documentation" 
      end
    end
    xml.tbody do
      @method_order.each do |method|
        desc = @method_descriptions[method][:description] || ""
        case
        when method.to_s == 'wsdl' || method.to_s == 'documentation'
          type = 'WS_documentation'
        when desc =~ /^Job management:/
          type = 'WS_job_management'
        when @method_descriptions[method][:args].include?( :sugested_name )
          type = 'WS_task'
        else
          type = 'WS_normal'
        end
        xml.tr :class => type, :id => "WS_method_#{method}" do
          xml.td method.to_s, :class => "WS_operation" 

          xml.td :class => "WS_parameters" do
            description = @method_descriptions[method]
            method_parameters = description[:args]
            method_parameters += ['return'] unless  description[:types][:return] == false || description[:types]['return'] == false
            if method_parameters.any?
              xml.dl :class => "WS_parameter_documentation" do
                method_parameters.each do |param|
                  if param.to_s == 'return'
                    xml.dt param, :class => 'WS_return' 
                  else
                    xml.dt param
                  end
                  
                  if description[:param_descriptions]
                    xml.dd description[:param_descriptions][param.to_s] || "" 
                  else
                    xml.dd
                  end
                end
              end
            end
          end
          xml.td desc, :class => "WS_documentation" 
        end
      end
    end
  end

  if filename
    directory = File.dirname(File.expand_path(filename))
    FileUtils.mkdir_p directory unless File.exists? directory
    File.open(filename,'w') {|f| f.write html_table }
    nil
  else
    html_table
  end
end

#name=(name) ⇒ Object



128
129
130
131
132
# File 'lib/simplews.rb', line 128

def name=(name)
  @name = name
  appname = name
  default_namespace = "urn:#{name}"
end

#param_desc(param_descriptions = {}) ⇒ Object

Add descriptions for the parameters of the next method served



142
143
144
145
# File 'lib/simplews.rb', line 142

def param_desc(param_descriptions = {})
  @@last_param_description = {} 
  param_descriptions.each{|param, description| @@last_param_description[param.to_s] = description}
end

#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.



172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
# File 'lib/simplews.rb', line 172

def serve(name, args=[], types={}, &block)

  @method_descriptions[name] = {:args => args, :types => types, :block => block, 
    :description => @@last_description, :param_descriptions => @@last_param_description}
  
  @@last_description = nil
  @@last_param_description = nil

  @method_order << name
  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.



209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
# File 'lib/simplews.rb', line 209

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
    directory = File.dirname(File.expand_path(filename))
    FileUtils.mkdir_p directory unless File.exists? directory
    File.open(filename,'w') {|f| f.write wsdl }
    nil
  else
    wsdl
  end
end