class Sitespec::Artifact
“Artifact” is corresponding to each response body to an endpoint. This class has ActiveRecord-like interface like Sitespec::Artifact.create
.
@example
Sitespec::Artifact.create(example: example, example_group: example_group)
Public Class Methods
Short-hand method to call `.new` and `#save` @return [Sitespec::Artifact]
# File lib/sitespec/artifact.rb, line 15 def create(**args) new(**args).tap(&:save) end
@param [RSpec::Core::Example] example In almost cases, `RSpec.current_example` is passed @param [RSpec::Core::ExampleGroup] example_group
In almost cases, `self` is passed
# File lib/sitespec/artifact.rb, line 22 def initialize(example: nil, example_group: nil) @example = example @example_group = example_group end
Public Instance Methods
Validate its attributes and generate a new file @return [false, true] True for successful case, otherwise false
# File lib/sitespec/artifact.rb, line 29 def save if valid? write increment true else false end end
Private Instance Methods
Utility method to access to `@example`
# File lib/sitespec/artifact.rb, line 42 def example @example end
Utility method to access to `@example_group`
# File lib/sitespec/artifact.rb, line 47 def example_group @example_group end
# File lib/sitespec/artifact.rb, line 51 def generate_file pathname.write(response.body) end
# File lib/sitespec/artifact.rb, line 55 def increment Sitespec.increment_artifacts_count end
# File lib/sitespec/artifact.rb, line 59 def make_parent_directory pathname.parent.mkpath end
@return [String] Where to an artifact file is located
# File lib/sitespec/artifact.rb, line 64 def path File.join(Sitespec.configuration.build_pathname, request.path_info) + path_suffix end
@return [String]
# File lib/sitespec/artifact.rb, line 69 def path_suffix case when !Sitespec.configuration.auto_complete_html_path "" when response.content_type.nil? || !response.content_type.include?("text/html") || request.path_info.end_with?(".html") "" when request.path_info.end_with?("/") "index.html" else ".html" end end
@return [Pathname]
# File lib/sitespec/artifact.rb, line 83 def pathname Pathname.new(path) end
@note We expect `request` is provided via rack-test
# File lib/sitespec/artifact.rb, line 88 def request example_group.last_request end
@note We expect `response` is provided via rack-test
# File lib/sitespec/artifact.rb, line 93 def response example_group.last_response end
@return [false, true] Validation result
# File lib/sitespec/artifact.rb, line 98 def valid? example.exception.nil? && Sitespec.configuration.enabled? end
Generate directory and file
# File lib/sitespec/artifact.rb, line 103 def write make_parent_directory generate_file end