Module: Soaspec::ExeHelpers

Included in:
GenerateServer, WsdlGenerator
Defined in:
lib/soaspec/exe_helpers.rb

Overview

Help with tasks common to soaspec executables

Instance Method Summary collapse

Instance Method Details

#class_contentString

Returns Create class representing wsdl in general.

Returns:

  • (String)

    Create class representing wsdl in general



82
83
84
# File 'lib/soaspec/exe_helpers.rb', line 82

def class_content
  ERB.new(File.read(File.join(File.dirname(__FILE__), 'generator', 'lib/dynamic_class_content.rb.erb'))).result(binding)
end

#create_file(filename: nil, content: nil, ignore_if_present: false, erb: true) ⇒ String

Returns String describing if file created or not.

Parameters:

  • filename (String) (defaults to: nil)

    Name of the file to create

  • content (String) (defaults to: nil)

    Content to place inside file

  • ignore_if_present (Boolean) (defaults to: false)

    Don’t complain if file is present

Returns:

  • (String)

    String describing if file created or not



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/soaspec/exe_helpers.rb', line 49

def create_file(filename: nil, content: nil, ignore_if_present: false, erb: true)
  raise 'Need to pass filename' unless filename

  content ||= retrieve_contents(filename, erb)
  create_folder File.split(filename).first
  msg = if File.exist? filename
          old_content = File.read(filename)
          if old_content != content && !ignore_if_present
            "!! #{filename} already exists and differs from template"
          else
            "#{filename} already exists"
          end
        else
          File.open(filename, 'w') { |f| f.puts content }
          "Created: #{filename}"
        end
  puts msg
  msg
end

#create_files(filenames, ignore_if_present: false) ⇒ Object

Parameters:

  • filenames (Array)

    List of files to create

Raises:

  • (ArgumentError)


20
21
22
23
24
# File 'lib/soaspec/exe_helpers.rb', line 20

def create_files(filenames, ignore_if_present: false)
  raise ArgumentError, 'Expected filenames to be an Array' unless filenames.is_a? Array

  filenames.each { |name| create_file filename: name, ignore_if_present: ignore_if_present }
end

#create_files_for(type) ⇒ Object

Create files in project depending on type of project

Parameters:

  • type (String)

    Type of project to create, e.g., ‘soap’, ‘rest’



7
8
9
10
11
12
13
14
15
16
17
# File 'lib/soaspec/exe_helpers.rb', line 7

def create_files_for(type)
  case type
  when 'soap'
    create_files %w[lib/blz_service.rb lib/shared_example.rb spec/soap_spec.rb]
    create_file(filename: 'template/soap_template.xml', erb: false)
  when 'rest'
    create_files %w[spec/rest_spec.rb lib/package_service.rb]
  else
    # TODO: This needs to have placeholders explaining what to fill in
  end
end

#create_folder(folder) ⇒ Object

Create folder if there’s not a file already there. Will create parent folder if necessary.

Parameters:

  • folder (String)

    Folder to create



72
73
74
75
76
77
78
79
# File 'lib/soaspec/exe_helpers.rb', line 72

def create_folder(folder)
  if File.exist? folder
    warn "!! #{folder} already exists and is not a directory" unless File.directory? folder
  else
    FileUtils.mkdir_p folder
    puts "Created folder: #{folder}/"
  end
end

#generated_soap_spec_for(operation) ⇒ Object

Create a spec for an WSDL operation

Parameters:

  • operation (String)

    Used in ERB to create a test for a WSDL operation



88
89
90
# File 'lib/soaspec/exe_helpers.rb', line 88

def generated_soap_spec_for(operation)
  ERB.new(File.read(File.join(File.dirname(__FILE__), 'generator', 'spec/dynamic_soap_spec.rb.erb'))).result(binding)
end

#retrieve_contents(filename, erb = true) ⇒ Object

Retrieve default file contents based on filename

Parameters:

  • filename (String)

    Filename within ‘lib/generator’ to file retrieve contents from

  • erb (Boolean) (defaults to: true)

    Whether to process file with ERB



35
36
37
38
39
40
41
42
43
# File 'lib/soaspec/exe_helpers.rb', line 35

def retrieve_contents(filename, erb = true)
  default_file = if filename.start_with?('../')
                   File.join(File.dirname(__FILE__), filename[3..-1] + (erb ? '.erb' : ''))
                 else
                   File.join(File.dirname(__FILE__), 'generator', filename + (erb ? '.erb' : ''))
                 end
  contents = File.read(default_file)
  erb ? ERB.new(contents).result(binding) : contents
end

#spec_taskObject

Spec task string depending upon whether virtual is used



27
28
29
30
# File 'lib/soaspec/exe_helpers.rb', line 27

def spec_task
  task_name = options[:virtual] ? 'spec: :start_test_server' : ':spec'
  "RSpec::Core::RakeTask.new(#{task_name}) do |t|"
end