class Grape::Batch::Validator

Parse and validate request params and ensure it is a valid batch request

Constants

ALLOWED_METHODS

Public Class Methods

parse(env, limit) click to toggle source
# File lib/grape/batch/validator.rb, line 8
def parse(env, limit)
  batch_body = decode_body(env['rack.input'].read)

  requests = batch_body['requests']
  validate_batch(requests, limit)
  requests.each { |request| validate_request(request) }

  requests
end

Private Class Methods

decode_body(body) click to toggle source
# File lib/grape/batch/validator.rb, line 20
def decode_body(body)
  fail RequestBodyError::Blank unless body.length > 0

  begin
    batch_body = MultiJson.decode(body)
  rescue MultiJson::ParseError
    raise RequestBodyError::JsonFormat
  end

  fail RequestBodyError::Nil unless batch_body
  fail RequestBodyError::Format unless batch_body.is_a?(Hash)

  batch_body
end
validate_batch(batch_requests, limit) click to toggle source
# File lib/grape/batch/validator.rb, line 35
def validate_batch(batch_requests, limit)
  fail RequestBodyError::MissingRequests unless batch_requests
  fail RequestBodyError::RequestFormat unless batch_requests.is_a?(Array)
  fail TooManyRequestsError if batch_requests.count > limit
end
validate_request(request) click to toggle source
# File lib/grape/batch/validator.rb, line 41
def validate_request(request)
  fail RequestBodyError::MissingMethod unless request['method']
  fail RequestBodyError::MethodFormat unless request['method'].is_a?(String)
  fail RequestBodyError::InvalidMethod unless ALLOWED_METHODS.include?(request['method'])
  fail RequestBodyError::MissingPath unless request['path']
  fail RequestBodyError::InvalidPath unless request['path'].is_a?(String)
end