Module: Exercise::Instructions

Includes:
Commandline, Commandline::Output
Included in:
RenderMethods
Defined in:
lib/commands/exercise/instructions.rb

Overview

module Instructions - Helper methods to be used within templates

Defined Under Namespace

Classes: EnvironmentVariableMissingError, TimeoutError

Instance Method Summary collapse

Methods included from Commandline::Output

#error, #ok, #output, #prefix, #say

Methods included from Commandline

#capture_output, #execute, #run

Instance Method Details

#after_rendering_run(command) ⇒ String

Runs the given command after the current template has been rendered. This is useful for running commands to clean clean up. E.g. stopping a server that was previously started.

Examples:

In ERB template:
<%= after_rendering_run('cic down') %>

Parameters:

  • command (String)

    the command to be run.

Returns:

  • (String)

    the command input parameter is returned so that it can be displayed within a template



31
32
33
34
# File 'lib/commands/exercise/instructions.rb', line 31

def after_rendering_run(command)
  after_rendering_commands << command
  command
end

#capture(url, filename) ⇒ String

Captures and saves screenshots. Useful for adding images to your templates.

Examples:

In ERB template:
![Screenshot](<%= capture('http://the.url.com', 'filename.png') %>)

Parameters:

  • url (String)

    the url to take a screenshot of.

  • filename (String)

    the filename to save the screenshot to.

Returns:

  • (String)

    The filename of the saved screenshot



44
45
46
47
48
49
50
# File 'lib/commands/exercise/instructions.rb', line 44

def capture(url, filename)
  page_class = Class.new { include PageMagic }
  session = PageMagic.session(browser: :headless_chrome, url: url)
  session.visit(page_class, url: url)
  session.browser.save_screenshot(filename)
  filename
end

#cd(path) ⇒ String

Change directory Change directory so that subsequent commands are executed in the correct context

Examples:

In ERB template:
<%= cd('path') %>

Parameters:

  • path (String)

    the path to move to

Returns:

  • (String)

    the path moved to



59
60
61
62
# File 'lib/commands/exercise/instructions.rb', line 59

def cd(path)
  Dir.chdir(path)
  "cd #{path}"
end

#command(command, fail_on_error: true) ⇒ String

Execute a command

Examples:

In ERB template:
<%= command('mkdir my_directory') %>

Parameters:

  • command (String)

    the command to execute

  • fail_on_error (Boolean) (defaults to: true)

    whether to raise an error if the command fails to execute

Returns:

  • (String)

    the command that was executed

Raises:

  • (CommandError)

    if command errors and if fail_on_error is set to to true



72
73
74
75
76
77
# File 'lib/commands/exercise/instructions.rb', line 72

def command(command, fail_on_error: true)
  result = test_command(command, fail_on_error: fail_on_error)
  raise CommandError if result.error? && fail_on_error

  command
end

#command_output(command) ⇒ String

get the output of a command

Examples:

In ERB template:
<%= command_output('mkdir my_directory') %>

Parameters:

  • command (String)

    the command to execute.

Returns:

  • (String)

    the output of the command executed.



85
86
87
88
# File 'lib/commands/exercise/instructions.rb', line 85

def command_output(command)
  command command
  last_command_output
end

#env(variable_name) ⇒ String

get value of an Environment variable # @raise [EnvironmentVariableMissingError] if environment variable is undefined

Examples:

In ERB template:
<%= env('VARIABLE_NAME') %>

Parameters:

  • variable_name (String)

    the variable name

Returns:

  • (String)

    the value of the specified environment variable.



97
98
99
# File 'lib/commands/exercise/instructions.rb', line 97

def env(variable_name)
  ENV[variable_name.to_s] || (raise EnvironmentVariableMissingError, variable_name)
end

#last_command_outputString

get the output of the last command that was run.

Examples:

In ERB template:
<%= last_command_output %>

Returns:

  • (String)

    the output of the last command.



106
107
108
# File 'lib/commands/exercise/instructions.rb', line 106

def last_command_output
  Output.new(@result.stdout)
end

#path(path) ⇒ String

Validate a path

Examples:

In ERB template:
<%= path('the/path') %>

Returns:

  • (String)

    the output of the last command.

Raises:

  • (RuntimeError)

    if given path does not exist



116
117
118
119
120
# File 'lib/commands/exercise/instructions.rb', line 116

def path(path)
  raise "#{path} does not exist" unless File.exist?(path)

  path
end

#substitute(hash) ⇒ Object

store strings that should be subsituted in the template on rendering

Examples:

In ERB template:
<% substitute({'localhost' => '127.0.0.1'}) %>

Parameters:

  • hash (Hash)

    values and their substitutes.



127
128
129
# File 'lib/commands/exercise/instructions.rb', line 127

def substitute(hash)
  @substitutes = hash
end

#wait_until(timeout_after: 5, retry_every: 0.1) ⇒ Object

Wait until the given block evaluates to true

Examples:

In ERB template:
<%
  wait_until do
    # code
  end
%>

Parameters:

  • timeout_after (Fixnum) (defaults to: 5)

    the number of seconds to wait before timing out.

  • retry_every (Float) (defaults to: 0.1)

    the number of seconds to wait before re-evaluating the given block again

Raises:

  • (TimeoutError)

    if given block does not return true within the allowed time.



142
143
144
145
146
147
148
149
150
# File 'lib/commands/exercise/instructions.rb', line 142

def wait_until(timeout_after: 5, retry_every: 0.1)
  start_time = Time.now
  until Time.now > start_time + timeout_after
    return true if yield == true

    sleep retry_every
  end
  raise TimeoutError, 'Action took to long'
end

#write_to_file(path, content) ⇒ String

Write the given content to file

Examples:

In ERB template:
<%
  write_to_file('path/file.txt', 'content')
%>

Parameters:

  • path (String)

    the path to write the file to.

  • content (Float)

    the content to write to file

Returns:

  • (String)

    the path that the file was written to.



161
162
163
164
165
166
167
# File 'lib/commands/exercise/instructions.rb', line 161

def write_to_file(path, content)
  directory = File.dirname(path)
  FileUtils.mkdir_p(directory)
  File.write(path, content)
  after_rendering_run("rm -rf #{path}")
  path
end