Class: Tina4::WSDL::Service

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name:, namespace: "http://tina4.com/wsdl") ⇒ Service

Returns a new instance of Service.



8
9
10
11
12
# File 'lib/tina4/wsdl.rb', line 8

def initialize(name:, namespace: "http://tina4.com/wsdl")
  @name = name
  @namespace = namespace
  @operations = {}
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



6
7
8
# File 'lib/tina4/wsdl.rb', line 6

def name
  @name
end

#namespaceObject (readonly)

Returns the value of attribute namespace.



6
7
8
# File 'lib/tina4/wsdl.rb', line 6

def namespace
  @namespace
end

#operationsObject (readonly)

Returns the value of attribute operations.



6
7
8
# File 'lib/tina4/wsdl.rb', line 6

def operations
  @operations
end

Instance Method Details

#add_operation(name, input_params: {}, output_params: {}, &handler) ⇒ Object



14
15
16
17
18
19
20
# File 'lib/tina4/wsdl.rb', line 14

def add_operation(name, input_params: {}, output_params: {}, &handler)
  @operations[name.to_s] = {
    input: input_params,
    output: output_params,
    handler: handler
  }
end

#generate_wsdl(endpoint_url) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/tina4/wsdl.rb', line 22

def generate_wsdl(endpoint_url)
  xml = '<?xml version="1.0" encoding="UTF-8"?>'
  xml += "\n<definitions xmlns=\"http://schemas.xmlsoap.org/wsdl/\""
  xml += " xmlns:soap=\"http://schemas.xmlsoap.org/wsdl/soap/\""
  xml += " xmlns:tns=\"#{@namespace}\""
  xml += " xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\""
  xml += " name=\"#{@name}\" targetNamespace=\"#{@namespace}\">\n"

  # Types
  xml += "  <types>\n    <xsd:schema targetNamespace=\"#{@namespace}\">\n"
  @operations.each do |op_name, op|
    xml += generate_elements(op_name, op[:input], "Request")
    xml += generate_elements(op_name, op[:output], "Response")
  end
  xml += "    </xsd:schema>\n  </types>\n"

  # Messages
  @operations.each_key do |op_name|
    xml += "  <message name=\"#{op_name}Request\">\n"
    xml += "    <part name=\"parameters\" element=\"tns:#{op_name}Request\"/>\n"
    xml += "  </message>\n"
    xml += "  <message name=\"#{op_name}Response\">\n"
    xml += "    <part name=\"parameters\" element=\"tns:#{op_name}Response\"/>\n"
    xml += "  </message>\n"
  end

  # PortType
  xml += "  <portType name=\"#{@name}PortType\">\n"
  @operations.each_key do |op_name|
    xml += "    <operation name=\"#{op_name}\">\n"
    xml += "      <input message=\"tns:#{op_name}Request\"/>\n"
    xml += "      <output message=\"tns:#{op_name}Response\"/>\n"
    xml += "    </operation>\n"
  end
  xml += "  </portType>\n"

  # Binding
  xml += "  <binding name=\"#{@name}Binding\" type=\"tns:#{@name}PortType\">\n"
  xml += "    <soap:binding style=\"document\" transport=\"http://schemas.xmlsoap.org/soap/http\"/>\n"
  @operations.each_key do |op_name|
    xml += "    <operation name=\"#{op_name}\">\n"
    xml += "      <soap:operation soapAction=\"#{@namespace}/#{op_name}\"/>\n"
    xml += "      <input><soap:body use=\"literal\"/></input>\n"
    xml += "      <output><soap:body use=\"literal\"/></output>\n"
    xml += "    </operation>\n"
  end
  xml += "  </binding>\n"

  # Service
  xml += "  <service name=\"#{@name}\">\n"
  xml += "    <port name=\"#{@name}Port\" binding=\"tns:#{@name}Binding\">\n"
  xml += "      <soap:address location=\"#{endpoint_url}\"/>\n"
  xml += "    </port>\n"
  xml += "  </service>\n"
  xml += "</definitions>"
  xml
end

#handle_soap_request(xml_body) ⇒ Object



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
# File 'lib/tina4/wsdl.rb', line 80

def handle_soap_request(xml_body)
  # Simple SOAP envelope parser
  op_name = nil
  params = {}

  @operations.each_key do |name|
    if xml_body.include?(name)
      op_name = name
      break
    end
  end

  return soap_fault("Unknown operation") unless op_name

  operation = @operations[op_name]

  # Extract parameters from XML
  operation[:input].each_key do |param_name|
    if xml_body =~ /<#{param_name}>(.*?)<\/#{param_name}>/m
      params[param_name.to_s] = Regexp.last_match(1)
    end
  end

  # Execute handler
  result = operation[:handler].call(params)

  # Build SOAP response
  build_soap_response(op_name, result)
rescue => e
  soap_fault(e.message)
end