hovercraft 0.1.1 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -92,6 +92,48 @@ extensible at this point. Consider it a proof of concept.
92
92
  curl -X DELETE http://localhost:9292/employees/1.json
93
93
  ```
94
94
 
95
+ ## Authentication
96
+
97
+ 1. Include `warden` in your Gemfile:
98
+
99
+ ```ruby
100
+ gem 'warden'
101
+ ```
102
+
103
+ 2. Use rack builder to add warden strategies to your rackup file:
104
+
105
+ ```ruby
106
+ # config.ru
107
+
108
+ require 'bundler'
109
+ Bundler.require
110
+
111
+ application = Rack::Builder.new do
112
+ use Rack::Session::Cookie, secret: '...'
113
+
114
+ Warden::Strategies.add :password do
115
+ def valid?
116
+ ...
117
+ end
118
+
119
+ def authenticate
120
+ ...
121
+ end
122
+ end
123
+
124
+ use Warden::Manager do |manager|
125
+ manager.default_strategies :password
126
+ manager.failure_app = ...
127
+ end
128
+
129
+ run Hovercraft::Server
130
+ end
131
+
132
+ run application
133
+ ```
134
+
135
+ See the [warden project](https://github.com/hassox/warden/) for more in-depth examples or help troubleshooting.
136
+
95
137
  ## Give Back
96
138
 
97
139
  1. Fork it:
@@ -1,6 +1,7 @@
1
1
  require 'hovercraft/loader'
2
- require 'hovercraft/routes'
3
2
  require 'hovercraft/helpers'
3
+ require 'hovercraft/filters'
4
+ require 'hovercraft/routes'
4
5
  require 'sinatra/base'
5
6
  require 'rack/contrib/post_body_content_type_parser'
6
7
  require 'forwardable'
@@ -18,23 +19,34 @@ module Hovercraft
18
19
  def application
19
20
  application = Sinatra.new
20
21
  application = configure(application)
22
+ application = generate_filters(application)
21
23
  application = generate_routes(application)
22
24
  application
23
25
  end
24
26
 
25
27
  def configure(application)
26
28
  application.register(Hovercraft::Helpers)
29
+ application.register(Hovercraft::Filters)
27
30
  application.register(Hovercraft::Routes)
28
31
  application.use(Rack::PostBodyContentTypeParser)
29
32
  application
30
33
  end
31
34
 
35
+ def generate_filters(application)
36
+ Hovercraft::Filters.public_instance_methods.each do |filter|
37
+ application.send(filter)
38
+ end
39
+
40
+ application
41
+ end
42
+
32
43
  def generate_routes(application)
33
44
  with_each_model do |model_class, model_name, plural_model_name|
34
- application.methods.grep(/generate/).each do |action|
35
- application.send(action, model_class, model_name, plural_model_name)
45
+ Hovercraft::Routes.public_instance_methods.each do |route|
46
+ application.send(route, model_class, model_name, plural_model_name)
36
47
  end
37
48
  end
49
+
38
50
  application
39
51
  end
40
52
  end
@@ -0,0 +1,9 @@
1
+ module Hovercraft
2
+ module Filters
3
+ def generate_authentication_filter
4
+ before do
5
+ authenticate_with_warden
6
+ end
7
+ end
8
+ end
9
+ end
@@ -4,6 +4,14 @@ module Hovercraft
4
4
  application.helpers(Hovercraft::Helpers)
5
5
  end
6
6
 
7
+ def authenticate_with_warden
8
+ warden.authenticate! if warden
9
+ end
10
+
11
+ def warden
12
+ env.fetch('warden')
13
+ end
14
+
7
15
  def respond_with(content)
8
16
  if content.respond_to?(format_method_name)
9
17
  content.send(format_method_name)
@@ -1,3 +1,3 @@
1
1
  module Hovercraft
2
- VERSION = '0.1.1'
2
+ VERSION = '0.2.0'
3
3
  end
@@ -13,6 +13,11 @@ describe Hovercraft::Builder do
13
13
  end
14
14
 
15
15
  describe '#application' do
16
+ before do
17
+ Hovercraft::Filters.stub(public_instance_methods: [])
18
+ Hovercraft::Routes.stub(public_instance_methods: [])
19
+ end
20
+
16
21
  it 'creates a sinatra application' do
17
22
  subject.application.ancestors.should include(Sinatra::Base)
18
23
  end
@@ -23,6 +28,12 @@ describe Hovercraft::Builder do
23
28
  subject.application
24
29
  end
25
30
 
31
+ it 'generates filters for the application' do
32
+ subject.should_receive(:generate_filters)
33
+
34
+ subject.application
35
+ end
36
+
26
37
  it 'generates routes for the application' do
27
38
  subject.should_receive(:generate_routes)
28
39
 
@@ -39,7 +50,13 @@ describe Hovercraft::Builder do
39
50
  subject.configure(application)
40
51
  end
41
52
 
42
- it 'registers the methods to generate actions' do
53
+ it 'registers the methods to generate filters' do
54
+ application.should_receive(:register).with(Hovercraft::Filters)
55
+
56
+ subject.configure(application)
57
+ end
58
+
59
+ it 'registers the methods to generate routes' do
43
60
  application.should_receive(:register).with(Hovercraft::Routes)
44
61
 
45
62
  subject.configure(application)
@@ -0,0 +1,34 @@
1
+ require 'hovercraft/filters'
2
+ require 'hovercraft/helpers'
3
+ require 'sinatra'
4
+ require 'rack/test'
5
+
6
+ describe Hovercraft::Filters do
7
+ include Rack::Test::Methods
8
+
9
+ let(:application) { Sinatra.new }
10
+
11
+ alias :app :application
12
+
13
+ before do
14
+ application.register(Hovercraft::Helpers)
15
+ application.register(Hovercraft::Filters)
16
+ end
17
+
18
+ describe '#generate_authentication_filter' do
19
+ before do
20
+ application.generate_authentication_filter
21
+ end
22
+
23
+ it 'generates a global filter' do
24
+ application.filters[:before][0][0] == //
25
+ end
26
+
27
+ it 'authenticates a request' do
28
+ block = application.filters[:before][0][3]
29
+ block.should_receive(:[])
30
+
31
+ get '/'
32
+ end
33
+ end
34
+ end
@@ -15,6 +15,31 @@ describe Hovercraft::Helpers do
15
15
  end
16
16
  end
17
17
 
18
+ describe '#authenticate_with_warden' do
19
+ let(:warden) { stub(authenticate!: nil) }
20
+
21
+ before { subject.stub(warden: warden) }
22
+
23
+ it 'uses warden to authenticate' do
24
+ warden.should_receive(:authenticate!)
25
+
26
+ subject.authenticate_with_warden
27
+ end
28
+ end
29
+
30
+ describe '#warden' do
31
+ let(:warden) { stub }
32
+ let(:env) { stub }
33
+
34
+ before { subject.stub(env: env) }
35
+
36
+ it 'finds the warden instance in the current session' do
37
+ env.stub(:fetch).with('warden') { warden }
38
+
39
+ subject.warden
40
+ end
41
+ end
42
+
18
43
  describe '#respond_with' do
19
44
  it 'serializes the content to the preferred format' do
20
45
  subject.stub(format: :json)
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hovercraft
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.2.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-07-16 00:00:00.000000000 Z
12
+ date: 2012-07-30 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: sinatra-activerecord
@@ -141,6 +141,7 @@ files:
141
141
  - lib/hovercraft.rb
142
142
  - lib/hovercraft/builder.rb
143
143
  - lib/hovercraft/caller.rb
144
+ - lib/hovercraft/filters.rb
144
145
  - lib/hovercraft/helpers.rb
145
146
  - lib/hovercraft/loader.rb
146
147
  - lib/hovercraft/routes.rb
@@ -148,6 +149,7 @@ files:
148
149
  - lib/hovercraft/version.rb
149
150
  - spec/hovercraft/builder_spec.rb
150
151
  - spec/hovercraft/caller_spec.rb
152
+ - spec/hovercraft/filters_spec.rb
151
153
  - spec/hovercraft/helpers_spec.rb
152
154
  - spec/hovercraft/loader_spec.rb
153
155
  - spec/hovercraft/routes_spec.rb
@@ -172,10 +174,10 @@ required_rubygems_version: !ruby/object:Gem::Requirement
172
174
  version: '0'
173
175
  segments:
174
176
  - 0
175
- hash: 3375812877038520870
177
+ hash: -2574273508112493119
176
178
  requirements: []
177
179
  rubyforge_project:
178
- rubygems_version: 1.8.24
180
+ rubygems_version: 1.8.23
179
181
  signing_key:
180
182
  specification_version: 3
181
183
  summary: There's a lot of boiler plate code that goes into creating an API so why
@@ -185,6 +187,7 @@ summary: There's a lot of boiler plate code that goes into creating an API so wh
185
187
  test_files:
186
188
  - spec/hovercraft/builder_spec.rb
187
189
  - spec/hovercraft/caller_spec.rb
190
+ - spec/hovercraft/filters_spec.rb
188
191
  - spec/hovercraft/helpers_spec.rb
189
192
  - spec/hovercraft/loader_spec.rb
190
193
  - spec/hovercraft/routes_spec.rb