rack-flags 0.1.2 → 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
data/lib/rack-flags.rb CHANGED
@@ -1,15 +1,15 @@
1
- require 'rack-flags/reader'
2
- require 'rack-flags/cookie_codec'
3
- require 'rack-flags/config'
4
- require 'rack-flags/rack_middleware'
5
- require 'rack-flags/admin_app'
6
-
7
1
  module RackFlags
8
2
  def self.for_env(env)
9
3
  env[RackMiddleware::ENV_KEY] || Reader.blank_reader
10
4
  end
11
5
 
12
- def self.path_for_resource(subpath)
13
- File.expand_path( File.join( "../../resources", subpath ), __FILE__ )
6
+ def self.resources_path_for(subpath)
7
+ File.expand_path( File.join('../../resources', subpath), __FILE__ )
14
8
  end
15
9
  end
10
+
11
+ require 'rack-flags/reader'
12
+ require 'rack-flags/cookie_codec'
13
+ require 'rack-flags/config'
14
+ require 'rack-flags/rack_middleware'
15
+ require 'rack-flags/admin_app'
@@ -1,4 +1,4 @@
1
- require 'erb'
1
+ require 'sinatra/base'
2
2
 
3
3
  module RackFlags
4
4
  class FullFlagPresenter
@@ -35,69 +35,28 @@ module RackFlags
35
35
 
36
36
  end
37
37
 
38
- class AdminApp
39
- def call(env)
40
- request = Rack::Request.new(env)
38
+ class AdminApp < Sinatra::Base
39
+ set :public_folder, RackFlags.resources_path_for('admin_app')
40
+ set :views, RackFlags.resources_path_for('admin_app')
41
41
 
42
- if request.path_info.chomp("/").empty?
43
- handle_root(request)
44
- else
45
- handle_non_root(request)
46
- end
47
- end
48
-
49
- private
50
-
51
- def handle_root(request)
52
- case
53
- when request.get? then handle_root_get(request)
54
- when request.post? then handle_root_post(request)
55
- else
56
- not_allowed
57
- end
58
- end
59
-
60
- def handle_root_get(request)
61
- reader = RackFlags.for_env(request.env)
62
-
63
- template = ERB.new(resource('index.html.erb'))
64
-
65
-
66
- flag_presenters = reader.full_flags.map{ |flag| FullFlagPresenter.new(flag) }
67
- view_model = OpenStruct.new(
68
- :css_href => "#{request.path}/style.css",
69
- :flags => flag_presenters
70
- )
71
-
72
- [
73
- 200,
74
- {'Content-Type'=>'text/html'},
75
- [template.result( view_model.instance_eval{ binding } )]
76
- ]
77
- end
42
+ get '/' do
43
+ reader = RackFlags.for_env(request.env)
44
+ flag_presenters = reader.full_flags.map{ |flag| FullFlagPresenter.new(flag) }
78
45
 
79
- def handle_root_post(request)
80
- overrides = request.POST.inject({}) do |overrides, (flag_name, form_param_flag_state)|
81
- overrides[flag_name.downcase.to_sym] = flag_value_for(form_param_flag_state)
82
- overrides
83
- end
84
-
85
- cookie = CookieCodec.new.generate_cookie_from(overrides)
86
-
87
- response = Rack::Response.new
88
- response.redirect(request.script_name, 303)
89
- response.set_cookie(CookieCodec::COOKIE_KEY, cookie)
46
+ erb :index, locals: {css_href: "#{request.path.chomp('/')}/style.css", flags: flag_presenters}
47
+ end
90
48
 
91
- response.finish
49
+ post '/' do
50
+ overrides = params.inject({}) do |overrides, (flag_name, form_param_flag_state)|
51
+ overrides[flag_name.downcase.to_sym] = flag_value_for(form_param_flag_state)
52
+ overrides
92
53
  end
93
54
 
94
- def handle_non_root(request)
95
- Rack::File.new( RackFlags.path_for_resource('admin_app') ).call(request.env)
96
- end
55
+ response.set_cookie(CookieCodec::COOKIE_KEY, value: CookieCodec.new.generate_cookie_from(overrides))
56
+ redirect to('/'), 303
57
+ end
97
58
 
98
- def not_allowed
99
- [405, {}, ['405 - METHOD NOT ALLOWED']]
100
- end
59
+ private
101
60
 
102
61
  def flag_value_for(form_param_flag_state)
103
62
  flag_states = {
@@ -108,13 +67,5 @@ module RackFlags
108
67
  flag_states[form_param_flag_state.to_sym]
109
68
  end
110
69
 
111
- def resource_root
112
- RackFlags.path_for_resource('admin_app')
113
- end
114
-
115
- def resource(filename)
116
- File.read(RackFlags.path_for_resource(File.join('admin_app',filename)))
117
- end
118
-
119
70
  end
120
71
  end
@@ -1,3 +1,3 @@
1
1
  module RackFlags
2
- VERSION = "0.1.2"
2
+ VERSION = "0.1.3"
3
3
  end
@@ -0,0 +1,35 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>rack-flags admin</title>
5
+ <link href="<%=css_href%>" rel="stylesheet" type="text/css">
6
+ </head>
7
+ <body>
8
+ <h1>rack-flags admin</h1>
9
+ <form method="post">
10
+ <%flags.each do |flag|%>
11
+ <section data-flag-name="<%=flag.name%>">
12
+ <h3><%=flag.name%></h3>
13
+ <p><%=flag.description%></p>
14
+ <div>
15
+ <label class="default">
16
+ Default (<%=flag.default%>)
17
+ <input type="radio" name="<%=flag.name%>" value="default" <%=flag.checked_attribute_for(:default)%>/>
18
+ </label>
19
+
20
+ <label class="on">
21
+ On
22
+ <input type="radio" name="<%=flag.name%>" value="on" <%=flag.checked_attribute_for(:on)%>/>
23
+ </label>
24
+
25
+ <label class="off">
26
+ Off
27
+ <input type="radio" name="<%=flag.name%>" value="off" <%=flag.checked_attribute_for(:off)%>/>
28
+ </label>
29
+ </div>
30
+ </section>
31
+ <%end%>
32
+ <input type="submit" value="Update Flags"/>
33
+ </form>
34
+ </body>
35
+ </html>
@@ -1,5 +1,4 @@
1
1
  require_relative '../spec_helper'
2
- require 'rack/test'
3
2
  require 'capybara/rspec'
4
3
 
5
4
  module ConfigFileHelper
@@ -1,4 +1,5 @@
1
- require 'sinatra'
1
+ require 'sinatra/base'
2
+
2
3
  class ReaderApp < Sinatra::Base
3
4
  enable :raise_errors
4
5
  disable :show_exceptions
data/spec/spec_helper.rb CHANGED
@@ -1,6 +1,7 @@
1
1
  require 'rr'
2
2
  require 'pry'
3
3
  require 'rack'
4
+ require 'rack/test'
4
5
 
5
6
  require_relative '../lib/rack-flags'
6
7
 
@@ -1,82 +1,38 @@
1
1
  require_relative 'spec_helper'
2
2
 
3
3
  module RackFlags
4
-
5
4
  describe AdminApp do
6
- describe '#call' do
7
- let(:fake_env) { {} }
8
- let(:admin_app) { AdminApp.new }
9
-
10
- context 'when the request is a get' do
11
- let(:fake_env) { {'REQUEST_METHOD' => 'GET'} }
5
+ include Rack::Test::Methods
12
6
 
13
- it 'returns a successful HTML response' do
14
- status, headers, body = admin_app.call(fake_env)
7
+ let(:app) { RackFlags::AdminApp }
15
8
 
16
- expect(status).to eq(200)
17
- expect(headers).to include({'Content-Type' => 'text/html'})
18
- expect(body).to_not be_empty
19
- end
9
+ describe 'GET' do
10
+ it 'is a success' do
11
+ get '/'
20
12
 
13
+ expect(last_response).to be_ok
14
+ expect(last_response.body).to_not be_empty
21
15
  end
16
+ end
22
17
 
23
- context 'when the request is a post' do
24
- let(:fake_env) { {'REQUEST_METHOD' => 'POST', 'SCRIPT_NAME' => 'admin-app' } }
25
-
26
- before do
27
- any_instance_of(Rack::Request) do |rack_request|
28
- stub(rack_request).POST { {} }
29
- end
30
- end
31
-
32
- it 'returns a 303 redirect response' do
33
- status, headers , body = admin_app.call(fake_env)
34
-
35
- expect(status).to eq(303)
36
- expect(headers).to include({'Location' => 'admin-app'})
37
- expect(body).to be_empty
38
- end
39
-
40
- it 'sets the cookie using the post params' do
41
- any_instance_of(Rack::Request) do |rack_request|
42
- stub(rack_request).POST { {flag_1: 'on', flag_2: 'off', flag_3: 'default'} }
43
- end
44
-
45
- stub.proxy(CookieCodec).new do |cookie_codec|
46
- mock(cookie_codec).generate_cookie_from({flag_1: true, flag_2: false, flag_3: nil}) { 'flag_1 !flag2' }
47
- end
48
-
49
- any_instance_of(Rack::Response) do |rack_response|
50
- mock(rack_response).set_cookie(CookieCodec::COOKIE_KEY, 'flag_1 !flag2')
51
- end
52
-
53
- admin_app.call(fake_env)
54
- end
55
-
56
- it 'returns the finished response' do
57
- stub.proxy(Rack::Response).new do |rack_response|
58
- mock(rack_response).finish { 'finished rack response' }
59
- end
60
-
61
- response = admin_app.call(fake_env)
62
-
63
- expect(response).to eq('finished rack response')
64
- end
18
+ describe 'POST' do
19
+ it 'is a redirect to GET' do
20
+ post '/'
65
21
 
22
+ expect(last_response.status).to eq(303)
23
+ expect(last_response.location).to eq('http://example.org/') # Root path - Rack::Test::DEFAULT_HOST is example.org
24
+ expect(last_response.body).to be_empty
66
25
  end
67
26
 
68
- context 'when the request is something else' do
69
- let(:fake_env) { {'REQUEST_METHOD' => 'OTHER'} }
27
+ it 'sets the cookie based on the posted params' do
28
+ stub.proxy(CookieCodec).new do |cookie_codec|
29
+ mock(cookie_codec).generate_cookie_from({flag_1: true, flag_2: false, flag_3: nil}) { 'flag_1 !flag_2' }
30
+ end
70
31
 
71
- it 'returns a 405 method not allowed response' do
72
- status, headers , body = admin_app.call(fake_env)
32
+ post '/', flag_1: 'on', flag_2: 'off', flag_3: 'default'
73
33
 
74
- expect(status).to eq(405)
75
- expect(headers).to be_empty
76
- expect(body).to eq('405 - METHOD NOT ALLOWED')
77
- end
34
+ expect(rack_mock_session.cookie_jar[CookieCodec::COOKIE_KEY]).to eq('flag_1 !flag_2')
78
35
  end
79
-
80
36
  end
81
37
 
82
38
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rack-flags
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2013-07-14 00:00:00.000000000 Z
13
+ date: 2013-07-16 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: rake
@@ -155,7 +155,7 @@ files:
155
155
  - lib/rack-flags/rack_middleware.rb
156
156
  - lib/rack-flags/reader.rb
157
157
  - lib/rack-flags/version.rb
158
- - resources/admin_app/index.html.erb
158
+ - resources/admin_app/index.erb
159
159
  - resources/admin_app/style.css
160
160
  - spec/acceptance/administering_feature_flags_spec.rb
161
161
  - spec/acceptance/reading_feature_flags_spec.rb
@@ -183,7 +183,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
183
183
  version: '0'
184
184
  segments:
185
185
  - 0
186
- hash: -2280499656748688466
186
+ hash: -2393715893714725756
187
187
  required_rubygems_version: !ruby/object:Gem::Requirement
188
188
  none: false
189
189
  requirements:
@@ -192,7 +192,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
192
192
  version: '0'
193
193
  segments:
194
194
  - 0
195
- hash: -2280499656748688466
195
+ hash: -2393715893714725756
196
196
  requirements: []
197
197
  rubyforge_project:
198
198
  rubygems_version: 1.8.25
@@ -1,35 +0,0 @@
1
- <!DOCTYPE html>
2
- <html>
3
- <head>
4
- <title>rack-flags admin</title>
5
- <link href="<%=css_href%>" rel="stylesheet" type="text/css">
6
- </head>
7
- <body>
8
- <h1>rack-flags admin</h1>
9
- <form method="post">
10
- <%flags.each do |flag|%>
11
- <section data-flag-name="<%=flag.name%>">
12
- <h3><%=flag.name%></h3>
13
- <p><%=flag.description%></p>
14
- <div>
15
- <label class="default">
16
- Default (<%=flag.default%>)
17
- <input type="radio" name="<%=flag.name%>" value="default" <%=flag.checked_attribute_for(:default)%>/>
18
- </label>
19
-
20
- <label class="on">
21
- On
22
- <input type="radio" name="<%=flag.name%>" value="on" <%=flag.checked_attribute_for(:on)%>/>
23
- </label>
24
-
25
- <label class="off">
26
- Off
27
- <input type="radio" name="<%=flag.name%>" value="off" <%=flag.checked_attribute_for(:off)%>/>
28
- </label>
29
- </div>
30
- </section>
31
- <%end%>
32
- <input type="submit" value="Update Flags"/>
33
- </form>
34
- </body>
35
- </html>