#!/usr/bin/env ruby require 'thor' $LOAD_PATH.unshift File.join(File.dirname(__FILE__), *%w[.. lib]) require 'savon' require 'soaspec' module Soaspec # Common executable for Soaspec class Exe < Thor include Soaspec::ExeHelpers include Soaspec::WsdlGenerator long_desc <<-LONGDESC `soaspec new` will generate the initial files and folders for starting a testing project using soaspec \x5 `soaspec new soap` will create example files testing against a virtual SOAP service \x5 `soaspec new rest` will create example files testing against a virtual REST service LONGDESC desc 'new [type]', 'Initialize soaspec repository' option :ci, default: 'jenkins', banner: 'What Continuous Integration is used' option :virtual, type: :boolean, default: true, banner: 'Whether to set things up for a virtual server' def new(type = 'initial') @virtual = options[:virtual] puts "Creating files for soaspec. options are #{options}" create_file(filename: 'Gemfile') create_file(filename: 'Rakefile') create_file(filename: '.rspec') create_file(filename: '.travis.yml') if options[:ci] == 'travis' create_file(filename: 'README.md') create_folder 'lib' if type == 'soap' create_file filename: 'lib/blz_service.rb' create_file filename: 'lib/shared_example.rb' end create_folder 'config' create_folder 'config/data' create_file(filename: 'config/data/default.yml') create_folder 'spec' create_file(filename: 'spec/spec_helper.rb') create_file(filename: 'spec/soap_spec.rb') if type == 'soap' create_folder 'template' create_file(filename: 'template/soap_template.xml', erb: false) if type == 'soap' create_folder 'logs' puts "Run 'bundle install' to install necessary gems" puts "Run 'rake spec' to run the tests" end desc 'generate', 'Generate initial test code from wsdl' long_desc <<-LONGDESC `soaspec generate wsdl=wsdl name=ServiceName ` will generate the initial files and folders to test each operation in a wsdl \x5 Additionally the auth parameter can be used to use basic authentication to retrieve the WSDL. To do use the following `soaspec generate --auth=basic` LONGDESC option :wsdl, required: true, aliases: :w option :name, default: 'Service', aliases: :n option :ci, default: 'jenkins', banner: 'What Continuous Integration is used' option :auth option :string_default, default: 'test string' def generate enter_auth_details if options[:auth] == 'basic' @virtual = false savon_options = { wsdl: options[:wsdl] } savon_options[:basic_auth] = [@auth_name, @auth_password] if options[:auth] == 'basic' @wsdl_doc = Savon.client(**savon_options).wsdl @wsdl_schemas = @wsdl_doc.parser.schemas create_file filename: 'Rakefile', ignore_if_present: true create_file filename: 'Gemfile', ignore_if_present: true create_file(filename: '.rspec') create_file(filename: '.travis.yml') if options[:ci] == 'travis' create_file filename: 'README.md', ignore_if_present: true create_folder 'spec' create_file filename: 'spec/spec_helper.rb', ignore_if_present: true create_folder 'logs' create_folder 'config' create_folder 'config/data' create_folder 'lib' create_file filename: "lib/#{options[:name].snakecase}.rb", content: class_content # Files according to WSDL @wsdl_doc.operations.each do |operation, details| puts "Creating files for operation: #{operation}" @content = "default:\n" @use_camel_case = false puts 'Message params: ' + details.to_s # From namespace identifier, find namespace, and for that find schemaLocation xsd and use that to build request if details[:parameters] details[:parameters].each do |element, details| @use_camel_case = true if /[[:upper:]]/.match(element.to_s[0]) != nil @content += " #{element.to_s.snakecase}: #{fill_in_field_from_type(details[:type])} # #{details[:type]} \n" # TODO: If details is a Hash need to loop again end end wsdl_to_yaml_for root_elements_for(details) params = [] params << 'convert_request_keys_to: :camelcase' if @use_camel_case params_string = params == [] ? '' : ', ' + params.join(', ') @class_params = "'#{camel_case(operation)}'#{params_string}" create_file(filename: "config/data/#{operation}.yml", content: @content) create_file(filename: "spec/#{operation}_spec.rb", content: generated_soap_spec_for(operation)) end end end end Soaspec::Exe.start(ARGV)