Class: Wasabi::Parser

Inherits:
Object
  • Object
show all
Includes:
XPathHelper
Defined in:
lib/wasabi/parser.rb

Overview

Wasabi::Parser

Parses WSDL documents and remembers their important parts.

Constant Summary

Constants included from XPathHelper

XPathHelper::NAMESPACES

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from XPathHelper

#at_xpath, #xpath

Constructor Details

#initialize(document) ⇒ Parser

Returns a new instance of Parser.



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

def initialize(document)
  self.document = document
  self.operations = {}
  self.namespaces = {}
  self.types = {}
  self.element_form_default = :unqualified
end

Instance Attribute Details

#documentObject

Returns the Nokogiri document.



23
24
25
# File 'lib/wasabi/parser.rb', line 23

def document
  @document
end

#element_form_defaultObject

Returns the elementFormDefault value.



41
42
43
# File 'lib/wasabi/parser.rb', line 41

def element_form_default
  @element_form_default
end

#endpointObject

Returns the SOAP endpoint.



38
39
40
# File 'lib/wasabi/parser.rb', line 38

def endpoint
  @endpoint
end

#namespaceObject

Returns the target namespace.



26
27
28
# File 'lib/wasabi/parser.rb', line 26

def namespace
  @namespace
end

#namespacesObject

Returns a map from namespace identifier to namespace URI.



29
30
31
# File 'lib/wasabi/parser.rb', line 29

def namespaces
  @namespaces
end

#operationsObject

Returns the SOAP operations.



32
33
34
# File 'lib/wasabi/parser.rb', line 32

def operations
  @operations
end

#typesObject

Returns a map from a type name to a Hash with type information.



35
36
37
# File 'lib/wasabi/parser.rb', line 35

def types
  @types
end

Instance Method Details

#find_namespace(type) ⇒ Object



107
108
109
110
# File 'lib/wasabi/parser.rb', line 107

def find_namespace(type)
  schema_namespace = at_xpath(type, "ancestor::xs:schema/@targetNamespace")
  schema_namespace ? schema_namespace.to_s : @namespace
end

#parseObject



43
44
45
46
47
48
# File 'lib/wasabi/parser.rb', line 43

def parse
  parse_namespaces
  parse_endpoint
  parse_operations
  parse_types
end

#parse_endpointObject



61
62
63
64
65
66
67
68
69
70
# File 'lib/wasabi/parser.rb', line 61

def parse_endpoint
  endpoint = at_xpath("wsdl:definitions/wsdl:service//soap11:address/@location")
  endpoint ||= at_xpath("wsdl:definitions/wsdl:service//soap12:address/@location")

  begin
    @endpoint = URI(URI.escape(endpoint.to_s)) if endpoint
  rescue URI::InvalidURIError
    @endpoint = nil
  end
end

#parse_namespacesObject



50
51
52
53
54
55
56
57
58
59
# File 'lib/wasabi/parser.rb', line 50

def parse_namespaces
  element_form_default = at_xpath("wsdl:definitions/wsdl:types/xs:schema/@elementFormDefault")
  @element_form_default = element_form_default.to_s.to_sym if element_form_default

  namespace = at_xpath("wsdl:definitions/@targetNamespace")
  @namespace = namespace.to_s if namespace

  @namespaces = @document.collect_namespaces.
    inject({}) { |result, (key, value)| result.merge(key.gsub(/xmlns:/, "") => value) }
end

#parse_operationsObject



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/wasabi/parser.rb', line 72

def parse_operations
  operations = xpath("wsdl:definitions/wsdl:binding/wsdl:operation")
  operations.each do |operation|
    name = operation.attribute("name").to_s

    soap_action = at_xpath(operation, ".//soap11:operation/@soapAction")
    soap_action ||= at_xpath(operation, ".//soap12:operation/@soapAction")

    if soap_action
      soap_action = soap_action.to_s
      action = soap_action.blank? ? name : soap_action
      input = name.blank? ? action.split("/").last : name
      @operations[input.snakecase.to_sym] = { :action => action, :input => input }
    elsif !@operations[name.snakecase.to_sym]
      @operations[name.snakecase.to_sym] = { :action => name, :input => name }
    end
  end
end

#parse_typesObject



91
92
93
94
95
96
97
# File 'lib/wasabi/parser.rb', line 91

def parse_types
  xpath("wsdl:definitions/wsdl:types/xs:schema/xs:element[@name]").
    each { |type| process_type(at_xpath(type, "./xs:complexType"), type.attribute("name").to_s) }

  xpath("wsdl:definitions/wsdl:types/xs:schema/xs:complexType[@name]").
    each { |type| process_type(type, type.attribute("name").to_s) }
end

#process_type(type, name) ⇒ Object



99
100
101
102
103
104
105
# File 'lib/wasabi/parser.rb', line 99

def process_type(type, name)
  return unless type
  @types[name] ||= { :namespace => find_namespace(type) }

  xpath(type, "./xs:sequence/xs:element").
    each { |inner| @types[name][inner.attribute("name").to_s] = { :type => inner.attribute("type").to_s } }
end