Class: DandelionS1

Inherits:
RackRscript
  • Object
show all
Defined in:
lib/dandelion_s1.rb

Instance Method Summary collapse

Constructor Details

#initialize(h = {}) ⇒ DandelionS1

Returns a new instance of DandelionS1.



10
11
12
13
14
15
16
17
18
19
20
# File 'lib/dandelion_s1.rb', line 10

def initialize(h={})

  raw_opts = {root: 'www', access: {}, static: []}.merge h
  #@static = %w(index.html dynarex snippets)
  @static = raw_opts[:static]
  @root = raw_opts[:root]

  #@access_list = {'/do/r/hello3' => 'user'}
  @access_list = raw_opts[:access]
  super(raw_opts)
end

Instance Method Details

#call(e) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/dandelion_s1.rb', line 22

def call(e)

  private_user = @access_list[e['REQUEST_PATH']]

  if private_user.nil? or private_user == e['REMOTE_USER'] then
    super(e)
  else
    request = '/unauthorised/'
    content, content_type, status_code = run_route(request)        
    content_type ||= 'text/html'
    [status_code=401, {"Content-Type" => content_type}, [content]]
  end

end

#default_routes(env, params) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/dandelion_s1.rb', line 37

def default_routes(env, params)

  super(env, params)

  get /(\/(?:#{@static.join('|')}).*)/ do |path|

    filepath = File.join(@root, path)

    if path.length < 1 or path[-1] == '/' then
      path += 'index.html' 
      File.read File.join(base, path)
    elsif File.directory? filepath then
      Redirect.new (path + '/') 
    elsif File.exists? filepath then
      h = {xml: 'application/xml', html: 'text/html', png: 'image/png', 
           jpg: 'image/jpeg', txt: 'text/plain'}
      content_type = h[filepath[/\w+$/].to_sym]
      [File.read(filepath), content_type || 'text/plain']
    else
      'oops, file ' + filepath + ' not found'
    end

  end

  get /^\/$/ do

    file = File.join(@root, 'index.html')
    File.read file
  end

  get '/unauthorised/' do
    'unauthorised user'
  end
 
end