Module: Rack::Hard::Util

Included in:
Copy, Load, Save
Defined in:
lib/rack/hard/util.rb

Instance Method Summary collapse

Instance Method Details

#expired?(timeout, path) ⇒ Boolean

Returns:

  • (Boolean)


33
34
35
36
37
38
# File 'lib/rack/hard/util.rb', line 33

def expired? timeout, path
  return false if timeout === false
  return true  if timeout === true
  return true  unless ::File.exists?(path)
  (::File.mtime(path)+timeout) < Time.now
end

#generate_path_from(store, path_info) ⇒ Object



20
21
22
23
# File 'lib/rack/hard/util.rb', line 20

def generate_path_from store, path_info
  path = ::File.join(store, path_info)
  return ( MIME::Types.type_for("try."+path.split("/").last.split(".").last).empty? ? path.gsub(/\/$/, '')+"/index.html" : path )
end

#http_headers(env) ⇒ Object



25
26
27
28
29
30
31
# File 'lib/rack/hard/util.rb', line 25

def http_headers env
  http_headers = {}
  env.select { |k,v| k.start_with?("HTTP_") }.each do |k,v|
    http_headers[k.gsub(/^HTTP_/, '')] = v
  end
  http_headers
end

#ignored?(ignores, path) ⇒ Boolean

Returns:

  • (Boolean)


15
16
17
18
# File 'lib/rack/hard/util.rb', line 15

def ignored?(ignores, path)
  ignores.each { |ignore| return true if path.end_with?(ignore) } unless ignores.empty?
  return false
end

#make_dir(path) ⇒ Object



40
41
42
# File 'lib/rack/hard/util.rb', line 40

def make_dir path
  FileUtils.mkdir_p(path) unless ::File.directory?(path)
end

#setup_variables(opts = {}) ⇒ Object



7
8
9
10
11
12
13
# File 'lib/rack/hard/util.rb', line 7

def setup_variables(opts={})
  @store   ||= opts[:store]   || "./static"
  @ignores ||= opts[:ignores] || []
  @headers ||= opts[:headers] || false
  @timeout ||= opts[:timeout] || false
  make_dir(@store)
end