summaryrefslogtreecommitdiff
path: root/app/controllers/controller_extension/json_file.rb
blob: df9cf55222497fd9d866e136b767033a3fa407d0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
module ControllerExtension::JsonFile
  extend ActiveSupport::Concern
  include ControllerExtension::Errors

  protected

  def send_file(filename)
    file = fetch_file(filename)
    if file.present?
      send_file_or_cache_hit(file)
    else
      not_found
    end
  end

  def send_file_or_cache_hit(file)
    if stale?(:last_modified => file.mtime)
      response.content_type = 'application/json'
      render :text => file.read
    end
  end

  def fetch_file(filename)
    File.new(filename) if File.exist?(filename)
  end

end