class Rester::Utils::StubFile

Constants

DEFAULT_TAGS

Attributes

path[R]

Public Class Methods

new(path) click to toggle source
# File lib/rester/utils/stub_file.rb, line 8
def initialize(path)
  @path = path
  @_stub = StubFile.parse(path)
end

Private Class Methods

_parse_response_code(verb, tags) click to toggle source
# File lib/rester/utils/stub_file.rb, line 108
def _parse_response_code(verb, tags)
  if tags['successful'] == 'true'
    (verb == 'POST') ? 201 : 200
  else
    400
  end
end
_parse_tags(path, verb, context, resp_key) click to toggle source

Takes a response key (e.g., “response”) and parses out the tags (e.g., {“successful” => false})

# File lib/rester/utils/stub_file.rb, line 94
def _parse_tags(path, verb, context, resp_key)
  DEFAULT_TAGS.merge(Hash[resp_key.scan(/(\w+) *= *(\w+)/)]).tap { |tags|
    _validate_tags(path, verb, context, tags)
  }
end
_update_context(path, verb, context, spec) click to toggle source

Given a context hash, updates it for consumption by the rest of the StubAdapter (i.e., removes tags from “response” key and puts them in a “response_tags” key).

# File lib/rester/utils/stub_file.rb, line 50
def _update_context(path, verb, context, spec)
  _update_request(path, verb, context, spec)
  _update_response(path, verb, context, spec)
end
_update_request(path, verb, context, spec) click to toggle source

Converts all the values in the request hash to strings, which mimics how the data will be received on the service side.

# File lib/rester/utils/stub_file.rb, line 58
def _update_request(path, verb, context, spec)
  spec['request'] = Utils.stringify(spec['request'] || {})
end
_update_response(path, verb, context, spec) click to toggle source

Parses response tags (e.g., response).

Currently supported tags:

successful    must be 'true' or 'false'
# File lib/rester/utils/stub_file.rb, line 67
def _update_response(path, verb, context, spec)
  responses = spec.select { |k,_|
    k =~ /\Aresponse(\[(\w+) *= *(\w+)(, *(\w+) *= *(\w+))*\])?\z/
  }

  if responses.count == 0
    fail Errors::StubError, "#{verb.upcase} #{path} is missing a " \
      "response for the context #{context.inspect}"
  elsif responses.count > 1
    fail Errors::StubError, "#{verb.upcase} #{path} has too many " \
      "responses defined for the context #{context.inspect}"
  end

  response_key = responses.keys.first

  tags = _parse_tags(path, verb, context, response_key)

  spec.merge!(
    'response' => spec.delete(response_key),
    'response_tags' => tags,
    'response_code' => _parse_response_code(verb, tags)
  )
end
_validate_tags(path, verb, context, tags) click to toggle source
# File lib/rester/utils/stub_file.rb, line 100
def _validate_tags(path, verb, context, tags)
  unless ['true', 'false'].include?(tags['successful'])
    fail Errors::StubError, '"successful" tag should be either "true" '\
      'or "false" in' "#{verb.upcase} #{path} in context " \
      "#{context.inspect}"
  end
end
parse(path) click to toggle source

Parses the stub file and returns the data as a hash

# File lib/rester/utils/stub_file.rb, line 28
def parse(path)
  parse!(YAML.load_file(path))
end
parse!(stub_hash) click to toggle source

Given a raw stub file hash, converts it to the format used internally.

# File lib/rester/utils/stub_file.rb, line 34
def parse!(stub_hash)
  stub_hash.each do |path, verbs|
    next if ['version', 'consumer', 'producer'].include?(path)

    verbs.each do |verb, contexts|
      contexts.each do |context, spec|
        _update_context(path, verb, context, spec)
      end
    end
  end
end

Private Instance Methods

method_missing(meth, *args, &block) click to toggle source
Calls superclass method
# File lib/rester/utils/stub_file.rb, line 15
def method_missing(meth, *args, &block)
  if @_stub.respond_to?(meth)
    @_stub.public_send(meth, *args, &block)
  else
    super
  end
end