Module: Soaspec::ExeHelpers

Included in:
Baseline, 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



84
85
86
# File 'lib/soaspec/exe_helpers.rb', line 84

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



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

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)


22
23
24
25
26
# File 'lib/soaspec/exe_helpers.rb', line 22

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’



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

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



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

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



90
91
92
# File 'lib/soaspec/exe_helpers.rb', line 90

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



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

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



29
30
31
32
# File 'lib/soaspec/exe_helpers.rb', line 29

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