Method: Gin::Controller#etag

Defined in:
lib/gin/controller.rb

#etag(value, opts = {}) ⇒ Object

Set the ETag header. If the ETag was set in a previous request and matches the current one, halts the action and returns a 304 on GET and HEAD requests.



363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
# File 'lib/gin/controller.rb', line 363

def etag value, opts={}
  opts         = {:kind => opts} unless Hash === opts
  kind         = opts[:kind] || :strong
  new_resource = opts.fetch(:new_resource) { @request.post? }

  unless [:strong, :weak].include?(kind)
    raise ArgumentError, ":strong or :weak expected"
  end

  value = '"%s"' % value
  value = 'W/' + value if kind == :weak
  @response[ETAG] = value

  if (200..299).include?(status) || status == 304
    if etag_matches? @env[IF_NONE_MATCH], new_resource
      halt(@request.safe? ? 304 : 412)
    end

    if @env[IF_MATCH]
      halt 412 unless etag_matches? @env[IF_MATCH], new_resource
    end
  end
end