rack-ketai 0.1.2 → 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc CHANGED
@@ -1,7 +1,7 @@
1
1
  = rack-ketai: A Rack Middleware for Japanese mobile-phones
2
2
 
3
3
  == rack-ketaiとは
4
- 携帯電話向けサイトを構築する際に役立ちそうな機能を提供する(ことが目標の)ミドルウェアです。
4
+ 携帯電話向けサイトを構築する際に役立ちそうな機能を提供する(ことが目標の)Rackミドルウェアです。
5
5
 
6
6
  * 携帯電話キャリア判別
7
7
  * 端末名の取得
@@ -26,7 +26,25 @@ JRuby 1.4.0
26
26
 
27
27
  == 使用例
28
28
 
29
- === 携帯電話の識別
29
+ === Rails 3.0
30
+ config/application.rb
31
+ module Example
32
+ class Application < Rails::Application
33
+
34
+ # 省略
35
+
36
+ config.middleware.use "::Rack::Ketai"
37
+ end
38
+ end
39
+
40
+ app/controllers/example_controller.rb
41
+ class ExampleController < ApplicationController
42
+ def index
43
+ render :text => (request.env['rack.ketai'].mobile? "mobile" ? : "PC")
44
+ end
45
+ end
46
+
47
+ === Sinatra
30
48
  require 'rubygems'
31
49
  require 'sinatra'
32
50
 
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.2
1
+ 0.1.3
@@ -86,8 +86,8 @@ module Rack::Ketai::Carrier
86
86
  def filtering(env, options = { }, &block)
87
87
  env = options[:disable_filter] ? env : filters(options).inject(env) { |env, filter| filter.inbound(env) }
88
88
  ret = block.call(env)
89
- ret[2] = ret[2].body if ret[2].is_a?(Rack::Response)
90
- options[:disable_filter] ? ret : filters(options).reverse.inject(ret) { |r, filter| filter.outbound(*r) }
89
+ ret = options[:disable_filter] ? ret : filters(options).reverse.inject(ret) { |r, filter| filter.outbound(*r) }
90
+ ret
91
91
  end
92
92
 
93
93
  def filters(options = { })
@@ -163,7 +163,6 @@ class Rack::Ketai::Carrier::Abstract
163
163
  end
164
164
 
165
165
  def outbound(status, headers, body)
166
- body = [body].flatten
167
166
  apply_outgoing?(status, headers, body) ? to_external(status, headers, body) : [status, headers, body]
168
167
  end
169
168
 
@@ -173,14 +172,6 @@ class Rack::Ketai::Carrier::Abstract
173
172
  end
174
173
 
175
174
  def to_external(status, headers, body)
176
- if headers['Content-Type']
177
- case headers['Content-Type']
178
- when /charset=[\w\-]+/i
179
- headers['Content-Type'].sub!(/charset=[\w\-]+/, 'charset=utf-8')
180
- else
181
- headers['Content-Type'] << "; charset=utf-8"
182
- end
183
- end
184
175
  [status, headers, body]
185
176
  end
186
177
 
@@ -242,27 +233,24 @@ class Rack::Ketai::Carrier::Abstract
242
233
  end
243
234
 
244
235
  def to_external(status, headers, body)
245
- if body.is_a?(Array)
246
- body = body.collect do |str|
247
- NKF.nkf('-m0 -x -Ws', str)
248
- end
249
- else
250
- body = NKF.nkf('-m0 -x -Ws', body)
236
+ output = ''
237
+
238
+ (body.respond_to?(:each) ? body : [body]).each do |str|
239
+ output << NKF.nkf('-m0 -x -Ws', str)
251
240
  end
252
241
 
253
242
  if headers['Content-Type']
254
243
  case headers['Content-Type']
255
244
  when /charset=[\w\-]+/i
256
- headers['Content-Type'].sub!(/charset=[\w\-]+/, 'charset=shift_jis')
245
+ headers['Content-Type'] = headers['Content-Type'].sub(/charset=[\w\-]+/, 'charset=shift_jis')
257
246
  else
258
- headers['Content-Type'] << "; charset=shift_jis"
247
+ headers['Content-Type'] = headers['Content-Type'] + "; charset=shift_jis"
259
248
  end
260
249
  end
261
250
 
262
- content = (body.is_a?(Array) ? body[0] : body).to_s
263
- headers['Content-Length'] = (content.respond_to?(:bytesize) ? content.bytesize : content.size).to_s if headers.member?('Content-Length')
251
+ headers['Content-Length'] = (output.respond_to?(:bytesize) ? output.bytesize : output.size).to_s if headers.member?('Content-Length')
264
252
 
265
- [status, headers, body]
253
+ [status, headers, [output]]
266
254
  end
267
255
 
268
256
  end
@@ -49,10 +49,10 @@ module Rack
49
49
  def to_external(status, headers, body)
50
50
  status, headers, body = super
51
51
 
52
- return [status, headers, body] unless body[0]
53
-
54
- body = body.collect do |str|
55
- str.gsub(/\[e:([0-9A-F]{3})\]/) do |match|
52
+ output = ''
53
+ (body.respond_to?(:each) ? body : [body]).each do |str|
54
+ next unless str
55
+ output << str.gsub(/\[e:([0-9A-F]{3})\]/) do |match|
56
56
  emojiid = $1.scanf('%X').first
57
57
  sjis = EMOJIID_TO_EMOJI[emojiid]
58
58
  if sjis
@@ -66,10 +66,9 @@ module Rack
66
66
  end
67
67
  end
68
68
 
69
- content = (body.is_a?(Array) ? body[0] : body).to_s
70
- headers['Content-Length'] = (content.respond_to?(:bytesize) ? content.bytesize : content.size).to_s if headers.member?('Content-Length')
69
+ headers['Content-Length'] = (output.respond_to?(:bytesize) ? output.bytesize : output.size).to_s if headers.member?('Content-Length')
71
70
 
72
- [status, headers, body]
71
+ [status, headers, [output]]
73
72
  end
74
73
  end
75
74
 
@@ -48,10 +48,10 @@ module Rack::Ketai::Carrier
48
48
  def to_external(status, headers, body)
49
49
  status, headers, body = super
50
50
 
51
- return [status, headers, body] unless body[0]
52
-
53
- body = body.collect do |str|
54
- str.gsub(/\[e:([0-9A-F]{3})\]/) do |match|
51
+ output = ''
52
+ (body.respond_to?(:each) ? body : [body]).each do |str|
53
+ next unless str
54
+ output << str.gsub(/\[e:([0-9A-F]{3})\]/) do |match|
55
55
  emojiid = $1.scanf('%X').first
56
56
  sjis = EMOJIID_TO_EMOJI[emojiid]
57
57
  if sjis
@@ -65,12 +65,11 @@ module Rack::Ketai::Carrier
65
65
  end
66
66
  end
67
67
 
68
- content = (body.is_a?(Array) ? body[0] : body).to_s
69
- headers['Content-Length'] = (content.respond_to?(:bytesize) ? content.bytesize : content.size).to_s if headers.member?('Content-Length')
68
+ headers['Content-Length'] = (output.respond_to?(:bytesize) ? output.bytesize : output.size).to_s if headers.member?('Content-Length')
70
69
 
71
- headers['Content-Type'].sub! Regexp.new(Regexp.quote('text/html')), 'application/xhtml+xml' if headers['Content-Type']
70
+ headers['Content-Type'] = headers['Content-Type'].sub Regexp.new(Regexp.quote('text/html')), 'application/xhtml+xml' if headers['Content-Type']
72
71
 
73
- [status, headers, body]
72
+ [status, headers, [output]]
74
73
  end
75
74
  end
76
75
 
@@ -19,11 +19,12 @@ module Rack::Ketai::Carrier
19
19
  # ただし、content-type がhtmlでないときおよび、
20
20
  # input内、textarea内では変換しない
21
21
  def to_external(status, headers, body)
22
- return [status, headers, body] unless body[0] && !@options[:emoticons_path].to_s.empty? && (headers['Content-Type'].to_s.empty? || headers['Content-Type'].to_s =~ /html/)
22
+ return [status, headers, body] unless body && !@options[:emoticons_path].to_s.empty? && (headers['Content-Type'].to_s.empty? || headers['Content-Type'].to_s =~ /html/)
23
23
 
24
24
  emoticons_path = @options[:emoticons_path]
25
-
26
- body = body.collect do |str|
25
+
26
+ output = ''
27
+ body.collect do |str|
27
28
  # input内・textarea内以外のものだけを置換する良い方法がわからないので、
28
29
  # とりあえず、input内、textarea内のものを別なのにしとく
29
30
  str = str.gsub(INSIDE_INPUT_TAG) do
@@ -50,13 +51,12 @@ module Rack::Ketai::Carrier
50
51
  end
51
52
 
52
53
  # とりあえず変えておいたものを戻す
53
- str.gsub(/\[E:([0-9A-F]{3})\]/){ "[e:#{$1}]" }
54
+ output << str.gsub(/\[E:([0-9A-F]{3})\]/){ "[e:#{$1}]" }
54
55
  end
55
56
 
56
- content = (body.is_a?(Array) ? body[0] : body).to_s
57
- headers['Content-Length'] = (content.respond_to?(:bytesize) ? content.bytesize : content.size).to_s if headers.member?('Content-Length')
57
+ headers['Content-Length'] = (output.respond_to?(:bytesize) ? output.bytesize : output.size).to_s if headers.member?('Content-Length')
58
58
 
59
- [status, headers, body]
59
+ [status, headers, [output]]
60
60
  end
61
61
 
62
62
  end
@@ -47,10 +47,12 @@ module Rack::Ketai::Carrier
47
47
 
48
48
  def to_external(status, headers, body)
49
49
  status, headers, body = super
50
- return [status, headers, body] unless body[0]
50
+ return [status, headers, body] unless body
51
51
 
52
- body = body.collect do |str|
53
- str.gsub(/\[e:([0-9A-F]{3})\]/) do |match|
52
+ output = ''
53
+ (body.respond_to?(:each) ? body : [body]).each do |str|
54
+ next unless str
55
+ output << str.gsub(/\[e:([0-9A-F]{3})\]/) do |match|
54
56
  emojiid = $1.scanf('%X').first
55
57
  utf8str = EMOJIID_TO_EMOJI[emojiid]
56
58
  if utf8str
@@ -64,10 +66,18 @@ module Rack::Ketai::Carrier
64
66
  end
65
67
  end
66
68
 
67
- content = (body.is_a?(Array) ? body[0] : body).to_s
68
- headers['Content-Length'] = (content.respond_to?(:bytesize) ? content.bytesize : content.size).to_s if headers.member?('Content-Length')
69
+ headers['Content-Length'] = (output.respond_to?(:bytesize) ? output.bytesize : output.size).to_s if headers.member?('Content-Length')
70
+
71
+ if headers['Content-Type']
72
+ case headers['Content-Type']
73
+ when /charset=[\w\-]+/i
74
+ headers['Content-Type'] = headers['Content-Type'].sub(/charset=[\w\-]+/, 'charset=utf-8')
75
+ else
76
+ headers['Content-Type'] = headers['Content-Type'] + "; charset=utf-8"
77
+ end
78
+ end
69
79
 
70
- [status, headers, body]
80
+ [status, headers, [output]]
71
81
  end
72
82
  end
73
83