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

create(**args) click to toggle source

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
new(example: nil, example_group: nil) click to toggle source

@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

save() click to toggle source

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

example() click to toggle source

Utility method to access to `@example`

# File lib/sitespec/artifact.rb, line 42
def example
  @example
end
example_group() click to toggle source

Utility method to access to `@example_group`

# File lib/sitespec/artifact.rb, line 47
def example_group
  @example_group
end
generate_file() click to toggle source
# File lib/sitespec/artifact.rb, line 51
def generate_file
  pathname.write(response.body)
end
increment() click to toggle source
# File lib/sitespec/artifact.rb, line 55
def increment
  Sitespec.increment_artifacts_count
end
make_parent_directory() click to toggle source
# File lib/sitespec/artifact.rb, line 59
def make_parent_directory
  pathname.parent.mkpath
end
path() click to toggle source

@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
path_suffix() click to toggle source

@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
pathname() click to toggle source

@return [Pathname]

# File lib/sitespec/artifact.rb, line 83
def pathname
  Pathname.new(path)
end
request() click to toggle source

@note We expect `request` is provided via rack-test

# File lib/sitespec/artifact.rb, line 88
def request
  example_group.last_request
end
response() click to toggle source

@note We expect `response` is provided via rack-test

# File lib/sitespec/artifact.rb, line 93
def response
  example_group.last_response
end
valid?() click to toggle source

@return [false, true] Validation result

# File lib/sitespec/artifact.rb, line 98
def valid?
  example.exception.nil? && Sitespec.configuration.enabled?
end
write() click to toggle source

Generate directory and file

# File lib/sitespec/artifact.rb, line 103
def write
  make_parent_directory
  generate_file
end