Module: Tap::Controller::Utils

Defined in:
lib/tap/controller/utils.rb

Instance Method Summary collapse

Instance Method Details

#download(path) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
# File 'lib/tap/controller/utils.rb', line 16

def download(path)
  content = File.read(path)
  headers = {
    "Last-Modified" => File.mtime(path).httpdate,
    "Content-Type" => Rack::Mime.mime_type(File.extname(path), 'text/plain'), 
    "Content-Disposition" => "attachment; filename=#{File.basename(path)};",
    "Content-Length" => content.size.to_s
  }
    
  [200, headers, [content]]
end

#static_file(path) ⇒ Object



5
6
7
8
9
10
11
12
13
14
# File 'lib/tap/controller/utils.rb', line 5

def static_file(path)
  content = File.read(path)
  headers = {
    "Last-Modified" => File.mtime(path).httpdate,
    "Content-Type" => Rack::Mime.mime_type(File.extname(path), 'text/plain'), 
    "Content-Length" => content.size.to_s
  }
    
  [200, headers, [content]]
end

#yamlize(obj, indent = "") ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/tap/controller/utils.rb', line 28

def yamlize(obj, indent="")
  case obj
  when Hash
    lines = []
    obj.each_pair do |key, value|
      lines << case value
      when Hash, Array
        "#{indent}#{key}:\n#{yamlize(value, indent + '  ')}"
      when '#'
        next
      else
        "#{indent}#{key}: #{value}"
      end
    end
    lines.empty? ? "#{indent}{}" : lines.join("\n")
  when Array
    lines = []
    obj.each do |value|
      lines << case value
      when Hash, Array
        "#{indent}-\n#{yamlize(value, indent + '  ')}"
      when '#'
        next
      else
        "#{indent}- #{value}"
      end
    end
    lines.empty? ? "#{indent}[]" : lines.join("\n")
  else
    obj
  end
end