class ResponseMate::Manifest

Responsible for parsing the requests manifest file to actually operate on the requests

Attributes

description[R]
environment[RW]
filename[RW]
name[R]
requests[RW]
requests_text[RW]

Public Class Methods

new(filename, environment = nil) click to toggle source
# File lib/response_mate/manifest.rb, line 7
def initialize(filename, environment = nil)
  @filename = filename || ResponseMate.configuration.requests_manifest
  @environment = environment
  parse
end

Public Instance Methods

requests_for_keys(keys) click to toggle source

Filters requests based on the supplied Array of keys @param [Array] keys The keys to lookup for matching requests @return [Array] of matching requests

# File lib/response_mate/manifest.rb, line 16
def requests_for_keys(keys)
  return [] if keys.blank?

  existing_keys = requests.map(&:key)
  missing_keys = keys - existing_keys

  if missing_keys.present?
    fail ResponseMate::KeysNotFound.new(missing_keys.join(', '))
  end

  requests.select! do |r|
    keys.include? r.key
  end

  requests
end

Private Instance Methods

check_environment() click to toggle source
# File lib/response_mate/manifest.rb, line 73
  def check_environment
    return if environment.exists?

    warning = 'The specified requests file is a template, but no environment file is found.'
    guide = <<-GUIDE
    The environment file holds any variables you wish to be interpolated.
    You may specify a different environment file using the -e [file.yml] option.
    Default: environment.yml
    GUIDE

    STDERR.puts warning.yellow, guide
  end
check_requests(parsed_manifest) click to toggle source
# File lib/response_mate/manifest.rb, line 86
  def check_requests(parsed_manifest)
    return if parsed_manifest.key? :requests

    gemspec = Gem::Specification.find_by_name('response_mate')
    example_manifest = "#{gemspec.homepage}/blob/master/requests.yml.sample"

    warning = 'The specified requests file contains no requests.'
    guide = <<-GUIDE
    To declare requests place them under the :requests key.
    Example manifest: #{example_manifest}
    GUIDE

    STDERR.puts warning.yellow, guide
  end
mustache?() click to toggle source

Check if the manifest file is a Mustache template

# File lib/response_mate/manifest.rb, line 62
def mustache?
  requests_text =~ /{{.*}}/
end
parse() click to toggle source

Parse the requests manifest @return [Array] of requests

# File lib/response_mate/manifest.rb, line 37
def parse
  preprocess_manifest
  parsed_manifest = YAML.load(@requests_text).deep_symbolize_keys
  @name = parsed_manifest.fetch(:name, filename)
  @description = parsed_manifest.fetch(:description, '')
  check_requests(parsed_manifest)
  @requests = parsed_manifest.
    fetch(:requests, []). # rubocop:disable Style/MultilineOperationIndentation
    map(&:deep_symbolize_keys!). # rubocop:disable Style/MultilineOperationIndentation
    map { |rh| ResponseMate::Request.new(rh).normalize! } # rubocop:disable Style/MultilineOperationIndentation
end
preprocess_manifest() click to toggle source

Parse the manifest file as a template @return [String] The manifest text parsed as a template

# File lib/response_mate/manifest.rb, line 51
def preprocess_manifest
  begin
    @requests_text = File.read filename
  rescue Errno::ENOENT
    raise ResponseMate::ManifestMissing.new(filename)
  end

  process_mustache! if environment.present? && mustache?
end
process_mustache!() click to toggle source

Evaluate Mustache template

# File lib/response_mate/manifest.rb, line 67
def process_mustache!
  check_environment

  @requests_text = Mustache.render(requests_text, environment.env)
end