ballast 1.0.0 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -134,7 +134,7 @@
134
134
 
135
135
  </div>
136
136
  </dt>
137
- <dd><pre class="code"><span class='int'>0</span></pre></dd>
137
+ <dd><pre class="code"><span class='int'>1</span></pre></dd>
138
138
 
139
139
  <dt id="PATCH-constant" class="">PATCH =
140
140
  <div class="docstring">
@@ -180,7 +180,7 @@
180
180
  </div>
181
181
 
182
182
  <div id="footer">
183
- Generated on Wed Dec 25 11:35:22 2013 by
183
+ Generated on Wed Dec 25 14:18:00 2013 by
184
184
  <a href="http://yardoc.org" title="Yay! A Ruby Documentation Tool" target="_parent">yard</a>
185
185
  0.8.7.3 (ruby-2.0.0).
186
186
  </div>
data/doc/Ballast.html CHANGED
@@ -80,7 +80,7 @@
80
80
 
81
81
  <dt class="r1 last">Defined in:</dt>
82
82
  <dd class="r1 last">lib/ballast/errors.rb<span class="defines">,<br />
83
- lib/ballast/version.rb,<br /> lib/ballast/context.rb,<br /> lib/ballast/operation.rb,<br /> lib/ballast/concerns/view.rb,<br /> lib/ballast/concerns/ajax.rb,<br /> lib/ballast/concerns/common.rb,<br /> lib/ballast/operations_chain.rb,<br /> lib/ballast/concerns/errors_handling.rb</span>
83
+ lib/ballast/context.rb,<br /> lib/ballast/version.rb,<br /> lib/ballast/operation.rb,<br /> lib/ballast/concerns/view.rb,<br /> lib/ballast/concerns/ajax.rb,<br /> lib/ballast/concerns/common.rb,<br /> lib/ballast/operations_chain.rb,<br /> lib/ballast/request_domain_matcher.rb,<br /> lib/ballast/concerns/errors_handling.rb,<br /> lib/ballast/middlewares/default_host.rb</span>
84
84
  </dd>
85
85
 
86
86
  </dl>
@@ -101,11 +101,11 @@ Licensed under the MIT license, which can be found at http://www.opensource.org/
101
101
  <p class="children">
102
102
 
103
103
 
104
- <strong class="modules">Modules:</strong> <span class='object_link'><a href="Ballast/Concerns.html" title="Ballast::Concerns (module)">Concerns</a></span>, <span class='object_link'><a href="Ballast/Errors.html" title="Ballast::Errors (module)">Errors</a></span>, <span class='object_link'><a href="Ballast/Version.html" title="Ballast::Version (module)">Version</a></span>
104
+ <strong class="modules">Modules:</strong> <span class='object_link'><a href="Ballast/Concerns.html" title="Ballast::Concerns (module)">Concerns</a></span>, <span class='object_link'><a href="Ballast/Errors.html" title="Ballast::Errors (module)">Errors</a></span>, <span class='object_link'><a href="Ballast/Middlewares.html" title="Ballast::Middlewares (module)">Middlewares</a></span>, <span class='object_link'><a href="Ballast/Version.html" title="Ballast::Version (module)">Version</a></span>
105
105
 
106
106
 
107
107
 
108
- <strong class="classes">Classes:</strong> <span class='object_link'><a href="Ballast/Context.html" title="Ballast::Context (class)">Context</a></span>, <span class='object_link'><a href="Ballast/Operation.html" title="Ballast::Operation (class)">Operation</a></span>, <span class='object_link'><a href="Ballast/OperationsChain.html" title="Ballast::OperationsChain (class)">OperationsChain</a></span>
108
+ <strong class="classes">Classes:</strong> <span class='object_link'><a href="Ballast/Context.html" title="Ballast::Context (class)">Context</a></span>, <span class='object_link'><a href="Ballast/Operation.html" title="Ballast::Operation (class)">Operation</a></span>, <span class='object_link'><a href="Ballast/OperationsChain.html" title="Ballast::OperationsChain (class)">OperationsChain</a></span>, <span class='object_link'><a href="Ballast/RequestDomainMatcher.html" title="Ballast::RequestDomainMatcher (class)">RequestDomainMatcher</a></span>
109
109
 
110
110
 
111
111
  </p>
@@ -121,7 +121,7 @@ Licensed under the MIT license, which can be found at http://www.opensource.org/
121
121
  </div>
122
122
 
123
123
  <div id="footer">
124
- Generated on Wed Dec 25 11:35:22 2013 by
124
+ Generated on Wed Dec 25 14:18:00 2013 by
125
125
  <a href="http://yardoc.org" title="Yay! A Ruby Documentation Tool" target="_parent">yard</a>
126
126
  0.8.7.3 (ruby-2.0.0).
127
127
  </div>
data/doc/_index.html CHANGED
@@ -144,6 +144,21 @@
144
144
  </ul>
145
145
 
146
146
 
147
+ <ul id="alpha_D" class="alpha">
148
+ <li class="letter">D</li>
149
+ <ul>
150
+
151
+ <li>
152
+ <span class='object_link'><a href="Ballast/Middlewares/DefaultHost.html" title="Ballast::Middlewares::DefaultHost (class)">DefaultHost</a></span>
153
+
154
+ <small>(Ballast::Middlewares)</small>
155
+
156
+ </li>
157
+
158
+ </ul>
159
+ </ul>
160
+
161
+
147
162
  <ul id="alpha_E" class="alpha">
148
163
  <li class="letter">E</li>
149
164
  <ul>
@@ -181,6 +196,24 @@
181
196
  </ul>
182
197
 
183
198
 
199
+ <ul id="alpha_M" class="alpha">
200
+ <li class="letter">M</li>
201
+ <ul>
202
+
203
+ <li>
204
+ <span class='object_link'><a href="Ballast/Middlewares.html" title="Ballast::Middlewares (module)">Middlewares</a></span>
205
+
206
+ <small>(Ballast)</small>
207
+
208
+ </li>
209
+
210
+ </ul>
211
+ </ul>
212
+
213
+
214
+ </td><td valign='top' width="33%">
215
+
216
+
184
217
  <ul id="alpha_O" class="alpha">
185
218
  <li class="letter">O</li>
186
219
  <ul>
@@ -218,8 +251,20 @@
218
251
  </ul>
219
252
 
220
253
 
221
- </td><td valign='top' width="33%">
222
-
254
+ <ul id="alpha_R" class="alpha">
255
+ <li class="letter">R</li>
256
+ <ul>
257
+
258
+ <li>
259
+ <span class='object_link'><a href="Ballast/RequestDomainMatcher.html" title="Ballast::RequestDomainMatcher (class)">RequestDomainMatcher</a></span>
260
+
261
+ <small>(Ballast)</small>
262
+
263
+ </li>
264
+
265
+ </ul>
266
+ </ul>
267
+
223
268
 
224
269
  <ul id="alpha_V" class="alpha">
225
270
  <li class="letter">V</li>
@@ -258,7 +303,7 @@
258
303
  </div>
259
304
 
260
305
  <div id="footer">
261
- Generated on Wed Dec 25 11:35:22 2013 by
306
+ Generated on Wed Dec 25 14:17:59 2013 by
262
307
  <a href="http://yardoc.org" title="Yay! A Ruby Documentation Tool" target="_parent">yard</a>
263
308
  0.8.7.3 (ruby-2.0.0).
264
309
  </div>
data/doc/class_list.html CHANGED
@@ -46,7 +46,7 @@
46
46
 
47
47
  <ul id="full_list" class="class">
48
48
  <li><span class='object_link'><a href="top-level-namespace.html" title="Top Level Namespace (root)">Top Level Namespace</a></span></li>
49
- <li><a class='toggle'></a> <span class='object_link'><a href="Ballast.html" title="Ballast (module)">Ballast</a></span><small class='search_info'>Top Level Namespace</small></li><ul><li><a class='toggle'></a> <span class='object_link'><a href="Ballast/Concerns.html" title="Ballast::Concerns (module)">Concerns</a></span><small class='search_info'>Ballast</small></li><ul><li><span class='object_link'><a href="Ballast/Concerns/Ajax.html" title="Ballast::Concerns::Ajax (module)">Ajax</a></span><small class='search_info'>Ballast::Concerns</small></li><li><span class='object_link'><a href="Ballast/Concerns/Common.html" title="Ballast::Concerns::Common (module)">Common</a></span><small class='search_info'>Ballast::Concerns</small></li><li><span class='object_link'><a href="Ballast/Concerns/ErrorsHandling.html" title="Ballast::Concerns::ErrorsHandling (module)">ErrorsHandling</a></span><small class='search_info'>Ballast::Concerns</small></li><li><span class='object_link'><a href="Ballast/Concerns/View.html" title="Ballast::Concerns::View (module)">View</a></span><small class='search_info'>Ballast::Concerns</small></li></ul><li><span class='object_link'><a href="Ballast/Context.html" title="Ballast::Context (class)">Context</a></span> &lt; Context<small class='search_info'>Ballast</small></li><li><a class='toggle'></a> <span class='object_link'><a href="Ballast/Errors.html" title="Ballast::Errors (module)">Errors</a></span><small class='search_info'>Ballast</small></li><ul><li><span class='object_link'><a href="Ballast/Errors/BaseError.html" title="Ballast::Errors::BaseError (class)">BaseError</a></span> &lt; RuntimeError<small class='search_info'>Ballast::Errors</small></li><li><span class='object_link'><a href="Ballast/Errors/InvalidDomain.html" title="Ballast::Errors::InvalidDomain (class)">InvalidDomain</a></span> &lt; BaseError<small class='search_info'>Ballast::Errors</small></li><li><span class='object_link'><a href="Ballast/Errors/PerformError.html" title="Ballast::Errors::PerformError (class)">PerformError</a></span> &lt; BaseError<small class='search_info'>Ballast::Errors</small></li><li><span class='object_link'><a href="Ballast/Errors/ValidationError.html" title="Ballast::Errors::ValidationError (class)">ValidationError</a></span> &lt; BaseError<small class='search_info'>Ballast::Errors</small></li></ul><li><span class='object_link'><a href="Ballast/Operation.html" title="Ballast::Operation (class)">Operation</a></span> &lt; Object<small class='search_info'>Ballast</small></li><li><span class='object_link'><a href="Ballast/OperationsChain.html" title="Ballast::OperationsChain (class)">OperationsChain</a></span> &lt; Operation<small class='search_info'>Ballast</small></li><li><span class='object_link'><a href="Ballast/Version.html" title="Ballast::Version (module)">Version</a></span><small class='search_info'>Ballast</small></li></ul>
49
+ <li><a class='toggle'></a> <span class='object_link'><a href="Ballast.html" title="Ballast (module)">Ballast</a></span><small class='search_info'>Top Level Namespace</small></li><ul><li><a class='toggle'></a> <span class='object_link'><a href="Ballast/Concerns.html" title="Ballast::Concerns (module)">Concerns</a></span><small class='search_info'>Ballast</small></li><ul><li><span class='object_link'><a href="Ballast/Concerns/Ajax.html" title="Ballast::Concerns::Ajax (module)">Ajax</a></span><small class='search_info'>Ballast::Concerns</small></li><li><span class='object_link'><a href="Ballast/Concerns/Common.html" title="Ballast::Concerns::Common (module)">Common</a></span><small class='search_info'>Ballast::Concerns</small></li><li><span class='object_link'><a href="Ballast/Concerns/ErrorsHandling.html" title="Ballast::Concerns::ErrorsHandling (module)">ErrorsHandling</a></span><small class='search_info'>Ballast::Concerns</small></li><li><span class='object_link'><a href="Ballast/Concerns/View.html" title="Ballast::Concerns::View (module)">View</a></span><small class='search_info'>Ballast::Concerns</small></li></ul><li><span class='object_link'><a href="Ballast/Context.html" title="Ballast::Context (class)">Context</a></span> &lt; Context<small class='search_info'>Ballast</small></li><li><a class='toggle'></a> <span class='object_link'><a href="Ballast/Errors.html" title="Ballast::Errors (module)">Errors</a></span><small class='search_info'>Ballast</small></li><ul><li><span class='object_link'><a href="Ballast/Errors/BaseError.html" title="Ballast::Errors::BaseError (class)">BaseError</a></span> &lt; RuntimeError<small class='search_info'>Ballast::Errors</small></li><li><span class='object_link'><a href="Ballast/Errors/InvalidDomain.html" title="Ballast::Errors::InvalidDomain (class)">InvalidDomain</a></span> &lt; BaseError<small class='search_info'>Ballast::Errors</small></li><li><span class='object_link'><a href="Ballast/Errors/PerformError.html" title="Ballast::Errors::PerformError (class)">PerformError</a></span> &lt; BaseError<small class='search_info'>Ballast::Errors</small></li><li><span class='object_link'><a href="Ballast/Errors/ValidationError.html" title="Ballast::Errors::ValidationError (class)">ValidationError</a></span> &lt; BaseError<small class='search_info'>Ballast::Errors</small></li></ul><li><a class='toggle'></a> <span class='object_link'><a href="Ballast/Middlewares.html" title="Ballast::Middlewares (module)">Middlewares</a></span><small class='search_info'>Ballast</small></li><ul><li><span class='object_link'><a href="Ballast/Middlewares/DefaultHost.html" title="Ballast::Middlewares::DefaultHost (class)">DefaultHost</a></span> &lt; Object<small class='search_info'>Ballast::Middlewares</small></li></ul><li><span class='object_link'><a href="Ballast/Operation.html" title="Ballast::Operation (class)">Operation</a></span> &lt; Object<small class='search_info'>Ballast</small></li><li><span class='object_link'><a href="Ballast/OperationsChain.html" title="Ballast::OperationsChain (class)">OperationsChain</a></span> &lt; Operation<small class='search_info'>Ballast</small></li><li><span class='object_link'><a href="Ballast/RequestDomainMatcher.html" title="Ballast::RequestDomainMatcher (class)">RequestDomainMatcher</a></span> &lt; Object<small class='search_info'>Ballast</small></li><li><span class='object_link'><a href="Ballast/Version.html" title="Ballast::Version (module)">Version</a></span><small class='search_info'>Ballast</small></li></ul>
50
50
 
51
51
  </ul>
52
52
  </div>
data/doc/file.README.html CHANGED
@@ -106,7 +106,7 @@
106
106
  </div></div>
107
107
 
108
108
  <div id="footer">
109
- Generated on Wed Dec 25 11:35:22 2013 by
109
+ Generated on Wed Dec 25 14:18:00 2013 by
110
110
  <a href="http://yardoc.org" title="Yay! A Ruby Documentation Tool" target="_parent">yard</a>
111
111
  0.8.7.3 (ruby-2.0.0).
112
112
  </div>
data/doc/index.html CHANGED
@@ -106,7 +106,7 @@
106
106
  </div></div>
107
107
 
108
108
  <div id="footer">
109
- Generated on Wed Dec 25 11:35:22 2013 by
109
+ Generated on Wed Dec 25 14:18:00 2013 by
110
110
  <a href="http://yardoc.org" title="Yay! A Ruby Documentation Tool" target="_parent">yard</a>
111
111
  0.8.7.3 (ruby-2.0.0).
112
112
  </div>
data/doc/method_list.html CHANGED
@@ -84,11 +84,23 @@
84
84
 
85
85
 
86
86
  <li class="r1 ">
87
+ <span class='object_link'><a href="Ballast/Middlewares/DefaultHost.html#call-instance_method" title="Ballast::Middlewares::DefaultHost#call (method)">#call</a></span>
88
+ <small>Ballast::Middlewares::DefaultHost</small>
89
+ </li>
90
+
91
+
92
+ <li class="r2 ">
87
93
  <span class='object_link'><a href="Ballast/Concerns/Ajax.html#disallow_robots-instance_method" title="Ballast::Concerns::Ajax#disallow_robots (method)">#disallow_robots</a></span>
88
94
  <small>Ballast::Concerns::Ajax</small>
89
95
  </li>
90
96
 
91
97
 
98
+ <li class="r1 ">
99
+ <span class='object_link'><a href="Ballast/RequestDomainMatcher.html#domains-instance_method" title="Ballast::RequestDomainMatcher#domains (method)">#domains</a></span>
100
+ <small>Ballast::RequestDomainMatcher</small>
101
+ </li>
102
+
103
+
92
104
  <li class="r2 ">
93
105
  <span class='object_link'><a href="Ballast/Operation.html#fail%21-instance_method" title="Ballast::Operation#fail! (method)">#fail!</a></span>
94
106
  <small>Ballast::Operation</small>
@@ -144,11 +156,23 @@
144
156
 
145
157
 
146
158
  <li class="r1 ">
159
+ <span class='object_link'><a href="Ballast/Middlewares/DefaultHost.html#initialize-instance_method" title="Ballast::Middlewares::DefaultHost#initialize (method)">#initialize</a></span>
160
+ <small>Ballast::Middlewares::DefaultHost</small>
161
+ </li>
162
+
163
+
164
+ <li class="r2 ">
147
165
  <span class='object_link'><a href="Ballast/OperationsChain.html#initialize-instance_method" title="Ballast::OperationsChain#initialize (method)">#initialize</a></span>
148
166
  <small>Ballast::OperationsChain</small>
149
167
  </li>
150
168
 
151
169
 
170
+ <li class="r1 ">
171
+ <span class='object_link'><a href="Ballast/RequestDomainMatcher.html#initialize-instance_method" title="Ballast::RequestDomainMatcher#initialize (method)">#initialize</a></span>
172
+ <small>Ballast::RequestDomainMatcher</small>
173
+ </li>
174
+
175
+
152
176
  <li class="r2 ">
153
177
  <span class='object_link'><a href="Ballast/Operation.html#initialize-instance_method" title="Ballast::Operation#initialize (method)">#initialize</a></span>
154
178
  <small>Ballast::Operation</small>
@@ -168,8 +192,8 @@
168
192
 
169
193
 
170
194
  <li class="r1 ">
171
- <span class='object_link'><a href="Ballast/Operation.html#method_missing-instance_method" title="Ballast::Operation#method_missing (method)">#method_missing</a></span>
172
- <small>Ballast::Operation</small>
195
+ <span class='object_link'><a href="Ballast/RequestDomainMatcher.html#matches%3F-instance_method" title="Ballast::RequestDomainMatcher#matches? (method)">#matches?</a></span>
196
+ <small>Ballast::RequestDomainMatcher</small>
173
197
  </li>
174
198
 
175
199
 
@@ -180,47 +204,71 @@
180
204
 
181
205
 
182
206
  <li class="r1 ">
207
+ <span class='object_link'><a href="Ballast/Operation.html#method_missing-instance_method" title="Ballast::Operation#method_missing (method)">#method_missing</a></span>
208
+ <small>Ballast::Operation</small>
209
+ </li>
210
+
211
+
212
+ <li class="r2 ">
183
213
  <span class='object_link'><a href="Ballast/OperationsChain.html#operations-instance_method" title="Ballast::OperationsChain#operations (method)">#operations</a></span>
184
214
  <small>Ballast::OperationsChain</small>
185
215
  </li>
186
216
 
187
217
 
188
- <li class="r2 ">
218
+ <li class="r1 ">
189
219
  <span class='object_link'><a href="Ballast/Operation.html#perform-class_method" title="Ballast::Operation.perform (method)">perform</a></span>
190
220
  <small>Ballast::Operation</small>
191
221
  </li>
192
222
 
193
223
 
194
- <li class="r1 ">
224
+ <li class="r2 ">
195
225
  <span class='object_link'><a href="Ballast/OperationsChain.html#perform-class_method" title="Ballast::OperationsChain.perform (method)">perform</a></span>
196
226
  <small>Ballast::OperationsChain</small>
197
227
  </li>
198
228
 
199
229
 
200
- <li class="r2 ">
230
+ <li class="r1 ">
201
231
  <span class='object_link'><a href="Ballast/Concerns/Common.html#perform_operation-instance_method" title="Ballast::Concerns::Common#perform_operation (method)">#perform_operation</a></span>
202
232
  <small>Ballast::Concerns::Common</small>
203
233
  </li>
204
234
 
205
235
 
206
- <li class="r1 ">
236
+ <li class="r2 ">
207
237
  <span class='object_link'><a href="Ballast/Operation.html#perform_with_handling-instance_method" title="Ballast::Operation#perform_with_handling (method)">#perform_with_handling</a></span>
208
238
  <small>Ballast::Operation</small>
209
239
  </li>
210
240
 
211
241
 
212
- <li class="r2 ">
242
+ <li class="r1 ">
213
243
  <span class='object_link'><a href="Ballast/Concerns/Ajax.html#prepare_ajax-instance_method" title="Ballast::Concerns::Ajax#prepare_ajax (method)">#prepare_ajax</a></span>
214
244
  <small>Ballast::Concerns::Ajax</small>
215
245
  </li>
216
246
 
217
247
 
218
- <li class="r1 ">
248
+ <li class="r2 ">
219
249
  <span class='object_link'><a href="Ballast/Concerns/Ajax.html#prevent_caching-instance_method" title="Ballast::Concerns::Ajax#prevent_caching (method)">#prevent_caching</a></span>
220
250
  <small>Ballast::Concerns::Ajax</small>
221
251
  </li>
222
252
 
223
253
 
254
+ <li class="r1 ">
255
+ <span class='object_link'><a href="Ballast/RequestDomainMatcher.html#replace_block-instance_method" title="Ballast::RequestDomainMatcher#replace_block (method)">#replace_block</a></span>
256
+ <small>Ballast::RequestDomainMatcher</small>
257
+ </li>
258
+
259
+
260
+ <li class="r2 ">
261
+ <span class='object_link'><a href="Ballast/RequestDomainMatcher.html#replace_pattern-instance_method" title="Ballast::RequestDomainMatcher#replace_pattern (method)">#replace_pattern</a></span>
262
+ <small>Ballast::RequestDomainMatcher</small>
263
+ </li>
264
+
265
+
266
+ <li class="r1 ">
267
+ <span class='object_link'><a href="Ballast/RequestDomainMatcher.html#replace_string-instance_method" title="Ballast::RequestDomainMatcher#replace_string (method)">#replace_string</a></span>
268
+ <small>Ballast::RequestDomainMatcher</small>
269
+ </li>
270
+
271
+
224
272
  <li class="r2 ">
225
273
  <span class='object_link'><a href="Ballast/Operation.html#resolve_error-instance_method" title="Ballast::Operation#resolve_error (method)">#resolve_error</a></span>
226
274
  <small>Ballast::Operation</small>
@@ -103,7 +103,7 @@
103
103
  </div>
104
104
 
105
105
  <div id="footer">
106
- Generated on Wed Dec 25 11:35:22 2013 by
106
+ Generated on Wed Dec 25 14:18:00 2013 by
107
107
  <a href="http://yardoc.org" title="Yay! A Ruby Documentation Tool" target="_parent">yard</a>
108
108
  0.8.7.3 (ruby-2.0.0).
109
109
  </div>
@@ -0,0 +1,38 @@
1
+ #
2
+ # This file is part of the ballast gem. Copyright (C) 2013 and above Shogun <shogun@cowtech.it>.
3
+ # Licensed under the MIT license, which can be found at http://www.opensource.org/licenses/mit-license.php.
4
+ #
5
+
6
+ module Ballast
7
+ # A list of useful middlewares for Rack.
8
+ module Middlewares
9
+ # Converts the host of an IP based request to the default host.
10
+ class DefaultHost
11
+ # Creates a new middleware instance.
12
+ #
13
+ # @param app [Object] A Rack application.
14
+ # @param path [String] The path of a YAML file with default hosts definition per environment.
15
+ def initialize(app, path)
16
+ @app = app
17
+ @hosts = YAML.load_file(path)
18
+ end
19
+
20
+ # Executes the middleware.
21
+ #
22
+ # @param env [Hash] A Rack environment.
23
+ def call(env)
24
+ old_host = env["SERVER_NAME"].ensure_string
25
+ new_host = @hosts[ENV["RACK_ENV"]]
26
+
27
+ if old_host =~ /^\d/ && new_host then
28
+ env["ORIG_SERVER_NAME"] = old_host
29
+ env["ORIG_HTTP_HOST"] = env["HTTP_HOST"].dup
30
+ env["SERVER_NAME"] = new_host
31
+ env["HTTP_HOST"].gsub!(old_host, new_host)
32
+ end
33
+
34
+ @app.call(env)
35
+ end
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,42 @@
1
+ #
2
+ # This file is part of the ballast gem. Copyright (C) 2013 and above Shogun <shogun@cowtech.it>.
3
+ # Licensed under the MIT license, which can be found at http://www.opensource.org/licenses/mit-license.php.
4
+ #
5
+
6
+ module Ballast
7
+ # A small class to match requests basing on the domain.
8
+ #
9
+ # @attribute domains
10
+ # @return [Array] The list of domains which mark a positive match.
11
+ # @attribute replace_pattern
12
+ # @return [String|Regexp] A optional pattern to replace in the request host. See `String#gsub`.
13
+ # @attribute replace_string
14
+ # @return [String] A string to use for replacement in the request host. See `String#gsub`.
15
+ # @attribute replace_block
16
+ # @return [Proc] A block to use for replacement in the request host. See `String#gsub`.
17
+ class RequestDomainMatcher
18
+ attr_accessor :domains, :replace_pattern, :replace_string, :replace_block
19
+
20
+ # Creates a new matcher.
21
+ #
22
+ # @param domains [String|Array] The list of domains which mark a positive match.
23
+ # @param replace_pattern [String|Regexp] A optional pattern to replace in the request host. See `String#gsub`.
24
+ # @param replace_string [String] A string to use for replacement in the request host. See `String#gsub`.
25
+ # @param replace_block [Proc] A block to use for replacement in the request host. See `String#gsub`.
26
+ def initialize(domains, replace_pattern = /\.dev$/, replace_string = "", &replace_block)
27
+ @domains = domains.ensure_array
28
+ @replace_pattern = replace_pattern
29
+ @replace_string = replace_string
30
+ @replace_block = replace_block
31
+ end
32
+
33
+ # Matches a request.
34
+ #
35
+ # @param request [ActionDispatch::Request] The request to match.
36
+ # @return [Boolean] `true` if the request matches, `false` otherwise.
37
+ def matches?(request)
38
+ final_host = @replace_block ? request.host.gsub(@replace_pattern, &@replace_block) : request.host.gsub(@replace_pattern, @replace_string)
39
+ @domains.include?(final_host)
40
+ end
41
+ end
42
+ end
@@ -13,7 +13,7 @@ module Ballast
13
13
  MAJOR = 1
14
14
 
15
15
  # The minor version.
16
- MINOR = 0
16
+ MINOR = 1
17
17
 
18
18
  # The patch version.
19
19
  PATCH = 0
data/lib/ballast.rb CHANGED
@@ -18,7 +18,9 @@ require "ballast/errors"
18
18
  require "ballast/context"
19
19
  require "ballast/operation"
20
20
  require "ballast/operations_chain"
21
+ require "ballast/request_domain_matcher"
21
22
  require "ballast/concerns/ajax"
22
23
  require "ballast/concerns/common"
23
24
  require "ballast/concerns/view"
24
- require "ballast/concerns/errors_handling"
25
+ require "ballast/concerns/errors_handling"
26
+ require "ballast/middlewares/default_host"
@@ -6,11 +6,12 @@
6
6
  require "spec_helper"
7
7
 
8
8
  describe Ballast::Errors::BaseError do
9
+ subject { Ballast::Errors::BaseError.new("ERROR") }
10
+
9
11
  describe ".initialize" do
10
12
  it "should propagate the message also as a response" do
11
- reference = Ballast::Errors::BaseError.new("ERROR")
12
- expect(reference.message).to eq("ERROR")
13
- expect(reference.response).to eq("ERROR")
13
+ expect(subject.message).to eq("ERROR")
14
+ expect(subject.response).to eq("ERROR")
14
15
  end
15
16
  end
16
17
  end
@@ -0,0 +1,42 @@
1
+ #
2
+ # This file is part of the ballast gem. Copyright (C) 2013 and above Shogun <shogun@cowtech.it>.
3
+ # Licensed under the MIT license, which can be found at http://www.opensource.org/licenses/mit-license.php.
4
+ #
5
+
6
+ require "spec_helper"
7
+
8
+ describe Ballast::Middlewares::DefaultHost do
9
+ describe "#initialize" do
10
+ it "should save the app and load default hosts" do
11
+ expect(YAML).to receive(:load_file).with("PATH").and_return({"production" => "HOST"})
12
+
13
+ subject = Ballast::Middlewares::DefaultHost.new("APP", "PATH")
14
+ expect(subject.instance_variable_get(:@app)).to eq("APP")
15
+ expect(subject.instance_variable_get(:@hosts)).to eq({"production" => "HOST"})
16
+ end
17
+ end
18
+
19
+ describe "#call" do
20
+ before(:each) do
21
+ @app = Proc.new { |env| env }
22
+ expect(YAML).to receive(:load_file).with("PATH").and_return({"production" => "HOST"})
23
+ ENV["RACK_ENV"] = "production"
24
+ end
25
+
26
+ subject { Ballast::Middlewares::DefaultHost.new(@app, "PATH") }
27
+
28
+ it "should correctly replace the IP if the environment is found" do
29
+ expect(subject.call({"SERVER_NAME" => "10.0.0.1", "HTTP_HOST" => "10.0.0.1"})).to eq({"SERVER_NAME" => "HOST", "HTTP_HOST" => "HOST", "ORIG_SERVER_NAME" => "10.0.0.1", "ORIG_HTTP_HOST" => "10.0.0.1"})
30
+ end
31
+
32
+ it "should not replace the IP if the environment is not found" do
33
+ ENV["RACK_ENV"] = "staging"
34
+ expect(subject.call({"SERVER_NAME" => "10.0.0.1", "HTTP_HOST" => "10.0.0.1"})).to eq({"SERVER_NAME" => "10.0.0.1", "HTTP_HOST" => "10.0.0.1"})
35
+ end
36
+
37
+ it "should not replace a non IP host" do
38
+ expect(subject.call({"SERVER_NAME" => "abc", "HTTP_HOST" => "cde"})).to eq({"SERVER_NAME" => "abc", "HTTP_HOST" => "cde"})
39
+
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,31 @@
1
+ #
2
+ # This file is part of the ballast gem. Copyright (C) 2013 and above Shogun <shogun@cowtech.it>.
3
+ # Licensed under the MIT license, which can be found at http://www.opensource.org/licenses/mit-license.php.
4
+ #
5
+
6
+ require "spec_helper"
7
+
8
+ describe Ballast::RequestDomainMatcher do
9
+ describe ".initialize" do
10
+ it "should save the attributes" do
11
+ block = Proc.new {}
12
+ subject = Ballast::RequestDomainMatcher.new("A", "B", "C", &block)
13
+ expect(subject.domains).to eq(["A"])
14
+ expect(subject.replace_pattern).to eq("B")
15
+ expect(subject.replace_string).to eq("C")
16
+ expect(subject.replace_block).to be(block)
17
+ end
18
+ end
19
+
20
+ describe "#matches?" do
21
+ subject { Ballast::RequestDomainMatcher.new(["A", "ABB", "AC"], "B", "C") { |a| a * 2 } }
22
+
23
+ it "should correctly match a request" do
24
+ expect(subject.matches?(OpenStruct.new(host: "A"))).to be_true
25
+ expect(subject.matches?(OpenStruct.new(host: "AB"))).to be_true
26
+ subject.replace_block = nil
27
+ expect(subject.matches?(OpenStruct.new(host: "AB"))).to be_true
28
+ expect(subject.matches?(OpenStruct.new(host: "DD"))).to be_false
29
+ end
30
+ end
31
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ballast
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Shogun
@@ -150,8 +150,11 @@ files:
150
150
  - doc/Ballast/Errors/InvalidDomain.html
151
151
  - doc/Ballast/Errors/PerformError.html
152
152
  - doc/Ballast/Errors/ValidationError.html
153
+ - doc/Ballast/Middlewares.html
154
+ - doc/Ballast/Middlewares/DefaultHost.html
153
155
  - doc/Ballast/Operation.html
154
156
  - doc/Ballast/OperationsChain.html
157
+ - doc/Ballast/RequestDomainMatcher.html
155
158
  - doc/Ballast/Version.html
156
159
  - doc/_index.html
157
160
  - doc/class_list.html
@@ -174,8 +177,10 @@ files:
174
177
  - lib/ballast/concerns/view.rb
175
178
  - lib/ballast/context.rb
176
179
  - lib/ballast/errors.rb
180
+ - lib/ballast/middlewares/default_host.rb
177
181
  - lib/ballast/operation.rb
178
182
  - lib/ballast/operations_chain.rb
183
+ - lib/ballast/request_domain_matcher.rb
179
184
  - lib/ballast/version.rb
180
185
  - spec/ballast/concerns/ajax_spec.rb
181
186
  - spec/ballast/concerns/common_spec.rb
@@ -183,8 +188,10 @@ files:
183
188
  - spec/ballast/concerns/view_spec.rb
184
189
  - spec/ballast/context_spec.rb
185
190
  - spec/ballast/errors_spec.rb
191
+ - spec/ballast/middlewares/default_host_spec.rb
186
192
  - spec/ballast/operation_spec.rb
187
193
  - spec/ballast/operations_chain_spec.rb
194
+ - spec/ballast/request_domain_matcher_spec.rb
188
195
  - spec/coverage_helper.rb
189
196
  - spec/spec_helper.rb
190
197
  homepage: http://github.com/ShogunPanda/ballast
@@ -218,8 +225,10 @@ test_files:
218
225
  - spec/ballast/concerns/view_spec.rb
219
226
  - spec/ballast/context_spec.rb
220
227
  - spec/ballast/errors_spec.rb
228
+ - spec/ballast/middlewares/default_host_spec.rb
221
229
  - spec/ballast/operation_spec.rb
222
230
  - spec/ballast/operations_chain_spec.rb
231
+ - spec/ballast/request_domain_matcher_spec.rb
223
232
  - spec/coverage_helper.rb
224
233
  - spec/spec_helper.rb
225
234
  has_rdoc: