Class: Jpmobile::Mobile::AbstractMobile
- Inherits:
-
Object
- Object
- Jpmobile::Mobile::AbstractMobile
- Defined in:
- lib/jpmobile/mobile/abstract_mobile.rb
Overview
携帯電話の抽象クラス。
Constant Summary collapse
- MAIL_CHARSET =
メールのデフォルトのcharset
'ISO-2022-JP'.freeze
- USER_AGENT_REGEXP =
対応するuser-agentの正規表現
nil
- MAIL_ADDRESS_REGEXP =
対応するメールアドレスの正規表現
nil
- MAIL_CONTENT_TRANSFER_ENCODING =
テキスト部分の content-transfer-encoding
'7bit'.freeze
Instance Attribute Summary collapse
-
#decorated ⇒ Object
writeonly
Sets the attribute decorated.
Class Method Summary collapse
- .add_user_agent_regexp(regexp) ⇒ Object
- .carrier(env) ⇒ Object
-
.check_carrier(env) ⇒ Object
リクエストがこのクラスに属するか調べる メソッド名に関して非常に不安.
- .ip_address_class ⇒ Object
- .user_agent_regexp ⇒ Object
-
.valid_ip?(remote_addr) ⇒ Boolean
当該キャリアのIPアドレス帯域からのアクセスであれば
true
を返す。 そうでなければfalse
を返す。 IP空間が定義されていない場合はnil
を返す。.
Instance Method Summary collapse
-
#apply_filter? ⇒ Boolean
Jpmobile::Filter を適用するかどうか.
-
#apply_params_filter? ⇒ Boolean
Jpmobile::ParamsFilter を適用するかどうか.
- #content_transfer_encoding(headers) ⇒ Object
- #decode_transfer_encoding(body, charset) ⇒ Object
- #decoratable? ⇒ Boolean
- #decorated? ⇒ Boolean
- #default_charset ⇒ Object
-
#display ⇒ Object
画面情報を
Display
クラスのインスタンスで返す。. -
#ident ⇒ Object
契約者又は端末を識別する文字列があれば返す。.
-
#ident_device ⇒ Object
端末を識別する文字列があれば返す。.
-
#ident_subscriber ⇒ Object
契約者を識別する文字列があれば返す。.
-
#initialize(env, request) ⇒ AbstractMobile
constructor
A new instance of AbstractMobile.
- #mail_charset(charset = nil) ⇒ Object
- #mail_variants ⇒ Object
-
#position ⇒ Object
緯度経度があれば Position のインスタンスを返す。.
- #require_related_part? ⇒ Boolean
-
#smart_phone? ⇒ Boolean
smartphone かどうか.
-
#supports_cookie? ⇒ Boolean
クッキーをサポートしているか。.
-
#tablet? ⇒ Boolean
tablet かどうか.
- #to_external(str, content_type, charset) ⇒ Object
-
#to_internal(str) ⇒ Object
エンコーディング変換用.
- #to_mail_body(str) ⇒ Object
- #to_mail_body_encoded?(str) ⇒ Boolean
- #to_mail_encoding(str) ⇒ Object
- #to_mail_internal(str, charset) ⇒ Object
-
#to_mail_subject(str) ⇒ Object
メール送信用.
- #to_mail_subject_encoded?(str) ⇒ Boolean
- #utf8_to_mail_encode(str) ⇒ Object
- #valid_ip? ⇒ Boolean
-
#variants ⇒ Object
for view selector.
Constructor Details
#initialize(env, request) ⇒ AbstractMobile
Returns a new instance of AbstractMobile.
16 17 18 19 20 21 22 |
# File 'lib/jpmobile/mobile/abstract_mobile.rb', line 16 def initialize(env, request) @env = env @request = request @_variants = nil @_mail_variants = nil @decorated = nil end |
Instance Attribute Details
#decorated=(value) ⇒ Object (writeonly)
Sets the attribute decorated
201 202 203 |
# File 'lib/jpmobile/mobile/abstract_mobile.rb', line 201 def decorated=(value) @decorated = value end |
Class Method Details
.add_user_agent_regexp(regexp) ⇒ Object
228 229 230 |
# File 'lib/jpmobile/mobile/abstract_mobile.rb', line 228 def add_user_agent_regexp(regexp) @_user_agent_regexp = Regexp.union(user_agent_regexp, regexp) end |
.carrier(env) ⇒ Object
232 233 234 235 236 237 238 239 240 241 242 |
# File 'lib/jpmobile/mobile/abstract_mobile.rb', line 232 def carrier(env) ::Jpmobile::Mobile.carriers.each do |const| c = ::Jpmobile::Mobile.const_get(const) if c.check_carrier(env) res = ::Rack::Request.new(env) return c.new(env, res) end end nil end |
.check_carrier(env) ⇒ Object
リクエストがこのクラスに属するか調べるメソッド名に関して非常に不安
220 221 222 |
# File 'lib/jpmobile/mobile/abstract_mobile.rb', line 220 def check_carrier(env) user_agent_regexp && user_agent_regexp.match(env['HTTP_USER_AGENT']) end |
.ip_address_class ⇒ Object
244 245 246 247 248 |
# File 'lib/jpmobile/mobile/abstract_mobile.rb', line 244 def ip_address_class Object.const_get("::Jpmobile::Mobile::IpAddresses::#{self.to_s.split("::").last}").new rescue nil end |
.user_agent_regexp ⇒ Object
224 225 226 |
# File 'lib/jpmobile/mobile/abstract_mobile.rb', line 224 def user_agent_regexp @_user_agent_regexp ||= self::USER_AGENT_REGEXP end |
.valid_ip?(remote_addr) ⇒ Boolean
当該キャリアのIPアドレス帯域からのアクセスであれば true
を返す。そうでなければ false
を返す。IP空間が定義されていない場合は nil
を返す。
211 212 213 214 215 216 |
# File 'lib/jpmobile/mobile/abstract_mobile.rb', line 211 def valid_ip?(remote_addr) @ip_list ||= ip_address_class return false unless @ip_list @ip_list.valid_ip?(remote_addr) end |
Instance Method Details
#apply_filter? ⇒ Boolean
Jpmobile::Filter を適用するかどうか
71 72 73 |
# File 'lib/jpmobile/mobile/abstract_mobile.rb', line 71 def apply_filter? true end |
#apply_params_filter? ⇒ Boolean
Jpmobile::ParamsFilter を適用するかどうか
76 77 78 |
# File 'lib/jpmobile/mobile/abstract_mobile.rb', line 76 def apply_params_filter? true end |
#content_transfer_encoding(headers) ⇒ Object
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 |
# File 'lib/jpmobile/mobile/abstract_mobile.rb', line 142 def content_transfer_encoding(headers) transfer_encoding = headers['Content-Transfer-Encoding'] case headers['Content-Type'].to_s when %r{text/plain} (transfer_encoding.to_s == MAIL_CONTENT_TRANSFER_ENCODING) ? transfer_encoding : MAIL_CONTENT_TRANSFER_ENCODING when %r{text/html} if self.decorated? 'quoted-printable' else (transfer_encoding.to_s == MAIL_CONTENT_TRANSFER_ENCODING) ? transfer_encoding : MAIL_CONTENT_TRANSFER_ENCODING end else transfer_encoding end end |
#decode_transfer_encoding(body, charset) ⇒ Object
187 188 189 190 191 |
# File 'lib/jpmobile/mobile/abstract_mobile.rb', line 187 def decode_transfer_encoding(body, charset) body = Jpmobile::Util.set_encoding(body, charset) body = to_mail_internal(body, nil) Jpmobile::Util.force_encode(body, charset, Jpmobile::Util::UTF8) end |
#decoratable? ⇒ Boolean
193 194 195 |
# File 'lib/jpmobile/mobile/abstract_mobile.rb', line 193 def decoratable? false end |
#decorated? ⇒ Boolean
203 204 205 |
# File 'lib/jpmobile/mobile/abstract_mobile.rb', line 203 def decorated? @decorated end |
#default_charset ⇒ Object
89 90 91 |
# File 'lib/jpmobile/mobile/abstract_mobile.rb', line 89 def default_charset 'UTF-8' end |
#display ⇒ Object
画面情報を Display
クラスのインスタンスで返す。
49 50 51 52 53 |
# File 'lib/jpmobile/mobile/abstract_mobile.rb', line 49 def display @__displlay ||= Jpmobile::Mobile::Terminfo.new(self, @env) rescue LoadError puts 'display method require jpmobile-terminfo plugin.' end |
#ident ⇒ Object
契約者又は端末を識別する文字列があれば返す。
30 31 32 |
# File 'lib/jpmobile/mobile/abstract_mobile.rb', line 30 def ident ident_subscriber || ident_device end |
#ident_device ⇒ Object
端末を識別する文字列があれば返す。
40 41 42 |
# File 'lib/jpmobile/mobile/abstract_mobile.rb', line 40 def ident_device nil end |
#ident_subscriber ⇒ Object
契約者を識別する文字列があれば返す。
35 36 37 |
# File 'lib/jpmobile/mobile/abstract_mobile.rb', line 35 def ident_subscriber nil end |
#mail_charset(charset = nil) ⇒ Object
136 137 138 139 140 |
# File 'lib/jpmobile/mobile/abstract_mobile.rb', line 136 def mail_charset(charset = nil) # (charset.nil? or charset == "") ? self.class::MAIL_CHARSET : charset # self.class::MAIL_CHARSET (charset.nil? || charset == '' || charset =~ /US-ASCII/i) ? self.class::MAIL_CHARSET : charset end |
#mail_variants ⇒ Object
119 120 121 122 123 |
# File 'lib/jpmobile/mobile/abstract_mobile.rb', line 119 def mail_variants return @_mail_variants if @_mail_variants @_mail_variants = (variants == ['mobile']) ? [] : variants end |
#position ⇒ Object
緯度経度があれば Position のインスタンスを返す。
25 26 27 |
# File 'lib/jpmobile/mobile/abstract_mobile.rb', line 25 def position nil end |
#require_related_part? ⇒ Boolean
197 198 199 |
# File 'lib/jpmobile/mobile/abstract_mobile.rb', line 197 def false end |
#smart_phone? ⇒ Boolean
smartphone かどうか
61 62 63 |
# File 'lib/jpmobile/mobile/abstract_mobile.rb', line 61 def smart_phone? false end |
#supports_cookie? ⇒ Boolean
クッキーをサポートしているか。
56 57 58 |
# File 'lib/jpmobile/mobile/abstract_mobile.rb', line 56 def false end |
#tablet? ⇒ Boolean
tablet かどうか
66 67 68 |
# File 'lib/jpmobile/mobile/abstract_mobile.rb', line 66 def tablet? false end |
#to_external(str, content_type, charset) ⇒ Object
85 86 87 |
# File 'lib/jpmobile/mobile/abstract_mobile.rb', line 85 def to_external(str, content_type, charset) [str, charset] end |
#to_internal(str) ⇒ Object
エンコーディング変換用
81 82 83 |
# File 'lib/jpmobile/mobile/abstract_mobile.rb', line 81 def to_internal(str) str end |
#to_mail_body(str) ⇒ Object
132 133 134 |
# File 'lib/jpmobile/mobile/abstract_mobile.rb', line 132 def to_mail_body(str) to_mail_encoding(str) end |
#to_mail_body_encoded?(str) ⇒ Boolean
183 184 185 |
# File 'lib/jpmobile/mobile/abstract_mobile.rb', line 183 def to_mail_body_encoded?(str) Jpmobile::Util.jis?(str) end |
#to_mail_encoding(str) ⇒ Object
158 159 160 161 162 |
# File 'lib/jpmobile/mobile/abstract_mobile.rb', line 158 def to_mail_encoding(str) str = Jpmobile::Emoticon.utf8_to_unicodecr(str) str = Jpmobile::Emoticon.unicodecr_to_external(str, Jpmobile::Emoticon::CONVERSION_TABLE_TO_PC_EMAIL, false) Jpmobile::Util.encode(str, mail_charset) end |
#to_mail_internal(str, charset) ⇒ Object
175 176 177 |
# File 'lib/jpmobile/mobile/abstract_mobile.rb', line 175 def to_mail_internal(str, charset) str end |
#to_mail_subject(str) ⇒ Object
メール送信用
126 127 128 129 130 |
# File 'lib/jpmobile/mobile/abstract_mobile.rb', line 126 def to_mail_subject(str) Jpmobile::Util.fold_text(Jpmobile::Emoticon.unicodecr_to_utf8(str)). map {|text| "=?#{mail_charset}?B?" + [to_mail_encoding(text)].pack('m').delete("\n") + '?=' }. join("\n\s") end |
#to_mail_subject_encoded?(str) ⇒ Boolean
179 180 181 |
# File 'lib/jpmobile/mobile/abstract_mobile.rb', line 179 def to_mail_subject_encoded?(str) str.match(/=\?#{mail_charset}\?B.+\?=/i) end |
#utf8_to_mail_encode(str) ⇒ Object
164 165 166 167 168 169 170 171 172 173 |
# File 'lib/jpmobile/mobile/abstract_mobile.rb', line 164 def utf8_to_mail_encode(str) case mail_charset when /ISO-2022-JP/i Jpmobile::Util.utf8_to_jis(str) when /Shift_JIS/i Jpmobile::Util.utf8_to_sjis(str) else str end end |
#valid_ip? ⇒ Boolean
44 45 46 |
# File 'lib/jpmobile/mobile/abstract_mobile.rb', line 44 def valid_ip? @__valid_ip ||= self.class.valid_ip? @request.ip end |
#variants ⇒ Object
for view selector
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 |
# File 'lib/jpmobile/mobile/abstract_mobile.rb', line 94 def variants return @_variants if @_variants @_variants = self.class.ancestors.select {|c| c.to_s =~ /^Jpmobile/ && c.to_s !~ /Emoticon/ }.map do |klass| klass = klass.to_s. gsub('Jpmobile::', ''). gsub('AbstractMobile::', ''). gsub('Mobile::SmartPhone', 'smart_phone'). gsub('Mobile::Tablet', 'tablet'). gsub('::', '_'). gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2'). gsub(/([a-z\d])([A-Z])/, '\1_\2'). downcase (klass =~ /abstract/) ? 'mobile' : klass end if @_variants.include?('tablet') @_variants = @_variants.reject {|v| v == 'mobile' }.map {|v| v.gsub('mobile_', 'tablet_') } elsif @_variants.include?('smart_phone') @_variants = @_variants.reject {|v| v == 'mobile' }.map {|v| v.gsub('mobile_', 'smart_phone_') } end @_variants || [] end |