helmet 0.1.3 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: a879ae7efe16ee0b0697501f4075fea37abe07b1
4
+ data.tar.gz: 448ee6bb790963c0cd5b5b2faabc7ad1fa585b19
5
+ SHA512:
6
+ metadata.gz: 15ec47ac0967d6492915529c865507451bb62e1236ec10fdf7c7c5a89af1d0e351bf0e75bfe190a4ebda7409de922a19a7b9961a3c2b536adb6dec979774c9b4
7
+ data.tar.gz: 495a7c1a4006bc21d140f226744983b68d09ea7c021839521c16a20b17714870a50047b275840d93eaaf99427a9172571ff96b142a6abd1828261988f1538d14
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
4
+
@@ -0,0 +1,7 @@
1
+ # HISTORY
2
+
3
+ ## v0.0.2
4
+
5
+ - Replace router by http_router gem.
6
+ - Include LICENSE file
7
+
data/LICENSE ADDED
@@ -0,0 +1,18 @@
1
+ Copyright (c) 2012 Thiago Lewin.
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ of this software and associated documentation files (the "Software"), to
5
+ deal in the Software without restriction, including without limitation the
6
+ rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
7
+ sell copies of the Software, and to permit persons to whom the Software is
8
+ furnished to do so, subject to the following conditions:
9
+
10
+ The above copyright notice and this permission notice shall be included in
11
+ all copies or substantial portions of the Software.
12
+
13
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
16
+ THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
17
+ IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
18
+ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,46 @@
1
+ Helmet
2
+ ======
3
+
4
+ Simple web framework for Goliath web server.
5
+
6
+ Helmet is **very** inspired on Sinatra framework (https://github.com/bmizerany/sinatra) without losing the Goliath API design.
7
+ The framework tries some functionalities that Goliath doesn't provide out of the box, like:
8
+
9
+ - Session management
10
+ - Simplified template support
11
+ - Simple notation for HTTP verbs and filters
12
+
13
+ This is the very first version, all the APIand code will be reviewed.
14
+
15
+ Example
16
+ =======
17
+
18
+ ```ruby
19
+ require 'helmet'
20
+
21
+ class Misc < Helmet::API
22
+
23
+ use Rack::Static,
24
+ :root => public_folder, # Default: ./public
25
+ :urls => ['/css']
26
+
27
+ get '/' do
28
+ erb(:index, {:layout => :layout}, {:data => session[:data]})
29
+ end
30
+
31
+ post '/session' do |env|
32
+ erb(:index, {:layout => :layout}, {:data => session[:data]})
33
+ end
34
+
35
+ end
36
+ ```
37
+ See examples directory
38
+
39
+ License
40
+ =======
41
+ Goliath License, (https://github.com/postrank-labs/goliath/blob/master/LICENSE)
42
+
43
+ Credits
44
+ =======
45
+
46
+ Helmet is copyright Thiago Lewin <thiago_lewin@yahoo.com.br>
@@ -0,0 +1,10 @@
1
+ require 'rake'
2
+ require 'rake/testtask'
3
+
4
+ task :default => [:test]
5
+
6
+ desc 'run tests'
7
+ Rake::TestTask.new(:test) do |test|
8
+ test.pattern = 'test/**/*_test.rb'
9
+ test.verbose = true
10
+ end
@@ -0,0 +1,10 @@
1
+ $LOAD_PATH.unshift File.join(File.dirname(__FILE__), '..', 'lib')
2
+ require 'helmet'
3
+
4
+ class HelloWorld < Helmet::API
5
+
6
+ get '/' do
7
+ 'hello world'
8
+ end
9
+
10
+ end
@@ -0,0 +1,17 @@
1
+ $LOAD_PATH.unshift File.join(File.dirname(__FILE__), '..', 'lib')
2
+ require 'helmet'
3
+
4
+ class Helper < Helmet::API
5
+
6
+ helpers do
7
+ def xs(num)
8
+ 'x' * num
9
+ end
10
+ end
11
+
12
+ get '/' do
13
+ num = (params['x'] || 0).to_i
14
+ xs(num)
15
+ end
16
+
17
+ end
@@ -0,0 +1,23 @@
1
+ $LOAD_PATH.unshift File.join(File.dirname(__FILE__), '..', 'lib')
2
+ require 'helmet'
3
+
4
+ class Misc < Helmet::API
5
+
6
+ use Rack::Static,
7
+ :root => config(:public_folder),
8
+ :urls => ['/css']
9
+
10
+ get '/' do
11
+ erb(:index,
12
+ {:layout => :layout},
13
+ {:data => session[:data]})
14
+ end
15
+
16
+ post '/session' do
17
+ session[:data] = env['params']['session']
18
+ erb(:index,
19
+ {:layout => :layout},
20
+ {:data => session[:data]})
21
+ end
22
+
23
+ end
@@ -0,0 +1,15 @@
1
+ html,
2
+ body {
3
+ margin: 0px;
4
+ padding: 0px;
5
+ font-family: 'Arial', 'Sans Serif', 'Courier New', 'Times New Roman';
6
+ }
7
+
8
+ .container {
9
+ width: 940px;
10
+ margin: 0px auto;
11
+ }
12
+
13
+ h1, h2, h3, h4, h5 {
14
+ margin: 0px 0px 20px;
15
+ }
@@ -0,0 +1,17 @@
1
+ $LOAD_PATH.unshift File.join(File.dirname(__FILE__), '..', 'lib')
2
+ require 'helmet/api'
3
+ require 'goliath/runner'
4
+
5
+ class Test < Helmet::API
6
+
7
+ get '/' do
8
+ 'test'
9
+ end
10
+
11
+ end
12
+
13
+ runner = Goliath::Runner.new(ARGV, nil)
14
+ runner.api = Test.new
15
+ runner.app = Goliath::Rack::Builder.build(Test, runner.api)
16
+ runner.run
17
+
@@ -0,0 +1,7 @@
1
+ <h1>Helmet for Goliath</h1>
2
+ <h2>Current Session Data: <%= data %></h2>
3
+ <form name="session" action="/session" method="post">
4
+ <label for="session">Session data:</label>
5
+ <input type="text" name="session">
6
+ <input type="submit" value="send">
7
+ </form>
@@ -0,0 +1,13 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <meta http-equiv="content-type" content="text/html; charset=UTF-8">
5
+ <link rel="stylesheet" href="/css/helmet.css" type="text/css" media="screen">
6
+ <title>Helmet - Test</title>
7
+ </head>
8
+ <body>
9
+ <div class="container">
10
+ <%= yield %>
11
+ </div>
12
+ </body>
13
+ </html>
@@ -0,0 +1,24 @@
1
+ require './lib/helmet/version'
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = 'helmet'
5
+ s.version = Helmet::VERSION
6
+ s.platform = Gem::Platform::RUBY
7
+ s.date = Time.now
8
+ s.summary = 'Simple web framework for Goliath web server.'
9
+ s.description = s.summary
10
+ s.authors = ['Thiago Lewin']
11
+ s.email = 'thiago_lewin@yahoo.com.br'
12
+ s.require_path = 'lib'
13
+ s.files = Dir['{lib/**/*,examples/**/*,test/**/*}', 'README.md', 'Rakefile', 'HISTORY.md', 'Gemfile', 'helmet.gemspec', 'LICENSE']
14
+ s.test_files = Dir['test/**/*']
15
+ s.homepage = 'https://github.com/tlewin/helmet'
16
+ s.license = 'MIT'
17
+
18
+ s.add_dependency 'goliath'
19
+ s.add_dependency 'http_router'
20
+ s.add_dependency 'tilt'
21
+
22
+ s.add_development_dependency 'yard'
23
+ s.add_development_dependency 'shoulda'
24
+ end
@@ -1,6 +1,7 @@
1
1
  require 'goliath'
2
2
 
3
3
  require 'helmet/api'
4
+ require 'helmet/version'
4
5
 
5
6
  module Helmet
6
7
  end
@@ -1,7 +1,11 @@
1
1
  require 'goliath/api'
2
+ require 'goliath/constants'
3
+ require 'http_router'
2
4
 
3
5
  require 'helmet/handler'
4
6
 
7
+ require 'pry'
8
+
5
9
  module Helmet
6
10
  class API < Goliath::API
7
11
 
@@ -15,8 +19,8 @@ module Helmet
15
19
  @config[key.to_sym]
16
20
  end
17
21
 
18
- def before(route, &block)
19
- @before_filters << [route, block]
22
+ def before(route, opts = {}, &block)
23
+ @before_filters.add(route, opts, &block)
20
24
  end
21
25
 
22
26
  def routes
@@ -36,24 +40,36 @@ module Helmet
36
40
  @helpers
37
41
  end
38
42
 
39
- def get(route, &block)
40
- register_route('GET', route, &block);
41
- register_route('HEAD', route, &block);
42
- end
43
- def post(route, &block) register_route('POST', route, &block); end
44
- def put(route, &block) register_route('PUT', route, &block); end
45
- def delete(route, &block) register_route('DELETE', route, &block); end
46
- def head(route, &block) register_route('HEAD', route, &block); end
47
-
48
- def register_route(method, route, &block)
49
- sig = API.signature(method, route)
50
- @routes[sig] = block
43
+ def get(route, opts = {}, &block)
44
+ register_route(:GET, route, opts, &block);
45
+ # HttpRouter already include head
46
+ # register_route('HEAD', route, opts, &block);
51
47
  end
52
48
 
53
- def signature(method, route)
54
- "#{method}#{route}"
55
- end
49
+ def connect(route, opts = {}, &block) register_route(:CONNECT, route, opts, &block); end
50
+ def delete(route, opts = {}, &block) register_route(:DELETE, route, opts, &block); end
51
+ # def head(route, opts = {}, &block) register_route(:HEAD, route, opts, &block); end
52
+ # def options(route, opts = {}, &block) register_route(:OPTIONS, route, opts, &block); end
53
+ def post(route, opts = {}, &block) register_route(:POST, route, opts, &block); end
54
+ def put(route, opts = {}, &block) register_route(:PUT, route, opts, &block); end
56
55
 
56
+ def register_route(method, route, opts = {}, &block)
57
+ case method
58
+ when :GET
59
+ @routes.get(route, opts, &block)
60
+ when :CONNECT
61
+ @routes.connect(route, opts, &block)
62
+ when :DELETE
63
+ @routes.delete(route, opts, &block)
64
+ when :OPTIONS
65
+ @routes.options(route, opts, &block)
66
+ when :POST
67
+ @routes.post(route, opts, &block)
68
+ when :PUT
69
+ @routes.put(route, opts, &block)
70
+ end
71
+ end
72
+
57
73
  def inherited(klass)
58
74
  klass.init
59
75
 
@@ -70,10 +86,10 @@ module Helmet
70
86
 
71
87
  def init
72
88
  # Handle application routes
73
- @routes = {}
89
+ @routes = HttpRouter.new
74
90
 
75
91
  # Handle before filters
76
- @before_filters = []
92
+ @before_filters = HttpRouter.new
77
93
 
78
94
  @config = {}
79
95
 
@@ -83,17 +99,12 @@ module Helmet
83
99
  private
84
100
 
85
101
  def setup_middlewares(klass)
86
- # support for session
87
- klass.use Rack::Session::Cookie
88
102
  # support for forms
89
103
  klass.use Goliath::Rack::Params
90
104
  end
91
105
  end
92
106
 
93
107
  def response(env)
94
- # request path
95
- path = env['REQUEST_PATH']
96
-
97
108
  # request handler
98
109
  handler = Handler.new(env, self.class)
99
110
 
@@ -101,20 +112,16 @@ module Helmet
101
112
  handler.extend(self.class.get_helpers)
102
113
 
103
114
  catch(:halt) do
104
- # evaluate filters
105
- self.class.before_filters.each do |route|
106
- case route.first
107
- when String
108
- handler.handle! &route[1] if route.first == path
109
- when Regexp
110
- handler.handle! &route[1] if route.first =~ path
111
- end
115
+ # evaluate any route match
116
+ filters = self.class.before_filters.recognize(env).first || []
117
+ filters.each do |f|
118
+ handler.handle! &f.route.dest
112
119
  end
113
-
114
- sig = API.signature(env['REQUEST_METHOD'], path)
115
- block = self.class.routes[sig]
116
- if block
117
- handler.handle!(&block)
120
+
121
+ routes_recognized = self.class.routes.recognize(env).first
122
+ if routes_recognized
123
+ # Use the first matched route
124
+ handler.handle! &routes_recognized.first.route.dest
118
125
  else
119
126
  handler.handle! do
120
127
  status 404
@@ -122,6 +129,7 @@ module Helmet
122
129
  end
123
130
  end
124
131
  end
132
+
125
133
  handler.response.format_response
126
134
  end
127
135
  end
@@ -0,0 +1,3 @@
1
+ module Helmet
2
+ VERSION = '0.2.0'
3
+ end
@@ -3,8 +3,10 @@ require File.join(File.dirname(__FILE__), 'test_helper')
3
3
  require 'uri'
4
4
 
5
5
  class Simple < Helmet::API
6
+
7
+ use Rack::Session::Cookie, :secret => 'remove_security_warning'
6
8
 
7
- before /\/x\w*$/ do
9
+ before '/xx' do
8
10
  halt 'filtered!'
9
11
  end
10
12
 
@@ -23,6 +25,10 @@ class Simple < Helmet::API
23
25
  delete '/' do
24
26
  'delete'
25
27
  end
28
+
29
+ get '/raise' do
30
+ raise 'Error'
31
+ end
26
32
 
27
33
  get '/redirect' do
28
34
  redirect '/redirected'
@@ -86,5 +92,13 @@ class APITest < Test::Unit::TestCase
86
92
  end
87
93
  end
88
94
  end
95
+
96
+ # def test_raise
97
+ # with_api(Simple) do
98
+ # get_request({:path => '/raise'}, @err) do |c|
99
+ # assert_equal 'get', c.response
100
+ # end
101
+ # end
102
+ # end
89
103
 
90
104
  end
metadata CHANGED
@@ -1,85 +1,143 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: helmet
3
- version: !ruby/object:Gem::Version
4
- prerelease:
5
- version: 0.1.3
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.0
6
5
  platform: ruby
7
- authors:
6
+ authors:
8
7
  - Thiago Lewin
9
8
  autorequire:
10
9
  bindir: bin
11
10
  cert_chain: []
12
-
13
- date: 2012-10-26 00:00:00 Z
14
- dependencies:
15
- - !ruby/object:Gem::Dependency
11
+ date: 2013-09-20 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
16
14
  name: goliath
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '>='
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
17
21
  prerelease: false
18
- requirement: &id001 !ruby/object:Gem::Requirement
19
- none: false
20
- requirements:
21
- - - ">="
22
- - !ruby/object:Gem::Version
23
- version: "0"
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '>='
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: http_router
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
24
34
  type: :runtime
25
- version_requirements: *id001
26
- - !ruby/object:Gem::Dependency
27
- name: tilt
28
35
  prerelease: false
29
- requirement: &id002 !ruby/object:Gem::Requirement
30
- none: false
31
- requirements:
32
- - - ">="
33
- - !ruby/object:Gem::Version
34
- version: "0"
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: tilt
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
35
48
  type: :runtime
36
- version_requirements: *id002
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: yard
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: shoulda
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - '>='
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - '>='
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
37
83
  description: Simple web framework for Goliath web server.
38
84
  email: thiago_lewin@yahoo.com.br
39
85
  executables: []
40
-
41
86
  extensions: []
42
-
43
87
  extra_rdoc_files: []
44
-
45
- files:
88
+ files:
46
89
  - lib/helmet/api.rb
47
90
  - lib/helmet/handler.rb
48
91
  - lib/helmet/response.rb
49
92
  - lib/helmet/templates.rb
93
+ - lib/helmet/version.rb
50
94
  - lib/helmet.rb
95
+ - examples/hello_world.rb
96
+ - examples/helper.rb
97
+ - examples/misc.rb
98
+ - examples/public/css/helmet.css
99
+ - examples/runner.rb
100
+ - examples/views/index.erb
101
+ - examples/views/layout.erb
51
102
  - test/api_test.rb
52
103
  - test/helper_test.rb
53
104
  - test/template_test.rb
54
105
  - test/test_helper.rb
55
106
  - test/views/test.erb
107
+ - README.md
108
+ - Rakefile
109
+ - HISTORY.md
110
+ - Gemfile
111
+ - helmet.gemspec
112
+ - LICENSE
56
113
  homepage: https://github.com/tlewin/helmet
57
- licenses: []
58
-
114
+ licenses:
115
+ - MIT
116
+ metadata: {}
59
117
  post_install_message:
60
118
  rdoc_options: []
61
-
62
- require_paths:
119
+ require_paths:
63
120
  - lib
64
- required_ruby_version: !ruby/object:Gem::Requirement
65
- none: false
66
- requirements:
67
- - - ">="
68
- - !ruby/object:Gem::Version
69
- version: "0"
70
- required_rubygems_version: !ruby/object:Gem::Requirement
71
- none: false
72
- requirements:
73
- - - ">="
74
- - !ruby/object:Gem::Version
75
- version: "0"
121
+ required_ruby_version: !ruby/object:Gem::Requirement
122
+ requirements:
123
+ - - '>='
124
+ - !ruby/object:Gem::Version
125
+ version: '0'
126
+ required_rubygems_version: !ruby/object:Gem::Requirement
127
+ requirements:
128
+ - - '>='
129
+ - !ruby/object:Gem::Version
130
+ version: '0'
76
131
  requirements: []
77
-
78
132
  rubyforge_project:
79
- rubygems_version: 1.8.11
133
+ rubygems_version: 2.0.3
80
134
  signing_key:
81
- specification_version: 3
135
+ specification_version: 4
82
136
  summary: Simple web framework for Goliath web server.
83
- test_files: []
84
-
137
+ test_files:
138
+ - test/api_test.rb
139
+ - test/helper_test.rb
140
+ - test/template_test.rb
141
+ - test/test_helper.rb
142
+ - test/views/test.erb
85
143
  has_rdoc: