jpmobile 5.0.0 → 5.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (130) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +2 -3
  3. data/.rubocop.yml +123 -0
  4. data/CONTRIBUTING.md +16 -14
  5. data/Gemfile +6 -0
  6. data/Gemfile.lock +198 -0
  7. data/README.md +409 -0
  8. data/Rakefile +9 -9
  9. data/circle.yml +14 -0
  10. data/jpmobile.gemspec +9 -10
  11. data/lib/jpmobile.rb +6 -5
  12. data/lib/jpmobile/datum_conv.rb +35 -35
  13. data/lib/jpmobile/docomo_guid.rb +7 -8
  14. data/lib/jpmobile/email.rb +12 -12
  15. data/lib/jpmobile/emoticon.rb +85 -76
  16. data/lib/jpmobile/emoticon/au.rb +1284 -1284
  17. data/lib/jpmobile/emoticon/conversion_table.rb +3905 -3906
  18. data/lib/jpmobile/emoticon/docomo.rb +252 -252
  19. data/lib/jpmobile/emoticon/google.rb +9 -9
  20. data/lib/jpmobile/emoticon/softbank.rb +471 -471
  21. data/lib/jpmobile/emoticon/unicode.rb +9 -9
  22. data/lib/jpmobile/emoticon/z_combine.rb +20 -23
  23. data/lib/jpmobile/encoding.rb +1 -1
  24. data/lib/jpmobile/fallback_view_selector.rb +2 -3
  25. data/lib/jpmobile/filter.rb +44 -47
  26. data/lib/jpmobile/helpers.rb +38 -38
  27. data/lib/jpmobile/hook_action_view.rb +7 -7
  28. data/lib/jpmobile/hook_test_request.rb +0 -1
  29. data/lib/jpmobile/lookup_context.rb +1 -2
  30. data/lib/jpmobile/mail.rb +156 -134
  31. data/lib/jpmobile/mailer.rb +4 -4
  32. data/lib/jpmobile/mobile/abstract_mobile.rb +62 -36
  33. data/lib/jpmobile/mobile/android.rb +0 -1
  34. data/lib/jpmobile/mobile/android_tablet.rb +0 -1
  35. data/lib/jpmobile/mobile/au.rb +40 -28
  36. data/lib/jpmobile/mobile/black_berry.rb +0 -1
  37. data/lib/jpmobile/mobile/ddipocket.rb +1 -2
  38. data/lib/jpmobile/mobile/docomo.rb +47 -36
  39. data/lib/jpmobile/mobile/emobile.rb +1 -2
  40. data/lib/jpmobile/mobile/google_emoticon.rb +1 -1
  41. data/lib/jpmobile/mobile/ipad.rb +0 -1
  42. data/lib/jpmobile/mobile/iphone.rb +0 -1
  43. data/lib/jpmobile/mobile/smart_phone.rb +0 -1
  44. data/lib/jpmobile/mobile/softbank.rb +24 -20
  45. data/lib/jpmobile/mobile/tablet.rb +0 -1
  46. data/lib/jpmobile/mobile/unicode_emoticon.rb +15 -15
  47. data/lib/jpmobile/mobile/vodafone.rb +0 -1
  48. data/lib/jpmobile/mobile/willcom.rb +7 -7
  49. data/lib/jpmobile/mobile/windows_phone.rb +0 -1
  50. data/lib/jpmobile/path_set.rb +1 -1
  51. data/lib/jpmobile/position.rb +30 -14
  52. data/lib/jpmobile/rack/filter.rb +4 -4
  53. data/lib/jpmobile/rack/mobile_carrier.rb +0 -1
  54. data/lib/jpmobile/rack/params_filter.rb +7 -6
  55. data/lib/jpmobile/rails.rb +3 -4
  56. data/lib/jpmobile/request_with_mobile.rb +9 -6
  57. data/lib/jpmobile/resolver.rb +23 -17
  58. data/lib/jpmobile/session/active_record_store.rb +7 -9
  59. data/lib/jpmobile/session/mem_cache_store.rb +7 -9
  60. data/lib/jpmobile/sinatra.rb +1 -1
  61. data/lib/jpmobile/trans_sid.rb +15 -20
  62. data/lib/jpmobile/util.rb +61 -77
  63. data/lib/jpmobile/version.rb +1 -1
  64. data/lib/tasks/jpmobile_tasks.rake +35 -28
  65. data/spec/rack/jpmobile/android_spec.rb +5 -5
  66. data/spec/rack/jpmobile/au_spec.rb +74 -59
  67. data/spec/rack/jpmobile/black_berry_spec.rb +5 -5
  68. data/spec/rack/jpmobile/docomo_spec.rb +77 -64
  69. data/spec/rack/jpmobile/emoticon_spec.rb +137 -121
  70. data/spec/rack/jpmobile/filter_spec.rb +149 -128
  71. data/spec/rack/jpmobile/iphone_spec.rb +9 -8
  72. data/spec/rack/jpmobile/mobile_by_ua_spec.rb +22 -21
  73. data/spec/rack/jpmobile/params_filter_spec.rb +104 -96
  74. data/spec/rack/jpmobile/softbank_spec.rb +49 -42
  75. data/spec/rack/jpmobile/willcom_spec.rb +22 -18
  76. data/spec/rack/jpmobile/windows_phone.rb +5 -5
  77. data/spec/rack_helper.rb +9 -8
  78. data/spec/spec_helper.rb +5 -6
  79. data/spec/unit/decorated_mail_spec.rb +8 -9
  80. data/spec/unit/email_spec.rb +44 -44
  81. data/spec/unit/emoticon_spec.rb +57 -58
  82. data/spec/unit/encoding_spec.rb +35 -36
  83. data/spec/unit/is_carrier_spec.rb +49 -49
  84. data/spec/unit/mail_spec.rb +153 -143
  85. data/spec/unit/mobile/iphone_spec.rb +6 -7
  86. data/spec/unit/receive_mail_spec.rb +172 -173
  87. data/spec/unit/spec_helper.rb +6 -6
  88. data/spec/unit/util_spec.rb +125 -46
  89. data/spec/unit/valid_ip_spec.rb +35 -35
  90. data/spec/unit/variants_spec.rb +18 -19
  91. data/test/rails/overrides/Gemfile +54 -0
  92. data/test/rails/overrides/Gemfile.jpmobile +1 -2
  93. data/test/rails/overrides/app/controllers/admin/top_controller.rb +1 -1
  94. data/test/rails/overrides/app/controllers/docomo_guid_base_controller.rb +1 -1
  95. data/test/rails/overrides/app/controllers/filter_controller_base.rb +14 -6
  96. data/test/rails/overrides/app/controllers/hankaku_input_filter_controller.rb +1 -1
  97. data/test/rails/overrides/app/controllers/mobile_spec_controller.rb +1 -1
  98. data/test/rails/overrides/app/controllers/template_path_controller.rb +1 -2
  99. data/test/rails/overrides/app/controllers/trans_sid_base_controller.rb +18 -10
  100. data/test/rails/overrides/app/controllers/trans_sid_metal_controller.rb +0 -1
  101. data/test/rails/overrides/app/mailers/decorated_mailer.rb +3 -4
  102. data/test/rails/overrides/app/mailers/mobile_mailer.rb +5 -6
  103. data/test/rails/overrides/app/mailers/normal_mailer.rb +2 -3
  104. data/test/rails/overrides/autotest/discover.rb +2 -2
  105. data/test/rails/overrides/config/initializers/jpmobile_generator.rb +2 -2
  106. data/test/rails/overrides/config/routes.rb +95 -2
  107. data/test/rails/overrides/db/migrate/001_add_sessions_table.rb +2 -2
  108. data/test/rails/overrides/db/migrate/20100824062306_create_users.rb +1 -1
  109. data/test/rails/overrides/spec/controllers/docomo_guid_spec.rb +24 -25
  110. data/test/rails/overrides/spec/controllers/helpers_spec.rb +88 -88
  111. data/test/rails/overrides/spec/controllers/mobile_spec_controller_spec.rb +9 -9
  112. data/test/rails/overrides/spec/controllers/template_path_spec.rb +19 -19
  113. data/test/rails/overrides/spec/features/admin/top_spec.rb +7 -7
  114. data/test/rails/overrides/spec/features/filter_spec.rb +101 -102
  115. data/test/rails/overrides/spec/helpers/helpers_spec.rb +4 -4
  116. data/test/rails/overrides/spec/mailers/decorated_mailer_spec.rb +12 -12
  117. data/test/rails/overrides/spec/mailers/mobile_mailer_spec.rb +209 -194
  118. data/test/rails/overrides/spec/mailers/normal_mailer_spec.rb +10 -10
  119. data/test/rails/overrides/spec/rails_helper.rb +1 -1
  120. data/test/rails/overrides/spec/requests/docomo_spec.rb +13 -13
  121. data/test/rails/overrides/spec/requests/emobile_spec.rb +19 -19
  122. data/test/rails/overrides/spec/requests/pc_spec.rb +6 -6
  123. data/test/rails/overrides/spec/requests/softbank_emulator_spec.rb +6 -6
  124. data/test/rails/overrides/spec/requests/template_path_spec.rb +60 -60
  125. data/test/rails/overrides/spec/requests/trans_sid_spec.rb +89 -89
  126. data/test/sinatra/test/filter_test.rb +10 -11
  127. data/tools/e4u_conv.rb +0 -1
  128. metadata +18 -14
  129. data/README +0 -0
  130. data/README.rdoc +0 -308
@@ -1,4 +1,3 @@
1
- # -*- coding: utf-8 -*-
2
1
  # 出力変換
3
2
  require 'scanf'
4
3
 
@@ -14,8 +13,8 @@ module Jpmobile
14
13
 
15
14
  status, env, response = @app.call(env)
16
15
 
17
- if env['Content-Type'] =~ %r!text/html|application/xhtml\+xml!
18
- if mobile and mobile.apply_filter?
16
+ if env['Content-Type'] =~ %r{text/html|application/xhtml\+xml}
17
+ if mobile && mobile.apply_filter?
19
18
  type, charset = env['Content-Type'].split(/;\s*charset=/)
20
19
 
21
20
  body = response_to_body(response)
@@ -24,7 +23,7 @@ module Jpmobile
24
23
 
25
24
  response, charset = mobile.to_external(body, type, charset)
26
25
 
27
- if type and charset
26
+ if type && charset
28
27
  env['Content-Type'] = "#{type}; charset=#{charset}"
29
28
  end
30
29
  elsif Jpmobile::Emoticon.pc_emoticon?
@@ -39,6 +38,7 @@ module Jpmobile
39
38
  end
40
39
 
41
40
  private
41
+
42
42
  def response_to_body(response)
43
43
  if response.respond_to?(:to_str)
44
44
  response.to_str
@@ -1,4 +1,3 @@
1
- # -*- coding: utf-8 -*-
2
1
  # UserAgent から生成すべき class 名を判定する
3
2
  module Jpmobile
4
3
  class MobileCarrier
@@ -1,4 +1,3 @@
1
- # -*- coding: utf-8 -*-
2
1
  # リクエストパラメータの変換
3
2
  module Jpmobile
4
3
  class ParamsFilter
@@ -8,11 +7,11 @@ module Jpmobile
8
7
 
9
8
  def call(env)
10
9
  # 入力
11
- if @mobile = env['rack.jpmobile'] and @mobile.apply_params_filter?
10
+ if (@mobile = env['rack.jpmobile']) && @mobile.apply_params_filter?
12
11
  # パラメータをkey, valueに分解
13
12
  # form_params
14
13
  unless env['REQUEST_METHOD'] == 'GET' || env['REQUEST_METHOD'] == 'HEAD'
15
- unless env['CONTENT_TYPE'] =~ /application\/json|application\/xml/
14
+ unless env['CONTENT_TYPE'] =~ %r{application/json|application/xml}
16
15
  env['rack.input'] = StringIO.new(parse_query(env['rack.input'].read))
17
16
  end
18
17
  end
@@ -27,21 +26,23 @@ module Jpmobile
27
26
  end
28
27
 
29
28
  private
29
+
30
30
  def to_internal(str)
31
31
  ::Rack::Utils.escape(@mobile.to_internal(::Rack::Utils.unescape(str)))
32
32
  end
33
+
33
34
  def parse_query(str)
34
35
  return nil unless str
35
36
 
36
37
  new_array = []
37
- str.split("&").each do |param_pair|
38
- k, v = param_pair.split("=")
38
+ str.split('&').each do |param_pair|
39
+ k, v = param_pair.split('=')
39
40
  k = to_internal(k) if k
40
41
  v = to_internal(v) if v
41
42
  new_array << "#{k}=#{v}" if k
42
43
  end
43
44
 
44
- new_array.join("&")
45
+ new_array.join('&')
45
46
  end
46
47
  end
47
48
  end
@@ -1,4 +1,3 @@
1
- # -*- coding: utf-8 -*-
2
1
  ActiveSupport.on_load(:action_controller) do
3
2
  require 'jpmobile/docomo_guid'
4
3
  require 'jpmobile/filter'
@@ -14,10 +13,10 @@ end
14
13
 
15
14
  ActiveSupport.on_load(:after_initialize) do
16
15
  case Rails.application.config.session_store.to_s
17
- when "ActionDispatch::Session::MemCacheStore"
16
+ when 'ActionDispatch::Session::MemCacheStore'
18
17
  require 'jpmobile/session/mem_cache_store'
19
18
  ActionDispatch::Session::MemCacheStore.send :prepend, Jpmobile::ParamsOverCookie
20
- when "ActiveRecord::SessionStore"
19
+ when 'ActionDispatch::Session::ActiveRecordStore'
21
20
  require 'jpmobile/session/active_record_store'
22
21
  ActionDispatch::Session::AbstractStore.send :prepend, Jpmobile::ParamsOverCookie
23
22
  else
@@ -47,7 +46,7 @@ module Jpmobile
47
46
  before_action :register_mobile
48
47
 
49
48
  self._view_paths = self._view_paths.dup
50
- self.view_paths.unshift(*self.view_paths.map {|resolver| Jpmobile::Resolver.new(resolver.to_path) })
49
+ self.view_paths.unshift(*self.view_paths.map { |resolver| Jpmobile::Resolver.new(resolver.to_path) })
51
50
  end
52
51
  end
53
52
 
@@ -1,4 +1,3 @@
1
- # -*- coding: utf-8 -*-
2
1
  #
3
2
  # jpmobile の各機能を提供するモジュール
4
3
  # envメソッドが実装されている必要がある。
@@ -13,22 +12,26 @@ module Jpmobile
13
12
  # for reverse proxy.
14
13
  def remote_addr
15
14
  if respond_to?(:remote_ip)
16
- return __send__(:remote_ip) # for Rails
15
+ __send__(:remote_ip) # for Rails
17
16
  elsif respond_to?(:ip)
18
- return __send__(:ip) # for Rack
17
+ __send__(:ip) # for Rack
19
18
  else
20
- return ( env["HTTP_X_FORWARDED_FOR"] ? env["HTTP_X_FORWARDED_FOR"].split(',').pop : env["REMOTE_ADDR"] )
19
+ if env['HTTP_X_FORWARDED_FOR']
20
+ env['HTTP_X_FORWARDED_FOR'].split(',').pop
21
+ else
22
+ env['REMOTE_ADDR']
23
+ end
21
24
  end
22
25
  end
23
26
 
24
27
  # 環境変数 HTTP_USER_AGENT を設定する。
25
28
  def user_agent=(str)
26
- self.env["HTTP_USER_AGENT"] = str
29
+ self.env['HTTP_USER_AGENT'] = str
27
30
  end
28
31
 
29
32
  # 携帯電話からであれば +true+を、そうでなければ +false+ を返す。
30
33
  def mobile?
31
- mobile and not mobile.smart_phone?
34
+ mobile and !mobile.smart_phone?
32
35
  end
33
36
 
34
37
  # viewの切り替えをするかどうか
@@ -1,10 +1,10 @@
1
1
  module Jpmobile
2
2
  class Resolver < ActionView::FileSystemResolver
3
- EXTENSIONS = [:locale, :formats, :handlers, :mobile]
4
- DEFAULT_PATTERN = ":prefix/:action{_:mobile,}{.:locale,}{.:formats,}{+:variants,}{.:handlers,}"
3
+ EXTENSIONS = [:locale, :formats, :handlers, :mobile].freeze
4
+ DEFAULT_PATTERN = ':prefix/:action{_:mobile,}{.:locale,}{.:formats,}{+:variants,}{.:handlers,}'.freeze
5
5
 
6
- def initialize(path, pattern=nil)
7
- raise ArgumentError, "path already is a Resolver class" if path.is_a?(Resolver)
6
+ def initialize(path, pattern = nil)
7
+ raise ArgumentError, 'path already is a Resolver class' if path.is_a?(Resolver)
8
8
  super(path, pattern || DEFAULT_PATTERN)
9
9
  @path = File.expand_path(path)
10
10
  end
@@ -21,7 +21,7 @@ module Jpmobile
21
21
  self.class_eval do
22
22
  def find_template_paths(query)
23
23
  # deals with case-insensitive file systems.
24
- sanitizer = Hash.new { |h,dir| h[dir] = Dir["#{dir}/*"] }
24
+ sanitizer = Hash.new { |h, dir| h[dir] = Dir["#{dir}/*"] }
25
25
 
26
26
  Dir[query].reject { |filename|
27
27
  File.directory?(filename) ||
@@ -37,18 +37,24 @@ module Jpmobile
37
37
  handler, format, variant = extract_handler_and_format_and_variant(template, formats)
38
38
  contents = File.binread(template)
39
39
 
40
- if format
41
- jpmobile_variant = template.match(/.+#{path}(.+)\.#{format.to_sym.to_s}.*$/) ? $1 : ''
42
- virtual_path = jpmobile_variant.blank? ? path.virtual : path.to_str + jpmobile_variant
43
- else
44
- virtual_path = path.virtual
45
- end
46
-
47
- ActionView::Template.new(contents, File.expand_path(template), handler,
48
- :virtual_path => virtual_path,
49
- :format => format,
50
- :variant => variant,
51
- :updated_at => mtime(template)
40
+ virtual_path = if format
41
+ if template =~ /.+#{path}(.+)\.#{format.to_sym.to_s}.*$/
42
+ path.to_str + Regexp.last_match(1)
43
+ else
44
+ path.virtual
45
+ end
46
+ else
47
+ path.virtual
48
+ end
49
+
50
+ ActionView::Template.new(
51
+ contents,
52
+ File.expand_path(template),
53
+ handler,
54
+ virtual_path: virtual_path,
55
+ format: format,
56
+ variant: variant,
57
+ updated_at: mtime(template)
52
58
  )
53
59
  }
54
60
  end
@@ -1,13 +1,11 @@
1
- module ActionDispatch
2
- module Session
3
- class ActiveRecordStore
4
- def destroy_session_with_jpmobile(env, session_id, options)
5
- destroy_session_without_jpmobile(env, session_id, options)
1
+ module Jpmobile
2
+ module ActiveRecordStoreRegenerateSessionId
3
+ def destroy_session(env, session_id, options)
4
+ super(env, session_id, options)
6
5
 
7
- session_id || Jpmobile::SessionID.generate_sid
8
- end
9
-
10
- alias_method_chain :destroy_session, :jpmobile
6
+ session_id || Jpmobile::SessionID.generate_sid
11
7
  end
12
8
  end
13
9
  end
10
+
11
+ ActionDispatch::Session::ActiveRecordStore.send :prepend, Jpmobile::ActiveRecordStoreRegenerateSessionId
@@ -1,13 +1,11 @@
1
- module ActionDispatch
2
- module Session
3
- class MemCacheStore
4
- def destroy_session_with_jpmobile(env, session_id, options)
5
- destroy_session_without_jpmobile(env, session_id, options)
1
+ module Jpmobile
2
+ module MemCacheStoreRegenerateSessionId
3
+ def destroy_session(env, session_id, options)
4
+ super(env, session_id, options)
6
5
 
7
- session_id || generate_sid
8
- end
9
-
10
- alias_method_chain :destroy_session, :jpmobile
6
+ session_id || generate_sid
11
7
  end
12
8
  end
13
9
  end
10
+
11
+ ActionDispatch::Session::MemCacheStore.send :prepend, Jpmobile::MemCacheStoreRegenerateSessionId
@@ -4,7 +4,7 @@ module Jpmobile
4
4
  # Calls the given block for every possible template file in views,
5
5
  # named name.ext, where ext is registered on engine.
6
6
  def find_template(views, name, engine)
7
- if env['rack.jpmobile'] and !env['rack.jpmobile'].variants.empty?
7
+ if env['rack.jpmobile'] && !env['rack.jpmobile'].variants.empty?
8
8
  env['rack.jpmobile'].variants.each do |variant|
9
9
  yield ::File.join(views, "#{name}_#{variant}.#{@preferred_extension}")
10
10
  end
@@ -1,10 +1,10 @@
1
- # -*- coding: utf-8 -*-
2
1
  # = セッションIDの付与
3
2
  require 'active_support/version'
4
3
 
5
4
  module Jpmobile
6
5
  module SessionID
7
6
  require 'action_dispatch/middleware/session/abstract_store'
7
+
8
8
  module_function
9
9
 
10
10
  extend ActionDispatch::Session::Compatibility
@@ -12,7 +12,7 @@ module Jpmobile
12
12
 
13
13
  module ParamsOverCookie
14
14
  def extract_session_id(req)
15
- if req.params[@key] and !@cookie_only
15
+ if req.params[@key] && !@cookie_only
16
16
  sid = req.params[@key]
17
17
  end
18
18
  sid ||= req.cookies[@key]
@@ -26,21 +26,21 @@ module Jpmobile
26
26
  end
27
27
 
28
28
  protected
29
+
29
30
  # URLにsession_idを追加する。
30
31
  def default_url_options
31
32
  result = super || {}.with_indifferent_access
32
33
  return result unless request # for test process
33
34
  return result unless apply_trans_sid?
34
- return result.merge({session_key.to_sym => jpmobile_session_id})
35
+ result.merge({ session_key.to_sym => jpmobile_session_id })
35
36
  end
36
37
 
37
38
  private
39
+
38
40
  # session_keyを返す。
39
41
  def session_key
40
- unless key = Rails.application.config.session_options.merge(request.session_options || {})[:key]
41
- key = ActionDispatch::Session::AbstractStore::DEFAULT_OPTIONS[:key]
42
- end
43
- key
42
+ Rails.application.config.session_options.merge(request.session_options || {})[:key] ||
43
+ ActionDispatch::Session::AbstractStore::DEFAULT_OPTIONS[:key]
44
44
  end
45
45
 
46
46
  # session_idを返す
@@ -52,7 +52,7 @@ module Jpmobile
52
52
 
53
53
  # session_idを埋め込むためのhidden fieldを出力する。
54
54
  def sid_hidden_field_tag
55
- "<input type=\"hidden\" name=\"#{CGI::escapeHTML session_key}\" value=\"#{CGI::escapeHTML jpmobile_session_id}\" />"
55
+ "<input type=\"hidden\" name=\"#{CGI.escapeHTML session_key}\" value=\"#{CGI.escapeHTML jpmobile_session_id}\" />"
56
56
  end
57
57
 
58
58
  # formにsession_idを追加する。
@@ -60,18 +60,16 @@ module Jpmobile
60
60
  return unless request # for test process
61
61
  return unless apply_trans_sid?
62
62
  return unless jpmobile_session_id
63
- response.body = response.body.gsub(%r{(</form>)}i, sid_hidden_field_tag+'\1')
63
+ response.body = response.body.gsub(%r{(</form>)}i, sid_hidden_field_tag + '\1')
64
64
  end
65
65
  end
66
66
 
67
67
  module TransSidRedirecting
68
68
  def redirect_to(options = {}, response_status = {})
69
- if apply_trans_sid? and jpmobile_session_id
69
+ if apply_trans_sid? && jpmobile_session_id && options != :back && options !~ /^\w[\w+.-]*:.*/
70
70
  case options
71
- when %r{^\w[\w+.-]*:.*}
72
- # nothing to do
73
71
  when String
74
- unless options.match(/#{session_key}/)
72
+ unless options =~ /#{session_key}/
75
73
  url = URI.parse(options)
76
74
  if url.query
77
75
  url.query += "&#{session_key}=#{jpmobile_session_id}"
@@ -80,14 +78,10 @@ module Jpmobile
80
78
  end
81
79
  options = url.to_s
82
80
  end
83
- when :back
84
- # nothing to do
85
81
  when Hash
86
82
  unless options[session_key.to_sym]
87
83
  options[session_key.to_sym] = jpmobile_session_id
88
84
  end
89
- else
90
- # nothing to do
91
85
  end
92
86
  end
93
87
 
@@ -109,21 +103,22 @@ module ActionController
109
103
  end
110
104
 
111
105
  private
106
+
112
107
  # trans_sidを適用すべきかを返す。
113
108
  def apply_trans_sid?
114
109
  # session_id が blank の場合は適用しない
115
- return false if trans_sid_mode and jpmobile_session_id.blank?
110
+ return false if trans_sid_mode && jpmobile_session_id.blank?
116
111
 
117
112
  case trans_sid_mode
118
113
  when :always
119
114
  return true
120
115
  when :mobile
121
- if request.mobile? and !request.mobile.supports_cookie?
116
+ if request.mobile? && !request.mobile.supports_cookie?
122
117
  return true
123
118
  end
124
119
  end
125
120
 
126
- return false
121
+ false
127
122
  end
128
123
  end
129
124
  end
@@ -1,29 +1,28 @@
1
- # -*- coding: utf-8 -*-
2
1
  require 'tempfile'
3
- require 'nkf'
4
2
 
5
3
  module Jpmobile
6
4
  module Util
7
5
  # SJIS = "Shift_JIS"
8
- SJIS = "Windows-31J"
9
- UTF8 = "UTF-8"
10
- JIS = "ISO-2022-JP"
11
- JIS_WIN = "CP50220"
12
- BINARY = "ASCII-8BIT"
6
+ SJIS = 'Windows-31J'.freeze
7
+ UTF8 = 'UTF-8'.freeze
8
+ JIS = 'ISO-2022-JP'.freeze
9
+ JIS_WIN = 'CP50220'.freeze
10
+ BINARY = 'ASCII-8BIT'.freeze
13
11
 
14
- WAVE_DASH = [0x301c].pack("U")
15
- FULLWIDTH_TILDE = [0xff5e].pack("U")
12
+ WAVE_DASH = [0x301c].pack('U')
13
+ FULLWIDTH_TILDE = [0xff5e].pack('U')
16
14
 
17
- OVERLINE = [0x203e].pack("U")
18
- FULLWIDTH_MACRON = [0xffe3].pack("U")
15
+ OVERLINE = [0x203e].pack('U')
16
+ FULLWIDTH_MACRON = [0xffe3].pack('U')
19
17
 
20
- EM_DASH = [0x2014].pack("U")
21
- HORIZONTAL_BAR = [0x2015].pack("U")
18
+ EM_DASH = [0x2014].pack('U')
19
+ HORIZONTAL_BAR = [0x2015].pack('U')
22
20
 
23
- MINUS_SIGN = [0x2212].pack("U")
24
- FULLWIDTH_HYPHEN_MINUS = [0xFF0D].pack("U")
21
+ MINUS_SIGN = [0x2212].pack('U')
22
+ FULLWIDTH_HYPHEN_MINUS = [0xFF0D].pack('U')
25
23
 
26
24
  module_function
25
+
27
26
  def deep_convert(obj, &proc)
28
27
  case obj
29
28
  when Hash
@@ -37,10 +36,10 @@ module Jpmobile
37
36
  deep_convert(value, &proc)
38
37
  end
39
38
  when Symbol
40
- new_obj = proc.call(obj.to_s).to_sym
39
+ new_obj = yield(obj.to_s).to_sym
41
40
  when String
42
41
  obj = obj.to_param if obj.respond_to?(:to_param)
43
- new_obj = proc.call(obj)
42
+ new_obj = yield(obj)
44
43
  else
45
44
  # NilClass, TrueClass, FalseClass, Tempfile, StringIO, etc...
46
45
  new_obj = obj
@@ -50,43 +49,43 @@ module Jpmobile
50
49
  end
51
50
 
52
51
  def sjis(str)
53
- if str.respond_to?(:force_encoding) and !shift_jis?(str)
54
- str.force_encoding(SJIS)
52
+ unless shift_jis?(str)
53
+ str = str.dup.force_encoding(SJIS)
55
54
  end
56
55
  str
57
56
  end
58
57
 
59
58
  def utf8(str)
60
- if str.respond_to?(:force_encoding) and !utf8?(str)
61
- str.force_encoding(UTF8)
59
+ unless utf8?(str)
60
+ str = str.dup.force_encoding(UTF8)
62
61
  end
63
62
  str
64
63
  end
65
64
 
66
65
  def jis(str)
67
- if str.respond_to?(:force_encoding) and !jis?(str)
68
- str.force_encoding(JIS)
66
+ unless jis?(str)
67
+ str = str.dup.force_encoding(JIS)
69
68
  end
70
69
  str
71
70
  end
72
71
 
73
72
  def jis_win(str)
74
- if str.respond_to?(:force_encoding) and !jis?(str)
75
- str.force_encoding(JIS_WIN)
73
+ unless jis?(str)
74
+ str = str.dup.force_encoding(JIS_WIN)
76
75
  end
77
76
  str
78
77
  end
79
78
 
80
79
  def ascii_8bit(str)
81
- if str.respond_to?(:force_encoding) and !ascii_8bit?(str)
82
- str.force_encoding(BINARY)
80
+ unless ascii_8bit?(str)
81
+ str = str.dup.force_encoding(BINARY)
83
82
  end
84
83
  str
85
84
  end
86
85
 
87
86
  def ascii_compatible!(str)
88
- if str.respond_to?(:encoding) and !str.encoding.ascii_compatible?
89
- str.force_encoding(BINARY)
87
+ unless str.encoding.ascii_compatible?
88
+ str = str.dup.force_encoding(BINARY)
90
89
  end
91
90
  str
92
91
  end
@@ -101,21 +100,13 @@ module Jpmobile
101
100
  # マイナス対策(不可逆的)
102
101
  utf8_str = minus_sign_to_fullwidth_hyphen_minus(utf8_str)
103
102
 
104
- if utf8_str.respond_to?(:encode)
105
- utf8_str.
103
+ utf8_str.
106
104
  gsub(/(\r\n|\r|\n)/, "\r\n").
107
- encode(SJIS, :undef => :replace, :replace => '?')
108
- else
109
- NKF.nkf("-m0 -x -W --oc=cp932 --fb-subchar=63", utf8_str).gsub(/(\r\n|\r|\n)/, "\r\n")
110
- end
105
+ encode(SJIS, undef: :replace, replace: '?')
111
106
  end
112
107
 
113
108
  def sjis_to_utf8(sjis_str)
114
- utf8_str = if sjis_str.respond_to?(:encode)
115
- sjis_str.encode("UTF-8", :universal_newline => true)
116
- else
117
- NKF.nkf("-m0 -x -w --ic=cp932", sjis_str).gsub(/\r\n?/, "\n")
118
- end
109
+ utf8_str = sjis_str.encode('UTF-8', universal_newline: true)
119
110
 
120
111
  # 波ダッシュ対策
121
112
  fullwidth_tilde_to_wavedash(utf8_str)
@@ -125,21 +116,13 @@ module Jpmobile
125
116
  # 波ダッシュ対策
126
117
  utf8_str = fullwidth_tilde_to_wavedash(utf8_str)
127
118
 
128
- if utf8_str.respond_to?(:encode)
129
- utf8_str.
119
+ utf8_str.
130
120
  gsub(/(\r\n|\r|\n)/, "\r\n").
131
- encode(JIS, :undef => :replace, :replace => '?')
132
- else
133
- NKF.nkf("-m0 -x -Wj --fb-subchar=63", utf8_str).gsub(/(\r\n|\r|\n)/, "\r\n")
134
- end
121
+ encode(JIS, undef: :replace, replace: '?')
135
122
  end
136
123
 
137
124
  def jis_to_utf8(jis_str)
138
- if jis_str.respond_to?(:encode)
139
- jis_str.encode(UTF8, :universal_newline => true)
140
- else
141
- NKF.nkf("-m0 -x -Jw", jis_str).gsub(/\r\n?/, "\n")
142
- end
125
+ jis_str.encode(UTF8, universal_newline: true)
143
126
  end
144
127
 
145
128
  def regexp_utf8_to_sjis(utf8_str)
@@ -152,13 +135,13 @@ module Jpmobile
152
135
 
153
136
  def hash_to_utf8(hash)
154
137
  new_hash = {}
155
- hash.each do |keu, value|
138
+ hash.each do |key, value|
156
139
  new_hash[utf8(key)] = utf8(value)
157
140
  end
158
141
  end
159
142
 
160
143
  def sjis_regexp(sjis)
161
- sjis_str = if sjis.kind_of?(Numeric)
144
+ sjis_str = if sjis.is_a?(Numeric)
162
145
  [sjis].pack('n')
163
146
  else
164
147
  sjis
@@ -168,26 +151,26 @@ module Jpmobile
168
151
  end
169
152
 
170
153
  def jis_regexp(jis)
171
- jis_str = jis.kind_of?(Numeric) ? [jis].pack('n') : jis
154
+ jis_str = jis.is_a?(Numeric) ? [jis].pack('n') : jis
172
155
 
173
156
  Regexp.compile(Regexp.escape(jis_str.force_encoding(BINARY)))
174
157
  end
175
158
 
176
159
  def jis_string_regexp
177
- Regexp.compile(Regexp.escape(ascii_8bit("\x1b\x24\x42")) + "(.+?)" + Regexp.escape(ascii_8bit("\x1b\x28\x42")))
160
+ Regexp.compile(Regexp.escape(ascii_8bit("\x1b\x24\x42")) + '(.+?)' + Regexp.escape(ascii_8bit("\x1b\x28\x42")))
178
161
  end
179
162
 
180
163
  def encode(str, charset)
181
- if (charset.nil? or charset == "" or str.nil? or str == "")
182
- str
183
- elsif utf8?(str) and charset.match(/iso-2022-jp/i)
164
+ return str if charset.nil? || (charset == '') || str.nil? || (str == '')
165
+ return str.encode(charset) unless utf8?(str)
166
+
167
+ case charset
168
+ when /iso-2022-jp/i
184
169
  utf8_to_jis(str)
185
- elsif utf8?(str) and charset.match(/shift_jis/i)
170
+ when /shift_jis/i
186
171
  utf8_to_sjis(str)
187
- elsif utf8?(str) and charset.match(/utf-8/i)
172
+ when /utf-8/i
188
173
  str
189
- else
190
- str.encode(charset)
191
174
  end
192
175
  end
193
176
 
@@ -237,12 +220,10 @@ module Jpmobile
237
220
  s.encode(to)
238
221
  rescue ::Encoding::InvalidByteSequenceError, ::Encoding::UndefinedConversionError => e
239
222
  # iPhone MailがISO-2022-JPに半角カナや①などのCP50220文字を含めてくる問題対策
240
- if s.encoding == ::Encoding::ISO2022_JP
241
- s.force_encoding(::Encoding::CP50220)
242
- retry
243
- else
244
- raise e
245
- end
223
+ raise e unless s.encoding == ::Encoding::ISO2022_JP
224
+
225
+ s.force_encoding(::Encoding::CP50220)
226
+ retry
246
227
  end
247
228
  end
248
229
 
@@ -256,13 +237,13 @@ module Jpmobile
256
237
  def extract_charset(str)
257
238
  case str
258
239
  when /iso-2022-jp/i
259
- "ISO-2022-JP"
240
+ 'ISO-2022-JP'
260
241
  when /shift_jis/i
261
- "Shift_JIS"
242
+ 'Shift_JIS'
262
243
  when /utf-8/i
263
- "UTF-8"
244
+ 'UTF-8'
264
245
  else
265
- ""
246
+ ''
266
247
  end
267
248
  end
268
249
 
@@ -284,12 +265,15 @@ module Jpmobile
284
265
  def ascii_8bit?(str)
285
266
  detect_encoding(str) == BINARY
286
267
  end
268
+
287
269
  def utf8?(str)
288
270
  detect_encoding(str) == UTF8
289
271
  end
272
+
290
273
  def shift_jis?(str)
291
274
  detect_encoding(str) == SJIS
292
275
  end
276
+
293
277
  def jis?(str)
294
278
  detect_encoding(str) == JIS
295
279
  end
@@ -297,7 +281,7 @@ module Jpmobile
297
281
  def fold_text(str, size = 15)
298
282
  folded_texts = []
299
283
 
300
- while texts = split_text(str, size) and texts.first.size != 0
284
+ while (texts = split_text(str, size)) && (texts.first.size != 0)
301
285
  folded_texts << texts.first
302
286
  str = texts.last
303
287
  end
@@ -306,16 +290,16 @@ module Jpmobile
306
290
  end
307
291
 
308
292
  def split_text(str, size = 15)
309
- return nil if str.nil? or str == ''
293
+ return nil if str.nil? || (str == '')
310
294
 
311
- [str[0..(size-1)], str[size..-1]]
295
+ [str[0..(size - 1)], str[size..-1]]
312
296
  end
313
297
 
314
298
  def invert_table(hash)
315
299
  result = {}
316
300
  hash.keys.each do |key|
317
301
  if result[hash[key]]
318
- if !key.kind_of?(Array) and !result[hash[key]].kind_of?(Array) and result[hash[key]] > key
302
+ if !key.is_a?(Array) && !result[hash[key]].is_a?(Array) && result[hash[key]] > key
319
303
  result[hash[key]] = key
320
304
  end
321
305
  else
@@ -336,7 +320,7 @@ module Jpmobile
336
320
  end
337
321
 
338
322
  _extract_charset = Jpmobile::Util.extract_charset(_str)
339
- charset = _extract_charset unless _extract_charset.empty? or _extract_charset == charset
323
+ charset = _extract_charset unless _extract_charset.empty? || (_extract_charset == charset)
340
324
  Jpmobile::Util.set_encoding(_str, charset)
341
325
  end
342
326
  end