jpmobile 0.1.5 → 0.1.6

Sign up to get free protection for your applications and to get access to all the features.
Files changed (23) hide show
  1. data/VERSION.yml +1 -1
  2. data/lib/jpmobile/rack/params_filter.rb +1 -1
  3. data/test/rails/rails_root/app/models/mobile_mailer.rb +18 -0
  4. data/test/rails/rails_root/config/initializers/secret_token.rb +1 -1
  5. data/test/rails/rails_root/spec/models/mobile_mailer_spec.rb +744 -0
  6. data/test/rails/rails_root/spec/spec_helper.rb +6 -0
  7. data/test/rails/rails_root/vendor/plugins/jpmobile/lib/jpmobile.rb +2 -0
  8. data/test/rails/rails_root/vendor/plugins/jpmobile/lib/jpmobile/email.rb +17 -2
  9. data/test/rails/rails_root/vendor/plugins/jpmobile/lib/jpmobile/emoticon.rb +85 -23
  10. data/test/rails/rails_root/vendor/plugins/jpmobile/lib/jpmobile/emoticon/au.rb +644 -0
  11. data/test/rails/rails_root/vendor/plugins/jpmobile/lib/jpmobile/emoticon/softbank.rb +488 -0
  12. data/test/rails/rails_root/vendor/plugins/jpmobile/lib/jpmobile/emoticon/z_combine.rb +16 -13
  13. data/test/rails/rails_root/vendor/plugins/jpmobile/lib/jpmobile/hook_action_mailer.rb +22 -0
  14. data/test/rails/rails_root/vendor/plugins/jpmobile/lib/jpmobile/mail.rb +265 -0
  15. data/test/rails/rails_root/vendor/plugins/jpmobile/lib/jpmobile/mobile/abstract_mobile.rb +35 -0
  16. data/test/rails/rails_root/vendor/plugins/jpmobile/lib/jpmobile/mobile/au.rb +28 -2
  17. data/test/rails/rails_root/vendor/plugins/jpmobile/lib/jpmobile/mobile/docomo.rb +24 -1
  18. data/test/rails/rails_root/vendor/plugins/jpmobile/lib/jpmobile/mobile/emobile.rb +1 -1
  19. data/test/rails/rails_root/vendor/plugins/jpmobile/lib/jpmobile/mobile/softbank.rb +34 -1
  20. data/test/rails/rails_root/vendor/plugins/jpmobile/lib/jpmobile/mobile/vodafone.rb +1 -1
  21. data/test/rails/rails_root/vendor/plugins/jpmobile/lib/jpmobile/mobile/willcom.rb +1 -1
  22. data/test/rails/rails_root/vendor/plugins/jpmobile/lib/jpmobile/util.rb +214 -13
  23. metadata +11 -3
@@ -6,7 +6,7 @@ module Jpmobile::Mobile
6
6
  class Emobile < AbstractMobile
7
7
  USER_AGENT_REGEXP = %r{^emobile/|^Mozilla/5.0 \(H11T; like Gecko; OpenBrowser\) NetFront/3.4$|^Mozilla/4.0 \(compatible; MSIE 6.0; Windows CE; IEMobile 7.7\) S11HT$}
8
8
  # 対応するメールアドレスの正規表現
9
- MAIL_ADDRESS_REGEXP = /^.+@emnet\.ne\.jp$/
9
+ MAIL_ADDRESS_REGEXP = /.+@emnet\.ne\.jp/
10
10
  # EMnet対応端末から通知されるユニークなユーザIDを取得する。
11
11
  def em_uid
12
12
  @request.env['HTTP_X_EM_UID']
@@ -8,7 +8,7 @@ module Jpmobile::Mobile
8
8
  # 対応するuser-agentの正規表現
9
9
  USER_AGENT_REGEXP = /^(?:SoftBank|Semulator)/
10
10
  # 対応するメールアドレスの正規表現 ディズニーモバイル対応
11
- MAIL_ADDRESS_REGEXP = /^.+@(?:softbank\.ne\.jp|disney\.ne\.jp)$/
11
+ MAIL_ADDRESS_REGEXP = /.+@(?:softbank\.ne\.jp|disney\.ne\.jp)/
12
12
 
13
13
  # 製造番号を返す。無ければ +nil+ を返す。
14
14
  def serial_number
@@ -58,5 +58,38 @@ module Jpmobile::Mobile
58
58
 
59
59
  [str, charset]
60
60
  end
61
+
62
+ # メール送信用
63
+ def to_mail_subject(str)
64
+ "=?#{mail_charset}?B?" + [to_mail_encoding(str)].pack('m').strip + "?="
65
+ end
66
+ def to_mail_body(str)
67
+ to_mail_encoding(str)
68
+ end
69
+ def mail_charset
70
+ "Shift_JIS"
71
+ end
72
+ def to_mail_internal(str, val)
73
+ # 絵文字を数値参照に変換
74
+ if Jpmobile::Util.shift_jis?(str) or Jpmobile::Util.ascii_8bit?(str)
75
+ # Shift_JIS
76
+ str = Jpmobile::Emoticon.external_to_unicodecr_softbank_sjis(Jpmobile::Util.sjis(str))
77
+ elsif Jpmobile::Util.utf8?(str)
78
+ # UTF-8
79
+ str = Jpmobile::Emoticon.external_to_unicodecr_softbank(Jpmobile::Util.utf8(str))
80
+ end
81
+
82
+ str
83
+ end
84
+ def to_mail_body_encoded?(str)
85
+ Jpmobile::Util.shift_jis?(str)
86
+ end
87
+
88
+ private
89
+ def to_mail_encoding(str)
90
+ str = Jpmobile::Emoticon.utf8_to_unicodecr(str)
91
+ str = Jpmobile::Util.utf8_to_sjis(str)
92
+ Jpmobile::Emoticon.unicodecr_to_softbank_email(str)
93
+ end
61
94
  end
62
95
  end
@@ -7,7 +7,7 @@ module Jpmobile::Mobile
7
7
  # 対応するUser-Agentの正規表現
8
8
  USER_AGENT_REGEXP = /^(Vodafone|Vemulator)/
9
9
  # 対応するメールアドレスの正規表現
10
- MAIL_ADDRESS_REGEXP = /^.+@[dhtcrknsq]\.vodafone\.ne\.jp$/
10
+ MAIL_ADDRESS_REGEXP = /.+@[dhtcrknsq]\.vodafone\.ne\.jp/
11
11
 
12
12
  # cookieに対応しているか?
13
13
  def supports_cookie?
@@ -7,7 +7,7 @@ module Jpmobile::Mobile
7
7
  # 対応するUser-Agentの正規表現
8
8
  USER_AGENT_REGEXP = /^Mozilla\/3.0\(WILLCOM/
9
9
  # 対応するメールアドレスの正規表現
10
- MAIL_ADDRESS_REGEXP = /^.+@(.+\.)?pdx\.ne\.jp$/
10
+ MAIL_ADDRESS_REGEXP = /.+@((.+\.)?pdx\.ne\.jp|willcom\.com)/
11
11
 
12
12
  # 位置情報があれば Position のインスタンスを返す。無ければ +nil+ を返す。
13
13
  def position
@@ -1,6 +1,16 @@
1
+ # -*- coding: utf-8 -*-
1
2
  require 'tempfile'
2
3
  module Jpmobile
3
4
  module Util
5
+ # SJIS = "Shift_JIS"
6
+ SJIS = "Windows-31J"
7
+ UTF8 = "UTF-8"
8
+ JIS = "ISO-2022-JP"
9
+ BINARY = "ASCII-8BIT"
10
+
11
+ WAVE_DASH = [0x301c].pack("U")
12
+ FULLWIDTH_TILDE = [0xff5e].pack("U")
13
+
4
14
  module_function
5
15
  def deep_apply(obj, &proc)
6
16
  case obj
@@ -10,11 +20,12 @@ module Jpmobile
10
20
  end
11
21
  when Array
12
22
  obj.collect!{|value| deep_apply(value, &proc)}
13
- when NilClass, TrueClass, FalseClass, Tempfile, StringIO
14
- return obj
15
- else
23
+ when String
16
24
  obj = obj.to_param if obj.respond_to?(:to_param)
17
25
  proc.call(obj)
26
+ else
27
+ # NilClass, TrueClass, FalseClass, Tempfile, StringIO, etc...
28
+ return obj
18
29
  end
19
30
  end
20
31
 
@@ -32,11 +43,12 @@ module Jpmobile
32
43
  end
33
44
  when Symbol
34
45
  new_obj = proc.call(obj.to_s).to_sym
35
- when NilClass, TrueClass, FalseClass, Tempfile, StringIO
36
- new_obj = obj
37
- else
46
+ when String
38
47
  obj = obj.to_param if obj.respond_to?(:to_param)
39
48
  new_obj = proc.call(obj)
49
+ else
50
+ # NilClass, TrueClass, FalseClass, Tempfile, StringIO, etc...
51
+ new_obj = obj
40
52
  end
41
53
 
42
54
  new_obj
@@ -44,38 +56,67 @@ module Jpmobile
44
56
 
45
57
  def sjis(ascii_8bit)
46
58
  if ascii_8bit.respond_to?(:force_encoding)
47
- ascii_8bit.force_encoding("Shift_JIS")
59
+ ascii_8bit.force_encoding(SJIS)
48
60
  end
49
61
  ascii_8bit
50
62
  end
51
63
 
52
64
  def utf8(ascii_8bit)
53
65
  if ascii_8bit.respond_to?(:force_encoding)
54
- ascii_8bit.force_encoding("utf-8")
66
+ ascii_8bit.force_encoding(UTF8)
67
+ end
68
+ ascii_8bit
69
+ end
70
+
71
+ def jis(ascii_8bit)
72
+ if ascii_8bit.respond_to?(:force_encoding)
73
+ ascii_8bit.force_encoding(JIS)
55
74
  end
56
75
  ascii_8bit
57
76
  end
58
77
 
59
78
  def ascii_8bit(str)
60
79
  if str.respond_to?(:force_encoding)
61
- str.force_encoding("ASCII-8BIT")
80
+ str.force_encoding(BINARY)
62
81
  end
63
82
  str
64
83
  end
65
84
 
66
85
  def utf8_to_sjis(utf8_str)
86
+ # 波ダッシュ対策
87
+ utf8_str = wavedash_to_fullwidth_tilde(utf8_str)
88
+
67
89
  if utf8_str.respond_to?(:encode)
68
- utf8_str.encode("Shift_JIS", :crlf_newline => true)
90
+ utf8_str.encode(SJIS, :crlf_newline => true)
69
91
  else
70
92
  NKF.nkf("-m0 -x -W --oc=cp932", utf8_str).gsub(/\n/, "\r\n")
71
93
  end
72
94
  end
73
95
 
74
96
  def sjis_to_utf8(sjis_str)
75
- if sjis_str.respond_to?(:encode)
76
- sjis_str.encode("UTF-8", :universal_newline => true)
97
+ utf8_str = if sjis_str.respond_to?(:encode)
98
+ sjis_str.encode("UTF-8", :universal_newline => true)
99
+ else
100
+ NKF.nkf("-m0 -x -w --ic=cp932", sjis_str).gsub(/\r\n/, "\n")
101
+ end
102
+
103
+ # 波ダッシュ対策
104
+ fullwidth_tilde_to_wavedash(utf8_str)
105
+ end
106
+
107
+ def utf8_to_jis(utf8_str)
108
+ if utf8_str.respond_to?(:encode)
109
+ utf8_str.encode(JIS, :crlf_newline => true)
77
110
  else
78
- NKF.nkf("-m0 -x -w --ic=cp932", sjis_str).gsub(/\r\n/, "\n")
111
+ NKF.nkf("-m0 -x -Wj", utf8_str).gsub(/\n/, "\r\n")
112
+ end
113
+ end
114
+
115
+ def jis_to_utf8(jis_str)
116
+ if jis_str.respond_to?(:encode)
117
+ jis_str.encode(UTF8, :universal_newline => true)
118
+ else
119
+ NKF.nkf("-m0 -x -Jw", jis_str).gsub(/\r\n/, "\n")
79
120
  end
80
121
  end
81
122
 
@@ -87,11 +128,171 @@ module Jpmobile
87
128
  end
88
129
  end
89
130
 
131
+ def regexp_to_sjis(sjis_str)
132
+ if Object.const_defined?(:Encoding)
133
+ Regexp.compile(Regexp.escape(sjis(sjis_str)))
134
+ else
135
+ Regexp.compile(Regexp.escape(sjis_str,"s"),nil,'s')
136
+ end
137
+ end
138
+
90
139
  def hash_to_utf8(hash)
91
140
  new_hash = {}
92
141
  hash.each do |keu, value|
93
142
  new_hash[utf8(key)] = utf8(value)
94
143
  end
95
144
  end
145
+
146
+ def sjis_regexp(sjis)
147
+ sjis_str = sjis.kind_of?(Numeric) ? [sjis].pack('n') : sjis
148
+
149
+ if Object.const_defined?(:Encoding)
150
+ Regexp.compile(Regexp.escape(sjis_str.force_encoding(SJIS)))
151
+ else
152
+ Regexp.compile(Regexp.escape(sjis_str,"s"),nil,'s')
153
+ end
154
+ end
155
+
156
+ def jis_regexp(jis)
157
+ jis_str = jis.kind_of?(Numeric) ? [jis].pack('n') : jis
158
+
159
+ if Object.const_defined?(:Encoding)
160
+ Regexp.compile(Regexp.escape(jis_str.force_encoding("stateless-ISO-2022-JP-KDDI"))) # for au only
161
+ else
162
+ Regexp.compile(Regexp.escape(jis_str,"j"),nil,'j')
163
+ end
164
+ end
165
+
166
+ def encode(str, charset)
167
+ if Object.const_defined?(:Encoding)
168
+ str.encode(charset)
169
+ else
170
+ case charset
171
+ when /iso-2022-jp/i
172
+ NKF.nkf("-j", str)
173
+ when /shift_jis/i
174
+ NKF.nkf("-s", str)
175
+ when /utf-8/i
176
+ NKF.nkf("-w", str)
177
+ else
178
+ str
179
+ end
180
+ end
181
+ end
182
+
183
+ def wavedash_to_fullwidth_tilde(utf8_str)
184
+ utf8_str.gsub(WAVE_DASH, FULLWIDTH_TILDE)
185
+ end
186
+
187
+ def fullwidth_tilde_to_wavedash(utf8_str)
188
+ utf8_str.gsub(FULLWIDTH_TILDE, WAVE_DASH)
189
+ end
190
+
191
+ def force_encode(str, from, to)
192
+ s = str.dup
193
+ return str if detect_encoding(str) == to
194
+
195
+ if Object.const_defined?(:Encoding)
196
+ to = SJIS if to =~ /shift_jis/i
197
+
198
+ to_enc = ::Encoding.find(to)
199
+ return str if s.encoding == to_enc
200
+
201
+ if from
202
+ from_enc = ::Encoding.find(from)
203
+ s.force_encoding(from) unless s.encoding == from_enc
204
+ end
205
+
206
+ s.encode(to)
207
+ else
208
+ opt = []
209
+ opt << case from
210
+ when /iso-2022-jp/i
211
+ "-Jx"
212
+ when /shift_jis/i
213
+ "-Sx"
214
+ when /utf-8/i
215
+ "-Wx"
216
+ else
217
+ ""
218
+ end
219
+ opt << case to
220
+ when /iso-2022-jp/i
221
+ "-j"
222
+ when /shift_jis/i, /windows_31j/i
223
+ "-s"
224
+ when /utf-8/i
225
+ "-w"
226
+ else
227
+ ""
228
+ end
229
+ NKF.nkf(opt.join(" "), str)
230
+ end
231
+ end
232
+
233
+ def set_encoding(str, encoding)
234
+ if encoding and Object.const_defined?(:Encoding)
235
+ encoding = SJIS if encoding =~ /shift_jis/i
236
+ str.force_encoding(encoding)
237
+ end
238
+
239
+ str
240
+ end
241
+
242
+ def extract_charset(str)
243
+ case str
244
+ when /iso-2022-jp/i
245
+ "ISO-2022-JP"
246
+ when /shift_jis/i
247
+ "Shift_JIS"
248
+ when /utf-8/i
249
+ "UTF-8"
250
+ else
251
+ ""
252
+ end
253
+ end
254
+
255
+ def detect_encoding(str)
256
+ if Object.const_defined?(:Encoding)
257
+ case str.encoding
258
+ when ::Encoding::ISO2022_JP
259
+ JIS
260
+ when ::Encoding::Shift_JIS, ::Encoding::Windows_31J, ::Encoding::CP932
261
+ SJIS
262
+ when ::Encoding::UTF_8
263
+ UTF8
264
+ when ::Encoding::ASCII_8BIT
265
+ BINARY
266
+ else
267
+ BINARY
268
+ end
269
+ else
270
+ case NKF.guess(str)
271
+ when NKF::SJIS
272
+ SJIS
273
+ when NKF::JIS
274
+ JIS
275
+ when NKF::UTF8
276
+ UTF8
277
+ when NKF::BINARY
278
+ BINARY
279
+ else
280
+ BINARY
281
+ end
282
+ end
283
+ end
284
+
285
+ def ascii_8bit?(str)
286
+ detect_encoding(str) == BINARY
287
+ end
288
+ def utf8?(str)
289
+ detect_encoding(str) == UTF8
290
+ end
291
+ def shift_jis?(str)
292
+ detect_encoding(str) == SJIS
293
+ end
294
+ def jis?(str)
295
+ detect_encoding(str) == JIS
296
+ end
96
297
  end
97
298
  end
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 1
8
- - 5
9
- version: 0.1.5
8
+ - 6
9
+ version: 0.1.6
10
10
  platform: ruby
11
11
  authors:
12
12
  - Yoji Shidara
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-01-20 00:00:00 +09:00
18
+ date: 2011-01-21 00:00:00 +09:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
@@ -395,6 +395,7 @@ files:
395
395
  - test/rails/rails_root/app/controllers/trans_sid_mobile_controller.rb
396
396
  - test/rails/rails_root/app/controllers/trans_sid_none_controller.rb
397
397
  - test/rails/rails_root/app/helpers/application_helper.rb
398
+ - test/rails/rails_root/app/models/mobile_mailer.rb
398
399
  - test/rails/rails_root/app/models/user.rb
399
400
  - test/rails/rails_root/autotest/discover.rb
400
401
  - test/rails/rails_root/config/application.rb
@@ -415,6 +416,7 @@ files:
415
416
  - test/rails/rails_root/db/schema.rb
416
417
  - test/rails/rails_root/db/seeds.rb
417
418
  - test/rails/rails_root/spec/helpers/helpers_spec.rb
419
+ - test/rails/rails_root/spec/models/mobile_mailer_spec.rb
418
420
  - test/rails/rails_root/spec/requests/docomo_guid_spec.rb
419
421
  - test/rails/rails_root/spec/requests/docomo_spec.rb
420
422
  - test/rails/rails_root/spec/requests/emobile_spec.rb
@@ -455,7 +457,9 @@ files:
455
457
  - test/rails/rails_root/vendor/plugins/jpmobile/lib/jpmobile/filter.rb
456
458
  - test/rails/rails_root/vendor/plugins/jpmobile/lib/jpmobile/helpers.rb
457
459
  - test/rails/rails_root/vendor/plugins/jpmobile/lib/jpmobile/hook_action_controller.rb
460
+ - test/rails/rails_root/vendor/plugins/jpmobile/lib/jpmobile/hook_action_mailer.rb
458
461
  - test/rails/rails_root/vendor/plugins/jpmobile/lib/jpmobile/hook_action_view.rb
462
+ - test/rails/rails_root/vendor/plugins/jpmobile/lib/jpmobile/mail.rb
459
463
  - test/rails/rails_root/vendor/plugins/jpmobile/lib/jpmobile/mobile/abstract_mobile.rb
460
464
  - test/rails/rails_root/vendor/plugins/jpmobile/lib/jpmobile/mobile/android.rb
461
465
  - test/rails/rails_root/vendor/plugins/jpmobile/lib/jpmobile/mobile/au.rb
@@ -604,6 +608,7 @@ test_files:
604
608
  - test/rails/rails_root/app/controllers/trans_sid_mobile_controller.rb
605
609
  - test/rails/rails_root/app/controllers/trans_sid_none_controller.rb
606
610
  - test/rails/rails_root/app/helpers/application_helper.rb
611
+ - test/rails/rails_root/app/models/mobile_mailer.rb
607
612
  - test/rails/rails_root/app/models/user.rb
608
613
  - test/rails/rails_root/autotest/discover.rb
609
614
  - test/rails/rails_root/config/application.rb
@@ -624,6 +629,7 @@ test_files:
624
629
  - test/rails/rails_root/db/schema.rb
625
630
  - test/rails/rails_root/db/seeds.rb
626
631
  - test/rails/rails_root/spec/helpers/helpers_spec.rb
632
+ - test/rails/rails_root/spec/models/mobile_mailer_spec.rb
627
633
  - test/rails/rails_root/spec/requests/docomo_guid_spec.rb
628
634
  - test/rails/rails_root/spec/requests/docomo_spec.rb
629
635
  - test/rails/rails_root/spec/requests/emobile_spec.rb
@@ -664,7 +670,9 @@ test_files:
664
670
  - test/rails/rails_root/vendor/plugins/jpmobile/lib/jpmobile/filter.rb
665
671
  - test/rails/rails_root/vendor/plugins/jpmobile/lib/jpmobile/helpers.rb
666
672
  - test/rails/rails_root/vendor/plugins/jpmobile/lib/jpmobile/hook_action_controller.rb
673
+ - test/rails/rails_root/vendor/plugins/jpmobile/lib/jpmobile/hook_action_mailer.rb
667
674
  - test/rails/rails_root/vendor/plugins/jpmobile/lib/jpmobile/hook_action_view.rb
675
+ - test/rails/rails_root/vendor/plugins/jpmobile/lib/jpmobile/mail.rb
668
676
  - test/rails/rails_root/vendor/plugins/jpmobile/lib/jpmobile/mobile/abstract_mobile.rb
669
677
  - test/rails/rails_root/vendor/plugins/jpmobile/lib/jpmobile/mobile/android.rb
670
678
  - test/rails/rails_root/vendor/plugins/jpmobile/lib/jpmobile/mobile/au.rb