Module: Soaspec::WsdlGenerator

Defined in:
lib/soaspec/wsdl_generator.rb

Instance Method Summary collapse

Instance Method Details

#ask_wsdlObject

Prompt user for wsdl



117
118
119
120
121
122
123
124
# File 'lib/soaspec/wsdl_generator.rb', line 117

def ask_wsdl
  prompt = <<-EOF
Enter WSDL:
  EOF
  print prompt.chop
  @wsdl = $stdin.gets.strip
  puts
end

#camel_case(underscore_separated) ⇒ Object

Parameters:

  • underscore_separated (String, Symbol)

    Snakecase value to be converted to camel case



59
60
61
# File 'lib/soaspec/wsdl_generator.rb', line 59

def camel_case(underscore_separated)
  underscore_separated.to_s.split('_').collect(&:capitalize).join
end

#complex_type?(element) ⇒ Boolean

Returns True if nokogori element is a complex type, that is, has a complexType element underneath itself.

Parameters:

  • (Nokogiri::XML::Element)

Returns:

  • (Boolean)

    True if nokogori element is a complex type, that is, has a complexType element underneath itself



54
55
56
# File 'lib/soaspec/wsdl_generator.rb', line 54

def complex_type?(element)
  element.children.any? { |child| child.name == 'complexType' }
end

#document_type_for(element, depth = 1) ⇒ Object

Parameters:

  • (Nokogiri::XML::Element)


85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/soaspec/wsdl_generator.rb', line 85

def document_type_for(element, depth = 1)
  # raise "Too far deep for #{element}" unless depth < 6
  return unless depth < 6

  if complex_type? element
    complex_type = element.children.find { |c| c.name == 'complexType' }
    sequence = complex_type.children.find { |c| c.name == 'sequence' }
    sequence.children.select { |node| node.class == Nokogiri::XML::Element }.each do |sub_element|
      document_type_for sub_element, depth += 1
    end
  else
    return "No type seen for #{element}, #{element.class}" unless element['type']

    name = element['name']
    type = value_after_namespace(element['type'])
    @use_camel_case = true unless /[[:upper:]]/.match(name[0]).nil?
    spaces = '  ' * depth
    @content += "#{spaces}#{name.snakecase}: #{fill_in_field_from_type(type)} # #{type} \n"
  end
end

#enter_auth_detailsObject

Prompt user to enter basic auth details



137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/soaspec/wsdl_generator.rb', line 137

def enter_auth_details
  prompt = <<-EOF
User Name:
  EOF
  print prompt.chop
  @auth_name = $stdin.gets.strip
  puts

  prompt = <<-EOF
User Password:
  EOF
  print prompt.chop
  @auth_password = $stdin.gets.strip
  puts
end

#enumeration?(type) ⇒ Boolean

Returns Whether WSDL type is an enumeration.

Parameters:

  • (Nokogiri::XML::NodeSet)

Returns:

  • (Boolean)

    Whether WSDL type is an enumeration



23
24
25
26
27
# File 'lib/soaspec/wsdl_generator.rb', line 23

def enumeration?(type)
  return false unless type.first

  !type.xpath("*/#{type.first.namespace.prefix}:enumeration").empty?
end

#fill_in_field_from_type(type) ⇒ Object

Based on WSDL type return a valid value

Parameters:

  • type (String)

    Type without the WSDL



37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/soaspec/wsdl_generator.rb', line 37

def fill_in_field_from_type(type)
  case type
  when 'string'
    options[:string_default] # 'test string'
  when 'int'
    2
  when 'boolean'
    true
  when 'double'
    '1.5'
  else
    try_enum_for type
  end
end

#name_of_wsdlObject

Prompt user for Service name for wsdl



127
128
129
130
131
132
133
134
# File 'lib/soaspec/wsdl_generator.rb', line 127

def name_of_wsdl
  prompt = <<-EOF
Enter what you would like to name WSDL (CamelCase):
  EOF
  print prompt.chop
  @name = $stdin.gets.strip
  puts
end

#root_elements_for(op_details) ⇒ Nokogiri::XML::NodeSet

Returns List of the root elements in the SOAP body.

Parameters:

  • op_details (Hash)

    Hash with details from WSDL for an operation

Returns:

  • (Nokogiri::XML::NodeSet)

    List of the root elements in the SOAP body



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/soaspec/wsdl_generator.rb', line 65

def root_elements_for(op_details)
  raise "'@wsdl_schemas' must be defined" if @wsdl_schemas.nil?

  root_element = @wsdl_schemas.at_xpath("//*[@name='#{op_details[:input]}']")
  raise 'Operation has no input defined' if root_element.nil?

  schema_namespace = root_element.namespace.prefix
  root_type = root_element['type']
  if root_type
    @wsdl_schemas.xpath("//*[@name='#{root_type.split(':').last}']//#{schema_namespace}:element")
  else
    return [] unless complex_type? root_element # Empty Array if there are no root elements

    complex_type = root_element.children.find { |c| c.name == 'complexType' }
    sequence = complex_type.children.find { |c| c.name == 'sequence' }
    sequence.xpath("#{schema_namespace}:element")
  end
end

#try_enum_for(type) ⇒ Object

Attempt to calculate values of enumeration by looking up type in Schema



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/soaspec/wsdl_generator.rb', line 4

def try_enum_for(type)
  raise "'@wsdl_schemas' must be defined" if @wsdl_schemas.nil?

  custom_type = @wsdl_schemas.xpath("//*[@name='#{type}']")
  if enumeration? custom_type
    prefix = custom_type.first.namespace.prefix
    enumerations = custom_type.xpath("//#{prefix}:enumeration")
    return 'Custom Type' if enumerations.empty?

    @enums_values = []
    enumerations.each { |enum_value| @enums_values << "'#{enum_value['value']}'" }
    "~randomize [#{@enums_values.join(', ')}]"
  else
    'Custom Type'
  end
end

#value_after_namespace(string) ⇒ Object

Return value of string after a namespace

Parameters:

  • string (String)

    String to parse for part after namespace



31
32
33
# File 'lib/soaspec/wsdl_generator.rb', line 31

def value_after_namespace(string)
  string.split(':').last
end

#wsdl_to_yaml_for(list) ⇒ Object

Makes a yaml string in a ‘@content’ instance variable

Parameters:

  • list (Nokogiri::XML::NodeSet)

    List



108
109
110
111
112
113
114
# File 'lib/soaspec/wsdl_generator.rb', line 108

def wsdl_to_yaml_for(list)
  raise "'@content' string must be set" if @content.nil?

  list.each do |element|
    document_type_for element
  end
end