rack-ketai 0.1.3 → 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/README.rdoc CHANGED
@@ -40,7 +40,7 @@ config/application.rb
40
40
  app/controllers/example_controller.rb
41
41
  class ExampleController < ApplicationController
42
42
  def index
43
- render :text => (request.env['rack.ketai'].mobile? "mobile" ? : "PC")
43
+ render :text => (request.env['rack.ketai'].mobile? ? "mobile" : "PC")
44
44
  end
45
45
  end
46
46
 
@@ -152,6 +152,25 @@ http://start.typepad.jp/typecast/
152
152
  use Rack::Ketai, :emoticons_path => '/images/emoticons'
153
153
  run MyApp.new
154
154
 
155
+ === スマートフォンへの対応
156
+ 現在のところ、UAによる判別のみ可能です。また、iPhone/iPod touch及びAndroid端末のみを想定しています。私はXperiaしか所持していないのでその他については動くかどうか不明です。
157
+
158
+ 次のようにして判別します。
159
+ if env['rack.ketai'].smartphone?
160
+ # スマートフォン
161
+ else
162
+ # 非スマートフォン
163
+ end
164
+ または
165
+ case env['rack.ketai']
166
+ when Rack::Ketai::Carrier::IPhone
167
+ # iPhone
168
+ when Rack::Ketai::Carrier::Android
169
+ # Android
170
+ end
171
+
172
+ 端末名の取得なども未実装です。また、#valid_addr?は常に偽です。
173
+
155
174
  === 作者
156
175
 
157
176
  Copyright 2009-2010 (c) Yuichi Takeuchi, under MIT License
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.3
1
+ 0.2.0
@@ -5,6 +5,7 @@ require 'nkf'
5
5
  require 'rack/request'
6
6
  require 'stringio'
7
7
  require 'ipaddr'
8
+ require 'rack/ketai/filter'
8
9
 
9
10
  unless IPAddr.instance_methods.include?("to_range")
10
11
  class IPAddr
@@ -101,6 +102,11 @@ module Rack::Ketai::Carrier
101
102
  false
102
103
  end
103
104
 
105
+ # スマートフォンか
106
+ def smartphone?
107
+ false
108
+ end
109
+
104
110
  # サブスクライバID
105
111
  # 契約者毎にユニーク
106
112
  def subscriberid
@@ -150,112 +156,3 @@ module Rack::Ketai::Carrier
150
156
  end
151
157
  end
152
158
  end
153
-
154
- class Rack::Ketai::Carrier::Abstract
155
- class Filter
156
-
157
- def initialize(options = { })
158
- @options = options.clone
159
- end
160
-
161
- def inbound(env)
162
- apply_incoming?(env) ? to_internal(env) : env
163
- end
164
-
165
- def outbound(status, headers, body)
166
- apply_outgoing?(status, headers, body) ? to_external(status, headers, body) : [status, headers, body]
167
- end
168
-
169
- private
170
- def to_internal(env)
171
- env
172
- end
173
-
174
- def to_external(status, headers, body)
175
- [status, headers, body]
176
- end
177
-
178
- def full_apply(*argv, &proc)
179
- argv.each do |obj|
180
- deep_apply(obj, &proc)
181
- end
182
- end
183
-
184
- def deep_apply(obj, &proc)
185
- case obj
186
- when Hash
187
- obj.each_pair do |key, value|
188
- obj[key] = deep_apply(value, &proc)
189
- end
190
- obj
191
- when Array
192
- obj.collect!{ |value| deep_apply(value, &proc)}
193
- when NilClass, TrueClass, FalseClass, Tempfile, StringIO
194
- obj
195
- else
196
- proc.call(obj)
197
- end
198
- end
199
-
200
- def apply_incoming?(env); true; end
201
- def apply_outgoing?(status, headers, body)
202
- headers['Content-Type'] !~ /^(.+?)(?:;|$)/
203
- [nil, "text/html", "application/xhtml+xml"].include?($1)
204
- end
205
-
206
- end
207
-
208
-
209
- class SjisFilter < Filter
210
-
211
- private
212
- def to_internal(env)
213
- request = Rack::Request.new(env)
214
-
215
- # 最低でも1回呼んでないと query_string, form_hash等が未設定
216
- request.params
217
-
218
- # 同一オブジェクトが両方に入ってたりして二重にかかることがあるので
219
- converted_objects = []
220
- converter = lambda { |value|
221
- unless converted_objects.include?(value)
222
- value = NKF.nkf('-m0 -x -Sw', value)
223
- converted_objects << value
224
- end
225
- value
226
- }
227
-
228
- full_apply(request.env["rack.request.query_hash"],
229
- request.env["rack.request.form_hash"],
230
- &converter)
231
-
232
- request.env
233
- end
234
-
235
- def to_external(status, headers, body)
236
- output = ''
237
-
238
- (body.respond_to?(:each) ? body : [body]).each do |str|
239
- output << NKF.nkf('-m0 -x -Ws', str)
240
- end
241
-
242
- if headers['Content-Type']
243
- case headers['Content-Type']
244
- when /charset=[\w\-]+/i
245
- headers['Content-Type'] = headers['Content-Type'].sub(/charset=[\w\-]+/, 'charset=shift_jis')
246
- else
247
- headers['Content-Type'] = headers['Content-Type'] + "; charset=shift_jis"
248
- end
249
- end
250
-
251
- headers['Content-Length'] = (output.respond_to?(:bytesize) ? output.bytesize : output.size).to_s if headers.member?('Content-Length')
252
-
253
- [status, headers, [output]]
254
- end
255
-
256
- end
257
-
258
- end
259
-
260
- require 'rack/ketai/carrier/emoji/emojidata'
261
-
@@ -0,0 +1,15 @@
1
+ # -*- coding: utf-8 -*-
2
+
3
+ require 'rack/ketai/carrier/smartphone'
4
+
5
+ module Rack::Ketai::Carrier
6
+ class Android < Smartphone
7
+
8
+ USER_AGENT_REGEXP = /Android/
9
+
10
+ def supports_cookie?
11
+ true
12
+ end
13
+
14
+ end
15
+ end
@@ -1,16 +1,18 @@
1
1
  # -*- coding: utf-8 -*-
2
2
  require 'scanf'
3
3
 
4
+ require 'rack/ketai/carrier/mobile'
5
+
4
6
  module Rack
5
7
  module Ketai
6
8
  module Carrier
7
- class Au < Abstract
9
+ class Au < Mobile
8
10
  autoload :CIDRS, 'rack/ketai/carrier/cidrs/au'
9
11
  autoload :SPECS, 'rack/ketai/carrier/specs/au'
10
12
 
11
13
  USER_AGENT_REGEXP = /^(?:KDDI|UP.Browser\/.+?)-(.+?) /
12
14
 
13
- class Filter < ::Rack::Ketai::Carrier::Abstract::SjisFilter
15
+ class Filter < ::Rack::Ketai::SjisFilter
14
16
 
15
17
  # 絵文字コード -> 絵文字ID 対応表から、絵文字コード検出用の正規表現をつくる
16
18
  # 複数の絵文字の組み合わせのものを前におくことで
@@ -78,10 +80,6 @@ module Rack
78
80
  end
79
81
  end
80
82
 
81
- def mobile?
82
- true
83
- end
84
-
85
83
  def subscriberid
86
84
  ezno
87
85
  end
@@ -1,15 +1,16 @@
1
1
  # -*- coding: utf-8 -*-
2
2
 
3
3
  require 'scanf'
4
+ require 'rack/ketai/carrier/mobile'
4
5
 
5
6
  module Rack::Ketai::Carrier
6
- class Docomo < Abstract
7
+ class Docomo < Mobile
7
8
  autoload :CIDRS, 'rack/ketai/carrier/cidrs/docomo'
8
9
  autoload :SPECS, 'rack/ketai/carrier/specs/docomo'
9
10
 
10
11
  USER_AGENT_REGEXP = /^DoCoMo/
11
12
 
12
- class Filter < ::Rack::Ketai::Carrier::Abstract::SjisFilter
13
+ class Filter < ::Rack::Ketai::SjisFilter
13
14
 
14
15
  # 絵文字コード -> 絵文字ID 対応表から、絵文字コード検出用の正規表現をつくる
15
16
  # 複数の絵文字の組み合わせのものを前におくことで
@@ -79,10 +80,6 @@ module Rack::Ketai::Carrier
79
80
  end
80
81
  end
81
82
 
82
- def mobile?
83
- true
84
- end
85
-
86
83
  # 端末個体識別子があれば返す。
87
84
  def deviceid
88
85
  case @env['HTTP_USER_AGENT']