Class: RightDevelop::Testing::Server::MightApi::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/right_develop/testing/servers/might_api/lib/config.rb

Constant Summary collapse

CONFIG_DIR_NAME =

default config path.

'config'.freeze
DEFAULT_CONFIG_PATH =
::File.join(CONFIG_DIR_NAME, 'might_deploy.yml').freeze
METADATA_CLASS =
::RightDevelop::Testing::Recording::Metadata
CONFIG_CLASS =
::RightDevelop::Testing::Recording::Config

Class Method Summary collapse

Class Method Details

.const_defined?(konst) ⇒ Boolean

Returns:

  • (Boolean)

See Also:

  • Class.const_defined?


100
101
102
# File 'lib/right_develop/testing/servers/might_api/lib/config.rb', line 100

def self.const_defined?(konst)
  super(konst) || CONFIG_CLASS.const_defined?(konst)
end

.const_missing(konst) ⇒ Object

See Also:

  • Class.const_missing


91
92
93
94
95
96
97
# File 'lib/right_develop/testing/servers/might_api/lib/config.rb', line 91

def self.const_missing(konst)
  if CONFIG_CLASS.const_defined?(konst)
    CONFIG_CLASS.const_get(konst)
  else
    super
  end
end

.environmentString

Returns environment configuration string.

Returns:

  • (String)

    environment configuration string



105
106
107
# File 'lib/right_develop/testing/servers/might_api/lib/config.rb', line 105

def self.environment
  @environment ||= ::ENV['RACK_ENV']
end

.from_file(path, options = nil) ⇒ Mash

Loads the config hash from given path or a relative location.

Parameters:

  • path (String)

    to configuration

Returns:

  • (Mash)

    configuration hash

Raises:

  • (ArgumentError)

    on failure to load



58
59
60
61
# File 'lib/right_develop/testing/servers/might_api/lib/config.rb', line 58

def self.from_file(path, options = nil)
  @config = CONFIG_CLASS.from_file(path, options)
  self
end

.from_hash(config_hash) ⇒ Config

Setup configuration. Defaults to using environment variables for setup due to rackup not allowing custom arguments to be passed on command line.

Parameters:

  • config (Hash)

    as raw configuration data

Returns:

Raises:

  • (ArgumentError)

    on failure to load



71
72
73
74
# File 'lib/right_develop/testing/servers/might_api/lib/config.rb', line 71

def self.from_hash(config_hash)
  @config = CONFIG_CLASS.new(config_hash)
  self
end

.method_missing(methud, *args, &block) ⇒ Object

See Also:

  • Object#method_missing


77
78
79
80
81
82
83
# File 'lib/right_develop/testing/servers/might_api/lib/config.rb', line 77

def self.method_missing(methud, *args, &block)
  if @config && @config.respond_to?(methud)
    @config.__send__(methud, *args, &block)
  else
    super
  end
end

.normalize_fixtures_dir(logger) ⇒ Object



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
# File 'lib/right_develop/testing/servers/might_api/lib/config.rb', line 109

def self.normalize_fixtures_dir(logger)
  # remove any residual state files at root of fixtures directory.
  logger.info("Normalizing fixtures directory: #{fixtures_dir.inspect} ...")
  ::Dir[::File.join(fixtures_dir, '*.yml')].each do |path|
    ::File.unlink(path) if ::File.file?(path)
  end

  # recursively iterate requests/responses ensuring that both files exist
  # and that they are MD5-named. if not, then supply the MD5 by renaming
  # both files. this allows a user to write custom request/responses without
  # having to supply the MD5 checksum for the significant request parts.
  ::Dir[::File.join(fixtures_dir, '*/*')].sort.each do |epoch_api_dir|
    if ::File.directory?(epoch_api_dir)
      # request/response pairs must be identical whether or not using human-
      # readable file names.
      route_subdir_name = ::File.basename(epoch_api_dir)
      if route = routes.find { |prefix, data| data[:subdir] == route_subdir_name }
        route_path, route_data = route
        requests_dir = epoch_api_dir + '/request/'
        responses_dir = epoch_api_dir + '/response/'
        request_files = ::Dir[requests_dir + '**/*.yml'].sort.map { |path| path[requests_dir.length..-1] }
        response_files = ::Dir[responses_dir + '**/*.yml'].sort.map { |path| path[responses_dir.length..-1] }
        if request_files != response_files
          difference = ((request_files - response_files) | (response_files - request_files)).sort
          message = 'Mismatched request/response file pairs under ' +
                    "#{epoch_api_dir.inspect}: #{difference.inspect}"
          raise ::ArgumentError, message
        end

        # convert filename prefix to MD5 wherever necessary.
        request_files.each do |path|
          # load request/response pair to validate.
          request_file_path = ::File.join(requests_dir, path)
          response_file_path = ::File.join(responses_dir, path)
          request_data = ::Mash.new(::YAML.load_file(request_file_path))
          response_data = ::Mash.new(::YAML.load_file(response_file_path))

          # if confing contains unreachable (i.e. no available route) files
          # then that is ignorable.
          query_string = request_data[:query]
          uri = METADATA_CLASS.normalize_uri(
            URI::HTTP.build(
              host:  'none',
              path:  (route_path + ::File.dirname(path)),
              query: request_data[:query]).to_s)

          # compute checksum from recorded request metadata.
           = METADATA_CLASS.new(
            mode:       :validate,
            kind:       :request,
            logger:     logger,
            route_data: route_data,
            uri:        uri,
            verb:       request_data[:verb],
            headers:    request_data[:headers],
            body:       request_data[:body],
            variables:  {})

          # rename fixure file prefix only if given custom name by user.
          name = ::File.basename(path)

          if matched = FIXTURE_FILE_NAME_REGEX.match(name)
            # verify correct MD5 for body.
            file_checksum = matched[1]
            if file_checksum.casecmp(.checksum) != 0
              message = "Checksum from fixture file name (#{file_checksum}) " +
                        "does not match the request body checksum " +
                        "(#{.checksum}): #{request_file_path.inspect}"
              raise ::ArgumentError, message
            end
          else
            # compute checksum from loaded request body.
            checksum_file_name = .checksum + '.yml'

            # rename file pair.
            to_request_file_path = ::File.join(::File.dirname(request_file_path), checksum_file_name)
            to_response_file_path = ::File.join(::File.dirname(response_file_path), checksum_file_name)
            logger.debug("Renaming #{request_file_path.inspect} to #{to_request_file_path.inspect}.")
            ::File.rename(request_file_path, to_request_file_path)
            logger.debug("Renaming #{response_file_path.inspect} to #{to_response_file_path.inspect}.")
            ::File.rename(response_file_path, to_response_file_path)
          end
        end
      else
        # unknown route fixture directories will cause playback engine to
        # spin forever.
        unrecognized_routes << epoch_api_dir
        message = "Cannot find a route for fixtures directory: #{epoch_api_dir.inspect}"
        raise ::ArgumentError, message
      end
    end
  end
end

.respond_to?(methud) ⇒ Boolean

Returns:

  • (Boolean)

See Also:

  • Object#respond_to?


86
87
88
# File 'lib/right_develop/testing/servers/might_api/lib/config.rb', line 86

def self.respond_to?(methud)
  super(methud) || (@config && @config.respond_to?(methud))
end