Class: Skypager::Protector

Inherits:
Object
  • Object
show all
Defined in:
lib/skypager/protector.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app, options = {}, &validator) ⇒ Protector

Skypager::Protector.new(options) do |request|

# validate request here

end

Valid options are:

- build_dir: defaults to ./build
- login_path: defaults to /login/index.html
- not_found_path: defaults to /not-found.html


19
20
21
22
23
24
# File 'lib/skypager/protector.rb', line 19

def initialize(app, options={}, &validator)
  @app = app
  @options = options
  @validator = validator
  @logger = options.fetch(:logger) { Logger.new(STDOUT) }
end

Instance Attribute Details

#appObject

Returns the value of attribute app.



9
10
11
# File 'lib/skypager/protector.rb', line 9

def app
  @app
end

#login_handlerObject

Returns the value of attribute login_handler.



9
10
11
# File 'lib/skypager/protector.rb', line 9

def 
  @login_handler
end

#optionsObject

Returns the value of attribute options.



9
10
11
# File 'lib/skypager/protector.rb', line 9

def options
  @options
end

#validatorObject

Returns the value of attribute validator.



9
10
11
# File 'lib/skypager/protector.rb', line 9

def validator
  @validator
end

Instance Method Details

#build_pathObject



71
72
73
74
# File 'lib/skypager/protector.rb', line 71

def build_path
  build_dir =  options.fetch(:build_dir) { Pathname(Dir.pwd).join('build') }
  Pathname(build_dir)
end

#call(env) ⇒ Object



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
60
61
# File 'lib/skypager/protector.rb', line 31

def call(env)
  request = Rack::Request.new(env)

  if request.post?
    log "#{  } #{ request.path } #{ .respond_to?(:call) }"
  end

  if request.post? && request.path ==  && .respond_to?(:call)

    result = .call(request)
    return  if result == false

    redirect_path = request.params['_redirect_to'] || "/"

    return redirect_to(redirect_path, result[:cookies] || {})
  end

  if !validate(request) && request.path != 
    log "== Validation failed: #{ request.path }"
    return 
  end

  path = normalize_path(request.path)
  file = build_path.join(path)

  if file.exist?
    serve_file(file)
  else
    @app.call(env)
  end
end


93
94
95
96
97
98
99
100
101
# File 'lib/skypager/protector.rb', line 93

def cookie_options
  options.fetch(:cookie_options) do
    {
      path: "/",
      expires: 180.minutes.from_now,
      domain: ".lvh.me"
    }
  end
end

#log(message) ⇒ Object



26
27
28
29
# File 'lib/skypager/protector.rb', line 26

def log message
  @logger && @logger.info(message)
  puts message if !@logger
end

#login_pathObject



111
112
113
# File 'lib/skypager/protector.rb', line 111

def 
  options.fetch(:login_path, "/login.html")
end

#normalize_path(path) ⇒ Object



115
116
117
118
119
120
121
122
123
124
# File 'lib/skypager/protector.rb', line 115

def normalize_path(path)
  if path == "/"
    path = "/index.html"
  elsif !path.to_s.match(/\.\w+/)
    path.gsub!(/\/$/,'')
    path + path_extension
  end

  path.to_s.gsub(/^\//,'')
end

#not_found_pathObject



107
108
109
# File 'lib/skypager/protector.rb', line 107

def not_found_path
   options.fetch(:not_found_path, "/not-found.html")
end

#path_extensionObject



126
127
128
129
130
# File 'lib/skypager/protector.rb', line 126

def path_extension
  options.fetch(:path_extension ) do
    options[:directory_indexes] ? "/index.html" : ".html"
  end
end

#redirect_to(path, cookies = {}) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/skypager/protector.rb', line 80

def redirect_to(path, cookies={})
  status, headers, body = [302, {"Content-Type" => "text/html", "Location" =>  path}, ["302 You've been redirected"]]
  response = Rack::Response.new(body,status,headers)

  unless cookies.empty?
    cookies.each do |k,v|
      response.set_cookie(k, cookie_options.merge(:value=>v))
    end
  end

  response.finish
end

#serve_file(file, status = 200) ⇒ Object



63
64
65
66
67
68
69
# File 'lib/skypager/protector.rb', line 63

def serve_file(file, status=200)
  mime_type = Rack::Mime.mime_type(Pathname(file).extname).to_s
  content   = Pathname(file).read.to_s
  content_length = Rack::Utils.bytesize(content).to_s

  [status, {"Content-Type" => mime_type,"Content-Length" => content_length}, [content]]
end

#serve_login_pathObject



103
104
105
# File 'lib/skypager/protector.rb', line 103

def 
  redirect_to()
end

#serve_not_found_pathObject



76
77
78
# File 'lib/skypager/protector.rb', line 76

def serve_not_found_path
  serve_file(not_found_path, 404)
end

#validate(request) ⇒ Object



132
133
134
135
136
137
138
139
140
141
142
# File 'lib/skypager/protector.rb', line 132

def validate(request)
  return true if validator.nil? || !validator.respond_to?(:call)

  return true if whitelist && request.path.match(whitelist)

  validator.call({
    cookies: request.cookies,
    params: request.params,
    env: request.env
  })
end

#whitelistObject



144
145
146
# File 'lib/skypager/protector.rb', line 144

def whitelist
  options.fetch(:whitelist) { /^\/(js|css|img|fonts|favico)/ }
end