class Rack::Raml::Response

Attributes

raml[R]
request[R]

Public Class Methods

new(raml_file, request) click to toggle source
# File lib/rack/raml/response.rb, line 9
def initialize(raml_file, request)
  @raml = ::Raml.parse_file(raml_file)
  @request = request
end

Public Instance Methods

matches?() click to toggle source
# File lib/rack/raml/response.rb, line 14
def matches?
  !response.nil?
end
to_rack() click to toggle source
# File lib/rack/raml/response.rb, line 18
def to_rack
  if matches?
    [code, { 'Content-Type' => type }, [body]]
  else
    [404, { 'Content-Type' => type }, [not_found]]
  end
end

Private Instance Methods

body() click to toggle source
# File lib/rack/raml/response.rb, line 48
def body
  response.example if matches?
end
code() click to toggle source
# File lib/rack/raml/response.rb, line 40
def code
  if value = request.params['_code']
    value.to_i
  elsif possible_responses
    possible_responses.keys.min
  end
end
find_possible_responses() click to toggle source
# File lib/rack/raml/response.rb, line 83
def find_possible_responses
  resource = resources.find do |res|
    pattern = uri_pattern(res.resource_path)
    res.methods[verb] && pattern.match(path)
  end

  resource.methods[verb].responses if resource
end
not_found() click to toggle source
# File lib/rack/raml/response.rb, line 52
def not_found
  {
    error: 'RAML not found',
    code: code,
    type: type,
    path: path,
    verb: verb
  }.to_json
end
path() click to toggle source
# File lib/rack/raml/response.rb, line 28
def path
  request.env['PATH_INFO']
end
possible_responses() click to toggle source
# File lib/rack/raml/response.rb, line 79
def possible_responses
  @possible_responses ||= find_possible_responses
end
resources(node = raml) click to toggle source
# File lib/rack/raml/response.rb, line 62
def resources(node = raml)
  return [] unless node.respond_to?(:children)

  node.children.inject([]) do |acc, child|
    acc << child if child.kind_of?(::Raml::Resource)
    acc + resources(child)
  end
end
response() click to toggle source
# File lib/rack/raml/response.rb, line 71
def response
  @response ||= (
    possible_responses &&
    possible_responses[code] &&
    possible_responses[code].bodies[type]
  )
end
type() click to toggle source
# File lib/rack/raml/response.rb, line 36
def type
  request.params['_type'] || request.content_type || 'application/json'
end
uri_pattern(template) click to toggle source
# File lib/rack/raml/response.rb, line 92
def uri_pattern(template)
  URITemplate::RFC6570.new("{/prefix*}#{template}")
end
verb() click to toggle source
# File lib/rack/raml/response.rb, line 32
def verb
  request.request_method.downcase
end