sinatra-default_charset 0.1.0 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/CHANGELOG.rdoc +8 -0
- data/Manifest.txt +1 -1
- data/README.rdoc +29 -3
- data/Rakefile +1 -0
- data/lib/sinatra/default_charset.rb +29 -3
- data/test/test_sinatra/test_default_charset.rb +77 -0
- metadata +23 -9
- data/test/test_sinatra_default_charset.rb +0 -15
data/CHANGELOG.rdoc
CHANGED
data/Manifest.txt
CHANGED
data/README.rdoc
CHANGED
@@ -9,21 +9,47 @@
|
|
9
9
|
|
10
10
|
== DESCRIPTION:
|
11
11
|
|
12
|
-
Sets a default charset in the MIME headers. Defaults to
|
12
|
+
Sets a default charset in the MIME headers. Defaults to
|
13
|
+
Encoding.default_external on ruby 1.9, and to $kcode on ruby 1.8, or falls
|
14
|
+
back to utf-8 when neither are available.
|
15
|
+
|
16
|
+
Provides a setting called +default_charset+ that allows overriding the
|
17
|
+
auto-discovered default.
|
18
|
+
|
19
|
+
To remove the transmitted charset for a single request, simply pass +:charset
|
20
|
+
=> nil+ as your parameter to the +content_type+ helper.
|
13
21
|
|
14
22
|
== FEATURES/PROBLEMS:
|
15
23
|
|
16
|
-
*
|
24
|
+
* Applies a default charset field to the Content-Type header of all responses
|
25
|
+
* Allows for removal of the default-applied field
|
26
|
+
* Allows for per-request overrides of the value
|
17
27
|
|
18
28
|
== SYNOPSIS:
|
19
29
|
|
20
30
|
require 'sinatra/default_charset'
|
21
31
|
register Sinatra::DefaultCharset
|
22
32
|
set :default_charset, 'utf-8'
|
33
|
+
|
34
|
+
get '/' do
|
35
|
+
"I'm utf-8"
|
36
|
+
end
|
37
|
+
|
38
|
+
get '/shift_jis' do
|
39
|
+
content_type :html, :charset => 'shift_jis'
|
40
|
+
"I'm shift_jis"
|
41
|
+
end
|
42
|
+
|
43
|
+
get '/none' do
|
44
|
+
content_type :html, :charset => nil
|
45
|
+
"I have no charset in my headers"
|
46
|
+
end
|
23
47
|
|
24
48
|
== REQUIREMENTS:
|
25
49
|
|
26
|
-
* sinatra
|
50
|
+
* sinatra for runtime.
|
51
|
+
|
52
|
+
* rack-test and minitest for testing.
|
27
53
|
|
28
54
|
== INSTALL:
|
29
55
|
|
data/Rakefile
CHANGED
@@ -8,6 +8,7 @@ Hoe.spec 'sinatra-default_charset' do
|
|
8
8
|
extra_dev_deps << %w(hoe-doofus >=1.0.0)
|
9
9
|
extra_dev_deps << %w(hoe-git >=1.3.0)
|
10
10
|
extra_dev_deps << %w(hoe-gemcutter >=1.0.0)
|
11
|
+
extra_dev_deps << %w(rack-test >=0.5.3)
|
11
12
|
extra_deps << %w(sinatra >=1.0)
|
12
13
|
self.extra_rdoc_files = FileList["**/*.rdoc"]
|
13
14
|
self.history_file = "CHANGELOG.rdoc"
|
@@ -2,15 +2,41 @@ require 'sinatra/base'
|
|
2
2
|
|
3
3
|
module Sinatra
|
4
4
|
module DefaultCharset
|
5
|
-
VERSION = '0.
|
5
|
+
VERSION = '0.2.0'
|
6
6
|
|
7
|
+
DEFAULT_FALLBACK = 'utf-8'
|
8
|
+
|
9
|
+
charset = if defined?(Encoding) && Encoding.respond_to?(:default_external)
|
10
|
+
Encoding.default_external.to_s.downcase
|
11
|
+
else
|
12
|
+
case $kcode
|
13
|
+
when /n/i
|
14
|
+
DEFAULT_FALLBACK
|
15
|
+
when /e/i
|
16
|
+
'euc-jp'
|
17
|
+
when /s/i
|
18
|
+
'shift_jis'
|
19
|
+
when /u/i
|
20
|
+
'utf-8'
|
21
|
+
else
|
22
|
+
DEFAULT_FALLBACK
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
DEFAULT_CHARSET = charset
|
27
|
+
|
28
|
+
# Defaults to
|
7
29
|
def content_type(type, params = {})
|
8
|
-
params
|
30
|
+
if params.include?(:charset)
|
31
|
+
params.delete(:charset) if params[:charset].nil?
|
32
|
+
else
|
33
|
+
params[:charset] = settings.default_charset
|
34
|
+
end
|
9
35
|
super
|
10
36
|
end
|
11
37
|
|
12
38
|
def self.registered(app)
|
13
|
-
app.set :default_charset,
|
39
|
+
app.set :default_charset, DEFAULT_CHARSET
|
14
40
|
app.helpers self
|
15
41
|
# re-default it so that we've always got a charset set
|
16
42
|
app.before { content_type :html }
|
@@ -0,0 +1,77 @@
|
|
1
|
+
require "sinatra/default_charset"
|
2
|
+
require 'minitest/unit'
|
3
|
+
require 'rack/test'
|
4
|
+
|
5
|
+
class TestSinatra; end
|
6
|
+
class TestSinatra::TestDefaultCharset < MiniTest::Unit::TestCase
|
7
|
+
class TestApp < Sinatra::Base
|
8
|
+
register Sinatra::DefaultCharset
|
9
|
+
|
10
|
+
get '/' do
|
11
|
+
"hello world\n"
|
12
|
+
end
|
13
|
+
|
14
|
+
get '/thing.xml' do
|
15
|
+
content_type :xml
|
16
|
+
'thing'
|
17
|
+
end
|
18
|
+
|
19
|
+
get '/custom.xml' do
|
20
|
+
content_type :xml, :charset => 'iso-8859-1'
|
21
|
+
'custom'
|
22
|
+
end
|
23
|
+
|
24
|
+
get '/none' do
|
25
|
+
content_type :html, :charset => nil
|
26
|
+
'none'
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
include Rack::Test::Methods
|
31
|
+
|
32
|
+
attr_reader :app
|
33
|
+
|
34
|
+
def test_defaults
|
35
|
+
# This test assumes you haven't set $kcode or Encoding.default_external to
|
36
|
+
# non-default values.
|
37
|
+
@app = TestApp.new
|
38
|
+
get '/'
|
39
|
+
assert last_response.ok?
|
40
|
+
assert_match 'charset=utf-8', last_response['Content-Type']
|
41
|
+
assert_equal "hello world\n", last_response.body
|
42
|
+
end
|
43
|
+
|
44
|
+
def test_custom_content_type
|
45
|
+
@app = TestApp.new
|
46
|
+
get '/thing.xml'
|
47
|
+
assert_match 'charset=utf-8', last_response['Content-Type']
|
48
|
+
assert_match 'application/xml', last_response['Content-Type']
|
49
|
+
assert_equal 'thing', last_response.body
|
50
|
+
end
|
51
|
+
|
52
|
+
def test_custom_content_type_and_charset
|
53
|
+
@app = TestApp.new
|
54
|
+
get '/custom.xml'
|
55
|
+
assert_match 'charset=iso-8859-1', last_response['Content-Type']
|
56
|
+
assert_match 'application/xml', last_response['Content-Type']
|
57
|
+
assert_equal 'custom', last_response.body
|
58
|
+
end
|
59
|
+
|
60
|
+
def test_no_charset
|
61
|
+
@app = TestApp.new
|
62
|
+
get '/none'
|
63
|
+
refute_match 'charset=', last_response['Content-Type']
|
64
|
+
assert_match 'text/html', last_response['Content-Type']
|
65
|
+
assert_equal 'none', last_response.body
|
66
|
+
end
|
67
|
+
|
68
|
+
class Windows < TestApp
|
69
|
+
set :default_charset, 'windows-1252'
|
70
|
+
end
|
71
|
+
|
72
|
+
def test_set_default_charset
|
73
|
+
@app = Windows.new
|
74
|
+
get '/'
|
75
|
+
assert_match 'charset=windows-1252', last_response['Content-Type']
|
76
|
+
end
|
77
|
+
end
|
metadata
CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
|
|
4
4
|
prerelease: false
|
5
5
|
segments:
|
6
6
|
- 0
|
7
|
-
-
|
7
|
+
- 2
|
8
8
|
- 0
|
9
|
-
version: 0.
|
9
|
+
version: 0.2.0
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- James Tucker
|
@@ -14,7 +14,7 @@ autorequire:
|
|
14
14
|
bindir: bin
|
15
15
|
cert_chain: []
|
16
16
|
|
17
|
-
date: 2010-05-
|
17
|
+
date: 2010-05-25 00:00:00 -03:00
|
18
18
|
default_executable:
|
19
19
|
dependencies:
|
20
20
|
- !ruby/object:Gem::Dependency
|
@@ -87,9 +87,23 @@ dependencies:
|
|
87
87
|
type: :development
|
88
88
|
version_requirements: *id005
|
89
89
|
- !ruby/object:Gem::Dependency
|
90
|
-
name:
|
90
|
+
name: rack-test
|
91
91
|
prerelease: false
|
92
92
|
requirement: &id006 !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - ">="
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
segments:
|
97
|
+
- 0
|
98
|
+
- 5
|
99
|
+
- 3
|
100
|
+
version: 0.5.3
|
101
|
+
type: :development
|
102
|
+
version_requirements: *id006
|
103
|
+
- !ruby/object:Gem::Dependency
|
104
|
+
name: hoe
|
105
|
+
prerelease: false
|
106
|
+
requirement: &id007 !ruby/object:Gem::Requirement
|
93
107
|
requirements:
|
94
108
|
- - ">="
|
95
109
|
- !ruby/object:Gem::Version
|
@@ -99,8 +113,8 @@ dependencies:
|
|
99
113
|
- 0
|
100
114
|
version: 2.6.0
|
101
115
|
type: :development
|
102
|
-
version_requirements: *
|
103
|
-
description:
|
116
|
+
version_requirements: *id007
|
117
|
+
description: helper.
|
104
118
|
email:
|
105
119
|
- raggi@rubyforge.org
|
106
120
|
executables: []
|
@@ -118,7 +132,7 @@ files:
|
|
118
132
|
- README.rdoc
|
119
133
|
- Rakefile
|
120
134
|
- lib/sinatra/default_charset.rb
|
121
|
-
- test/
|
135
|
+
- test/test_sinatra/test_default_charset.rb
|
122
136
|
has_rdoc: true
|
123
137
|
homepage: http://rubygems.org/gems/sinatra-default_charset
|
124
138
|
licenses: []
|
@@ -149,6 +163,6 @@ rubyforge_project: libraggi
|
|
149
163
|
rubygems_version: 1.3.6
|
150
164
|
signing_key:
|
151
165
|
specification_version: 3
|
152
|
-
summary:
|
166
|
+
summary: helper.
|
153
167
|
test_files:
|
154
|
-
- test/
|
168
|
+
- test/test_sinatra/test_default_charset.rb
|
@@ -1,15 +0,0 @@
|
|
1
|
-
require "sinatra/default_charset"
|
2
|
-
|
3
|
-
class TestSinatraDefaultCharset < MiniTest::Unit::TestCase
|
4
|
-
class TestApp < Sinatra::Base
|
5
|
-
register Sinatra::DefaultCharset
|
6
|
-
|
7
|
-
get '/' do
|
8
|
-
"hello world\n"
|
9
|
-
end
|
10
|
-
end
|
11
|
-
|
12
|
-
def test_sanity
|
13
|
-
flunk "write tests or I will kneecap you"
|
14
|
-
end
|
15
|
-
end
|