Class: Jpmobile::Mobile::AbstractMobile

Inherits:
Object
  • Object
show all
Defined in:
lib/jpmobile/mobile/abstract_mobile.rb

Overview

携帯電話の抽象クラス。

Direct Known Subclasses

Au, Docomo, Emobile, SmartPhone, Softbank, Willcom

Constant Summary

MAIL_CHARSET =

メールのデフォルトのcharset

"ISO-2022-JP"
USER_AGENT_REGEXP =

対応するuser-agentの正規表現

nil
MAIL_ADDRESS_REGEXP =

対応するメールアドレスの正規表現

nil
MAIL_CONTENT_TRANSFER_ENCODING =

テキスト部分の content-transfer-encoding

'7bit'

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(env, request) ⇒ AbstractMobile



10
11
12
13
14
15
16
# File 'lib/jpmobile/mobile/abstract_mobile.rb', line 10

def initialize(env, request)
  @env            = env
  @request        = request
  @_variants      = nil
  @_mail_variants = nil
  @decorated      = nil
end

Class Method Details

.add_user_agent_regexp(regexp) ⇒ Object



205
206
207
# File 'lib/jpmobile/mobile/abstract_mobile.rb', line 205

def add_user_agent_regexp(regexp)
  @_user_agent_regexp = Regexp.union(user_agent_regexp, regexp)
end

.carrier(env) ⇒ Object



209
210
211
212
213
214
215
216
217
218
219
# File 'lib/jpmobile/mobile/abstract_mobile.rb', line 209

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

リクエストがこのクラスに属するか調べる メソッド名に関して非常に不安



197
198
199
# File 'lib/jpmobile/mobile/abstract_mobile.rb', line 197

def check_carrier(env)
  user_agent_regexp && user_agent_regexp.match(env['HTTP_USER_AGENT'])
end

.ip_address_classObject



222
223
224
# File 'lib/jpmobile/mobile/abstract_mobile.rb', line 222

def ip_address_class
  eval("::Jpmobile::Mobile::IpAddresses::#{self.to_s.split(/::/).last}").new rescue nil
end

.user_agent_regexpObject



201
202
203
# File 'lib/jpmobile/mobile/abstract_mobile.rb', line 201

def user_agent_regexp
  @_user_agent_regexp ||= self::USER_AGENT_REGEXP
end

.valid_ip?(remote_addr) ⇒ Boolean

当該キャリアのIPアドレス帯域からのアクセスであれば true を返す。 そうでなければ false を返す。 IP空間が定義されていない場合は nil を返す。



188
189
190
191
192
193
# File 'lib/jpmobile/mobile/abstract_mobile.rb', line 188

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::Rack::Filter を適用するかどうか



62
63
64
# File 'lib/jpmobile/mobile/abstract_mobile.rb', line 62

def apply_filter?
  true
end

#apply_params_filter?Boolean

Jpmobile::Rack::ParamsFilter を適用するかどうか



67
68
69
# File 'lib/jpmobile/mobile/abstract_mobile.rb', line 67

def apply_params_filter?
  true
end

#content_transfer_encoding(headers) ⇒ Object



127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/jpmobile/mobile/abstract_mobile.rb', line 127

def content_transfer_encoding(headers)
  transfer_encoding = headers['Content-Transfer-Encoding']
  case headers['Content-Type'].to_s
  when /text\/plain/
    transfer_encoding.to_s == MAIL_CONTENT_TRANSFER_ENCODING ? transfer_encoding : MAIL_CONTENT_TRANSFER_ENCODING
  when /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



166
167
168
169
170
# File 'lib/jpmobile/mobile/abstract_mobile.rb', line 166

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



171
172
173
# File 'lib/jpmobile/mobile/abstract_mobile.rb', line 171

def decoratable?
  false
end

#decorated=(boolean) ⇒ Object



177
178
179
# File 'lib/jpmobile/mobile/abstract_mobile.rb', line 177

def decorated=(boolean)
  @decorated = boolean
end

#decorated?Boolean



180
181
182
# File 'lib/jpmobile/mobile/abstract_mobile.rb', line 180

def decorated?
  @decorated
end

#default_charsetObject



78
79
80
# File 'lib/jpmobile/mobile/abstract_mobile.rb', line 78

def default_charset
  "UTF-8"
end

#displayObject

画面情報を Display クラスのインスタンスで返す。



40
41
42
43
44
# File 'lib/jpmobile/mobile/abstract_mobile.rb', line 40

def display
  @__displlay ||= Jpmobile::Mobile::Terminfo.new(self, @env)
rescue LoadError
  puts "display method require jpmobile-terminfo plugin."
end

#identObject

契約者又は端末を識別する文字列があれば返す。



29
# File 'lib/jpmobile/mobile/abstract_mobile.rb', line 29

def ident; ident_subscriber || ident_device; end

#ident_deviceObject

端末を識別する文字列があれば返す。



33
# File 'lib/jpmobile/mobile/abstract_mobile.rb', line 33

def ident_device; nil; end

#ident_subscriberObject

契約者を識別する文字列があれば返す。



31
# File 'lib/jpmobile/mobile/abstract_mobile.rb', line 31

def ident_subscriber; nil; end

#mail_charset(charset = nil) ⇒ Object



122
123
124
125
126
# File 'lib/jpmobile/mobile/abstract_mobile.rb', line 122

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_variantsObject



107
108
109
110
111
# File 'lib/jpmobile/mobile/abstract_mobile.rb', line 107

def mail_variants
  return @_mail_variants if @_mail_variants

  @_mail_variants = variants == ['mobile'] ? [] : variants
end

#positionObject

緯度経度があれば Position のインスタンスを返す。



26
# File 'lib/jpmobile/mobile/abstract_mobile.rb', line 26

def position; return nil; end


174
175
176
# File 'lib/jpmobile/mobile/abstract_mobile.rb', line 174

def require_related_part?
  false
end

#smart_phone?Boolean

smartphone かどうか



52
53
54
# File 'lib/jpmobile/mobile/abstract_mobile.rb', line 52

def smart_phone?
  false
end

#supports_cookie?Boolean

クッキーをサポートしているか。



47
48
49
# File 'lib/jpmobile/mobile/abstract_mobile.rb', line 47

def supports_cookie?
  return false
end

#tablet?Boolean

tablet かどうか



57
58
59
# File 'lib/jpmobile/mobile/abstract_mobile.rb', line 57

def tablet?
  false
end

#to_external(str, content_type, charset) ⇒ Object



75
76
77
# File 'lib/jpmobile/mobile/abstract_mobile.rb', line 75

def to_external(str, content_type, charset)
  [str, charset]
end

#to_internal(str) ⇒ Object

エンコーディング変換用



72
73
74
# File 'lib/jpmobile/mobile/abstract_mobile.rb', line 72

def to_internal(str)
  str
end

#to_mail_body(str) ⇒ Object



119
120
121
# File 'lib/jpmobile/mobile/abstract_mobile.rb', line 119

def to_mail_body(str)
  to_mail_encoding(str)
end

#to_mail_body_encoded?(str) ⇒ Boolean



163
164
165
# File 'lib/jpmobile/mobile/abstract_mobile.rb', line 163

def to_mail_body_encoded?(str)
  Jpmobile::Util.jis?(str)
end

#to_mail_encoding(str) ⇒ Object



142
143
144
145
146
# File 'lib/jpmobile/mobile/abstract_mobile.rb', line 142

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



157
158
159
# File 'lib/jpmobile/mobile/abstract_mobile.rb', line 157

def to_mail_internal(str, charset)
  str
end

#to_mail_subject(str) ⇒ Object

メール送信用



114
115
116
117
118
# File 'lib/jpmobile/mobile/abstract_mobile.rb', line 114

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').gsub(/\n/, '') + "?=" }.
    join("\n\s")
end

#to_mail_subject_encoded?(str) ⇒ Boolean



160
161
162
# File 'lib/jpmobile/mobile/abstract_mobile.rb', line 160

def to_mail_subject_encoded?(str)
  str.match(/\=\?#{mail_charset}\?B.+\?\=/i)
end

#utf8_to_mail_encode(str) ⇒ Object



147
148
149
150
151
152
153
154
155
156
# File 'lib/jpmobile/mobile/abstract_mobile.rb', line 147

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



35
36
37
# File 'lib/jpmobile/mobile/abstract_mobile.rb', line 35

def valid_ip?
  @__valid_ip ||= self.class.valid_ip? @request.ip
end

#variantsObject

for view selector



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/jpmobile/mobile/abstract_mobile.rb', line 83

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