Module: Maze::Helper

Defined in:
lib/maze/helper.rb

Overview

Miscellaneous helper functions.

Class Method Summary collapse

Class Method Details

.error_exit(message) ⇒ Object

Logs the given message and exits the program with a failure status



110
111
112
113
114
# File 'lib/maze/helper.rb', line 110

def error_exit(message)
  $logger.error message
  Bugsnag.notify(message)
  exit false
end

.expand_path(path) ⇒ String

Nil-safe version of File.expand_path

Parameters:

  • path (String)

    Path to expand

Returns:

  • (String)

    Expanded path, or nil if path is nil.



73
74
75
76
77
# File 'lib/maze/helper.rb', line 73

def expand_path(path)
  return nil unless path

  File.expand_path path
end

.get_current_platformObject

Returns the current platform all lower-case.



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/maze/helper.rb', line 91

def get_current_platform
  if Maze.mode == :browser
    os = 'browser'
  else
    os = case Maze.config.farm
         when :bs
           Maze.config.capabilities['platformName']
         else
           Maze.config.os
         end
    os = os&.downcase
  end

  raise('Unable to determine the current platform') if os.nil?

  os
end

.parse_querystring(request) ⇒ Hash

Parses a request’s query string, because WEBrick doesn’t in POST requests

Parameters:

  • request (Hash)

    The received request

Returns:

  • (Hash)

    The parsed query string.



16
17
18
# File 'lib/maze/helper.rb', line 16

def parse_querystring(request)
  CGI.parse(request[:request].query_string)
end

.read_at_arg_file(argument) ⇒ Object

Helps interpret “@file” arguments. I.e. if the argument starts with an “@”, read the contents of the filename given.



81
82
83
84
85
86
87
# File 'lib/maze/helper.rb', line 81

def read_at_arg_file(argument)
  return nil if argument.nil?
  return argument unless argument.start_with? '@'

  file = argument[1..argument.size]
  (File.read file).strip
end

.read_key_path(hash, key_path) ⇒ Any

Enables traversal of a hash using Mongo-style dot notation.

Examples:

hash[0] becomes “hash.array.0.item”

Parameters:

  • hash (Hash)

    The hash to traverse

  • key_path (String)

    The dot notation path within the hash

Returns:

  • (Any)

    The value found by the key path



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/maze/helper.rb', line 28

def read_key_path(hash, key_path)
  value = hash
  key_path.split('.').each do |key|
    if key =~ /^(\d+)$/
      key = key.to_i
      if value.length > key
        value = value[key]
      else
        return nil
      end
    else
      if value.key? key
        value = value[key]
      else
        return nil
      end
    end
  end
  value
end

.to_friendly_filename(string) ⇒ Object

Returns the name of the scenario to

Parameters:

  • string (String)

    a string to convert to a file name



118
119
120
# File 'lib/maze/helper.rb', line 118

def to_friendly_filename(string)
  string.gsub(/[:"& ]/, "_").gsub(/_+/, "_")
end

.valid_bugsnag_integrity_header(request) ⇒ Boolean

Determines if the Bugsnag-Integrity header is valid. Whether a missing header is deemed valid depends on @see Maze.config.enforce_bugsnag_integrity.

Returns:

  • (Boolean)

    True if the header is present and valid, or not present and not enforced. False otherwise.



53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/maze/helper.rb', line 53

def valid_bugsnag_integrity_header(request)
  header = request[:request]['Bugsnag-Integrity']
  return !Maze.config.enforce_bugsnag_integrity if header.nil?

  digests = request[:digests]
  if header.start_with?('sha1')
    computed_digest = "sha1 #{digests[:sha1]}"
  elsif header.start_with?('simple')
    computed_digest = "simple #{digests[:simple]}"
  else
    return false
  end
  header == computed_digest
end