one-extension 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (2) hide show
  1. data/lib/one-extension.rb +29 -3
  2. metadata +1 -1
data/lib/one-extension.rb CHANGED
@@ -1,12 +1,38 @@
1
1
  module OneExtension
2
+ module ClassMethods
3
+ attr_accessor :one_extension_type
4
+ def one_extension(symbol)
5
+ expecting = [:inclusion, :exclusion]
6
+ unless expecting.include? symbol
7
+ raise PathError, "expecting :inclusion or :exclusion: got `:#{symbol}`"
8
+ end
9
+ self.one_extension_type = symbol
10
+ end
11
+ end
12
+
2
13
  def self.included(base)
3
14
  base.send(:before_filter, :one_ext_check)
15
+ base.extend(ClassMethods)
4
16
  end
17
+
5
18
  def one_ext_check
6
- if request.env["REQUEST_METHOD"] == "GET" &&
7
- request.filtered_parameters["format"] == 'html'
8
- if request.env["PATH_INFO"][-5..-1] == ".html"
19
+ # only run on get request for format.html
20
+ is_get_request = request.env["REQUEST_METHOD"] == "GET"
21
+ is_html_request = [nil, 'html'].include? request.filtered_parameters["format"]
22
+ if is_get_request && is_html_request
23
+ # check if the .html extension is set
24
+ has_extension = request.env["PATH_INFO"][-5..-1] == ".html"
25
+ # get the one_extension_type
26
+ type = self.class.one_extension_type || :exclusion
27
+ if type == :exclusion && has_extension
28
+ # strip the .html
9
29
  url = request.env["PATH_INFO"][0...-5]
30
+ elsif type == :inclusion && !has_extension
31
+ # add the .html
32
+ url = "#{request.env["PATH_INFO"]}.html"
33
+ end
34
+ if url.present?
35
+ # put the query string back into the URL if present
10
36
  if request.env["QUERY_STRING"].length > 0
11
37
  url = "#{url}?#{request.env["QUERY_STRING"]}"
12
38
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: one-extension
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors: