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



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

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.



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

def expand_path(path)
  return nil unless path

  File.expand_path path
end

.get_current_platformObject

Returns the current platform all lower-case.



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

def get_current_platform
  if Maze.mode == :selenium || (Maze.mode == :appium && Maze.config.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.



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

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.



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

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



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

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

.sanitize_url(url) ⇒ Object

Returns URL without any embedded username or password.

Returns:

  • URL without any embedded username or password



124
125
126
127
128
129
# File 'lib/maze/helper.rb', line 124

def sanitize_url(url)
  uri = URI.parse(url)
  uri.user = nil
  uri.password = nil
  uri.to_s
end

.to_friendly_filename(string) ⇒ Object

Returns the name of the scenario to

Parameters:

  • string (String)

    a string to convert to a file name



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

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.



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

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