Class: Soaspec::Baseline
- Inherits:
-
Object
- Object
- Soaspec::Baseline
- Includes:
- ExeHelpers
- Defined in:
- lib/soaspec/baseline.rb
Overview
Used for defining parameters for recording and asserting against a baseline. This does not take into account different payloads in the request, only for where a response varies by suburls or query parameters
Constant Summary collapse
- ALLOWED_FORMATS =
Returns List of allowed formats.
%i[raw hash].freeze
Class Attribute Summary collapse
-
.folder ⇒ String
Folder where baselines are recorded and retrieved.
Instance Attribute Summary collapse
-
#description ⇒ String
Name of file including folders describing baseline.
-
#exchange ⇒ Exchange
Exchange object to save/assert baseline for.
-
#format ⇒ Symbol
Format in which baseline is stored.
Instance Method Summary collapse
-
#actual_content ⇒ String, Hash
Actual response from API.
-
#content_to_save ⇒ Object
Content to save as a baseline.
-
#file ⇒ String
File where baseline is stored.
-
#initialize(exchange, format) ⇒ Baseline
constructor
A new instance of Baseline.
-
#matches? ⇒ Boolean
Compare baseline with expected result.
-
#read_baseline ⇒ String
Result of reading baseline.
Methods included from ExeHelpers
#class_content, #create_file, #create_files, #create_files_for, #create_folder, #generated_soap_spec_for, #retrieve_contents, #spec_task
Constructor Details
#initialize(exchange, format) ⇒ Baseline
Returns a new instance of Baseline.
26 27 28 29 30 31 32 33 |
# File 'lib/soaspec/baseline.rb', line 26 def initialize(exchange, format) self.exchange = exchange self.format = format return if ALLOWED_FORMATS.include? format raise ArgumentError, "Expected format #{format} to be " \ "either #{ALLOWED_FORMATS}" end |
Class Attribute Details
.folder ⇒ String
Returns Folder where baselines are recorded and retrieved.
84 85 86 |
# File 'lib/soaspec/baseline.rb', line 84 def folder @folder end |
Instance Attribute Details
#description ⇒ String
Returns Name of file including folders describing baseline.
78 79 80 |
# File 'lib/soaspec/baseline.rb', line 78 def description @description || exchange.request_parameters.description end |
#exchange ⇒ Exchange
Returns Exchange object to save/assert baseline for.
18 19 20 |
# File 'lib/soaspec/baseline.rb', line 18 def exchange @exchange end |
#format ⇒ Symbol
Returns Format in which baseline is stored. Either :raw or :hash.
20 21 22 |
# File 'lib/soaspec/baseline.rb', line 20 def format @format end |
Instance Method Details
#actual_content ⇒ String, Hash
Returns Actual response from API.
55 56 57 58 59 60 61 62 63 |
# File 'lib/soaspec/baseline.rb', line 55 def actual_content if format == :hash exchange.to_hash elsif format == :raw exchange.pretty_response_body.strip else raise NotImplementedError, "Format #{format} is not #{ALLOWED_FORMATS}" end end |
#content_to_save ⇒ Object
Content to save as a baseline
50 51 52 |
# File 'lib/soaspec/baseline.rb', line 50 def content_to_save format == :raw ? exchange.pretty_response_body : YAML.dump(exchange.to_hash) end |
#file ⇒ String
Returns File where baseline is stored.
71 72 73 74 75 |
# File 'lib/soaspec/baseline.rb', line 71 def file File.join(self.class.folder, exchange.exchange_handler.to_s.snakecase, "#{description}.#{format == :raw ? exchange.format : 'yml'}") end |
#matches? ⇒ Boolean
Compare baseline with expected result. This will create baseline if not created
38 39 40 41 42 43 44 45 46 47 |
# File 'lib/soaspec/baseline.rb', line 38 def matches? if File.exist?(file) actual_content == read_baseline else create_file filename: file, content: content_to_save raise Soaspec::BaselineError, "Created baseline at #{file}. Inspect file to ensure it is correct and rerun to ensure baseline is stable" end end |
#read_baseline ⇒ String
Returns Result of reading baseline.
66 67 68 |
# File 'lib/soaspec/baseline.rb', line 66 def read_baseline format == :raw ? File.read(file).strip : YAML.load_file(file) end |