Class: Handsoap::Compiler

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

Overview

Used internally by the generator to generate a Service stub.

Instance Method Summary (collapse)

Constructor Details

- (Compiler) initialize(wsdl, basename = nil)

:nodoc: all



42
43
44
45
46
47
48
49
50
# File 'lib/handsoap/compiler.rb', line 42

def initialize(wsdl, basename = nil)
  @wsdl = wsdl
  if basename
    @basename = basename.gsub(/[^a-zA-Z0-9]/, "_").gsub(/_+/, "_").gsub(/(^_+|_+$)/, '')
  else
    @basename = @wsdl.service
  end
  @basename = underscore(@basename).gsub(/_service$/, "")
end

Instance Method Details

- (Object) camelize(lower_case_and_underscored_word)



66
67
68
69
70
71
72
# File 'lib/handsoap/compiler.rb', line 66

def camelize(lower_case_and_underscored_word)
  lower_case_and_underscored_word.to_s.gsub(/\/(.?)/) {
    "::" + $1.upcase
  }.gsub(/(^|_)(.)/) {
    $2.upcase
  }
end

- (Object) compile_endpoints(protocol)



104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/handsoap/compiler.rb', line 104

def compile_endpoints(protocol)
  version = protocol == :soap12 ? 2 : 1
  @wsdl.endpoints.select { |endpoint| endpoint.protocol == protocol }.map do |endpoint|
    write do |w|
      w.puts "# wsdl: #{@wsdl.url}"
      w.begin "#{endpoint_name} = {"
      w.puts ":uri => '#{endpoint.url}',"
      w.puts ":version => #{version}"
      w.end "}"
    end
  end
end

- (Object) compile_service(protocol, *options)



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
# File 'lib/handsoap/compiler.rb', line 117

def compile_service(protocol, *options)
  binding = @wsdl.bindings.find { |b| b.protocol == protocol }
  raise "Can't find binding for requested protocol (#{protocol})" unless binding
  write do |w|
    w.puts "# -*- coding: utf-8 -*-"
    w.puts "require 'handsoap'"
    w.puts
    w.begin "class #{service_name} < Handsoap::Service"
    w.puts "endpoint #{endpoint_name}"
    w.begin "def on_create_document(doc)"
    w.puts "# register namespaces for the request"
    w.puts "doc.alias 'tns', '#{@wsdl.target_ns}'"
    w.end
    w.puts
    w.begin "def on_response_document(doc)"
    w.puts "# register namespaces for the response"
    w.puts "doc.add_namespace 'ns', '#{@wsdl.target_ns}'"
    w.end
    w.puts
    w.puts "# public methods"
    @wsdl.interface.operations.each do |operation|
      action = binding.actions.find { |a| a.name == operation.name }
      raise "Can't find action for operation #{operation.name}" unless action
      w.puts
      w.begin "def #{method_name(operation)}"
      # TODO allow :soap_action => :none
      if operation.name != action.soap_action && options.include?(:soap_actions)
        w.puts "soap_action = '#{action.soap_action}'"
        maybe_soap_action = ", soap_action"
      else
        maybe_soap_action = ""
      end
      w.begin((operation.output ? 'response = ' : '') + "invoke('tns:#{operation.name}'#{maybe_soap_action}) do |message|")
      w.puts 'raise "TODO"'
      w.end
      w.end
    end
    w.puts
    w.puts "private"
    w.puts "# helpers"
    w.puts "# TODO"
    w.end
  end
end

- (Object) compile_test(protocol)



162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/handsoap/compiler.rb', line 162

def compile_test(protocol)
  binding = @wsdl.bindings.find { |b| b.protocol == protocol }
  raise "Can't find binding for requested protocol (#{protocol})" unless binding
  write do |w|
    w.puts "# -*- coding: utf-8 -*-"
    w.puts "require 'test_helper'"
    w.puts
    w.puts "# #{service_name}.logger = $stdout"
    w.puts
    w.begin "class #{service_name}Test < Test::Unit::TestCase"
    @wsdl.interface.operations.each do |operation|
      w.puts
      w.begin "def test_#{underscore(operation.name)}"
      w.puts "result = #{service_name}.#{method_name(operation)}"
      w.end
    end
    w.end
  end
end

- (Object) detect_protocol



94
95
96
97
98
99
100
101
102
# File 'lib/handsoap/compiler.rb', line 94

def detect_protocol
  if endpoints.select { |endpoint| endpoint.protocol == :soap12 }.any?
    :soap12
  elsif endpoints.select { |endpoint| endpoint.protocol == :soap11 }.any?
    :soap11
  else
    raise "Can't find any soap 1.1 or soap 1.2 endpoints"
  end
end

- (Object) endpoint_name



90
91
92
# File 'lib/handsoap/compiler.rb', line 90

def endpoint_name
  "#{service_basename.upcase}_SERVICE_ENDPOINT"
end

- (Object) method_name(operation)



74
75
76
77
78
79
80
# File 'lib/handsoap/compiler.rb', line 74

def method_name(operation)
  if operation.name.match /^(get|find|select|fetch)/i
    "#{underscore(operation.name)}"
  else
    "#{underscore(operation.name)}!"
  end
end

- (Object) service_basename



82
83
84
# File 'lib/handsoap/compiler.rb', line 82

def service_basename
  @basename
end

- (Object) service_name



86
87
88
# File 'lib/handsoap/compiler.rb', line 86

def service_name
  camelize(service_basename) + "Service"
end

- (Object) underscore(camel_cased_word)



58
59
60
61
62
63
64
# File 'lib/handsoap/compiler.rb', line 58

def underscore(camel_cased_word)
  camel_cased_word.to_s.gsub(/::/, '/').
    gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
    gsub(/([a-z\d])([A-Z])/,'\1_\2').
    tr("-", "_").
    downcase
end

- (Object) write {|writer| ... }

Yields:

  • (writer)


52
53
54
55
56
# File 'lib/handsoap/compiler.rb', line 52

def write
  writer = CodeWriter.new
  yield writer
  writer.to_s
end