Class: WashAwayTheSoap

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeWashAwayTheSoap

Returns a new instance of WashAwayTheSoap.



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

def initialize
  @wsdl_operations = []
  @xsd_objects = {}
  @complex_objects = {}
  @temp_objects = {}
  @wsdls = []
  @xsds = []
end

Instance Attribute Details

#complex_objectsObject

Returns the value of attribute complex_objects.



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

def complex_objects
  @complex_objects
end

#temp_objectsObject

Returns the value of attribute temp_objects.



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

def temp_objects
  @temp_objects
end

#wsdl_operationsObject

Returns the value of attribute wsdl_operations.



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

def wsdl_operations
  @wsdl_operations
end

#wsdlsObject

Returns the value of attribute wsdls.



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

def wsdls
  @wsdls
end

#xsd_objectsObject

Returns the value of attribute xsd_objects.



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

def xsd_objects
  @xsd_objects
end

#xsdsObject

Returns the value of attribute xsds.



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

def xsds
  @xsds
end

Class Method Details

.parseObject



9
10
11
# File 'lib/wash_away_the_soap.rb', line 9

def parse
  self.new.parse
end

Instance Method Details

#build_field_attributes(nokogiri_element) ⇒ Object



88
89
90
91
92
93
94
95
# File 'lib/wash_away_the_soap.rb', line 88

def build_field_attributes(nokogiri_element)
  field_type, allow_blank, required = "unknown", false, false

  field_type = nokogiri_element.attributes["type"].value.gsub(/^xs:/, "") if nokogiri_element.attributes["type"]
  allow_blank = (nokogiri_element.attributes["nillable"].value ? true : false) if nokogiri_element.attributes["nillable"]
  required = (nokogiri_element.attributes["minOccurs"].value.to_i > 0 ? true : false) if nokogiri_element.attributes["minOccurs"]
  {"type" => field_type, "allow_blank" => allow_blank, "required" => required}
end

#build_field_options(values) ⇒ Object



97
98
99
100
101
102
103
# File 'lib/wash_away_the_soap.rb', line 97

def build_field_options(values)
  field_options = []
  field_options << values["type"]
  field_options << "required" if values["required"]
  field_options << "no_blanks" unless values["allow_blank"]
  field_options.join(", ")
end

#build_resultsObject



105
106
107
108
109
110
111
112
113
114
115
116
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
# File 'lib/wash_away_the_soap.rb', line 105

def build_results
  output = []
  longest_field_name = 0
  wsdl_operations.each do |operation|
    if xsd_objects[operation]
      xsd_objects[operation].each do |field, values|
        longest_field_name = field.length if field.length > longest_field_name
      end
    end
  end

  wsdl_operations.each do |operation|
    output << operation
    if xsd_objects[operation]
      xsd_objects[operation].each do |field, values|
        if values["type"] =~ /.*:.*/ && complex_objects.keys.include?(values["type"].split(":").last)
          complex_name = values["type"].split(":").last
          complex_line = " #{field.rjust(longest_field_name)}: #{complex_name}"
          output << complex_line
          
          complex_objects[complex_name].each do |object_name, fields|
            object_line = "- #{object_name}".rjust(complex_line.length + 1)
            output << object_line
            fields.each do |field_name, complex_values|
              output << "   #{field_name.rjust(object_line.length + 5)}: #{build_field_options(complex_values)}"
            end
          end
        else
          output << " #{field.rjust(longest_field_name)}: #{build_field_options(values)}"
        end
      end
    else
      output << " - no fields found?"
    end
  end
  output.join("\n") + "\n"
end

#parseObject



23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/wash_away_the_soap.rb', line 23

def parse
  ARGV.each do |argv|
    @wsdls << argv if argv =~ /.wsdl$/
    @xsds << argv if argv =~ /.xsd$/
  end
  wsdls.flatten!
  xsds.flatten!
  wsdls.each { |wsdl_file| parse_wsdl(File.read(wsdl_file), wsdl_file) }
  xsds.each { |xsd_file| parse_xsd(File.read(xsd_file), xsd_file) }
  build_results
  print_results
end

#parse_wsdl(wsdl, filename = nil) ⇒ Object



36
37
38
39
40
41
42
43
44
# File 'lib/wash_away_the_soap.rb', line 36

def parse_wsdl(wsdl, filename = nil)
  doc = Nokogiri::XML(wsdl)
  doc.xpath("//wsdl:operation", doc.root.namespaces).each do |operation|
    wsdl_operations << operation.attributes["name"].value
  end
rescue => e
  puts "Error parsing wsdl file #{filename}"
  exit 1
end

#parse_xsd(xsd, filename = nil) ⇒ Object



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
79
80
81
82
83
84
85
86
# File 'lib/wash_away_the_soap.rb', line 46

def parse_xsd(xsd, filename = nil)
  doc = Nokogiri::XML(xsd)
  doc.xpath("//xs:element//xs:sequence//xs:element", doc.root.namespaces).each do |field|
    operation = field.parent.parent.parent.attributes["name"]
    unless operation.nil?
      next unless operation.value
      next unless field.attributes["name"]
      operation_name = operation.value
      field_name = field.attributes["name"].value

      xsd_objects[operation_name] ||= {}
      xsd_objects[operation_name][field_name] ||= build_field_attributes(field)
    end
  end
  doc.xpath("//xs:complexType//xs:element", doc.root.namespaces).each do |field|
    if field.parent.parent.parent == doc.root
      if field.attributes["type"].value =~ /tns:.*/
        complex_name = field.parent.parent.attributes["name"].value
        field_name = field.attributes["name"].value
        complex_objects[complex_name] ||= {}
        complex_objects[complex_name][field_name] ||= -1
      else
        complex_name = field.parent.parent.attributes["name"].value
        field_name = field.attributes["name"].value
        temp_objects[complex_name] ||= {}
        temp_objects[complex_name][field_name] ||= build_field_attributes(field)
      end
    end
  end
  
  complex_objects.each do |key, hash|
    hash.each do |k, v|
      if v == -1
        complex_objects[key][k] = temp_objects[k] # inplace modification horribleness
      end
    end
  end
rescue => e
  puts "Error parsing xsd file #{filename}\nReason: #{e.to_s}, #{e.backtrace}"
  exit 1
end


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

def print_results
  puts build_results
end