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

- (AbstractMobile) initialize(env, request)

Returns a new instance of AbstractMobile



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

def initialize(env, request)
  @env     = env
  @request = request
end

Class Method Details

+ (Object) add_user_agent_regexp(regexp)



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

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

+ (Object) carrier(env)



206
207
208
209
210
211
212
213
214
215
216
# File 'lib/jpmobile/mobile/abstract_mobile.rb', line 206

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

+ (Object) check_carrier(env)

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



194
195
196
# File 'lib/jpmobile/mobile/abstract_mobile.rb', line 194

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

+ (Object) ip_address_class



219
220
221
# File 'lib/jpmobile/mobile/abstract_mobile.rb', line 219

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

+ (Object) user_agent_regexp



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

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

+ (Boolean) valid_ip?(remote_addr)

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

Returns:

  • (Boolean)


185
186
187
188
189
190
# File 'lib/jpmobile/mobile/abstract_mobile.rb', line 185

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

- (Boolean) apply_filter?

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

Returns:

  • (Boolean)


59
60
61
# File 'lib/jpmobile/mobile/abstract_mobile.rb', line 59

def apply_filter?
  true
end

- (Boolean) apply_params_filter?

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

Returns:

  • (Boolean)


64
65
66
# File 'lib/jpmobile/mobile/abstract_mobile.rb', line 64

def apply_params_filter?
  true
end

- (Object) content_transfer_encoding(headers)



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

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

- (Object) decode_transfer_encoding(body, charset)



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

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

- (Boolean) decoratable?

Returns:

  • (Boolean)


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

def decoratable?
  false
end

- (Object) decorated=(boolean)



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

def decorated=(boolean)
  @decorated = boolean
end

- (Boolean) decorated?

Returns:

  • (Boolean)


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

def decorated?
  @decorated
end

- (Object) default_charset



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

def default_charset
  "UTF-8"
end

- (Object) display

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



37
38
39
40
41
# File 'lib/jpmobile/mobile/abstract_mobile.rb', line 37

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

- (Object) ident

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



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

def ident; ident_subscriber || ident_device; end

- (Object) ident_device

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



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

def ident_device; nil; end

- (Object) ident_subscriber

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



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

def ident_subscriber; nil; end

- (Object) mail_charset(charset = nil)



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

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

- (Object) mail_variants



104
105
106
107
108
# File 'lib/jpmobile/mobile/abstract_mobile.rb', line 104

def mail_variants
  return @_mail_variants if @_mail_variants

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

- (Object) position

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



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

def position; return nil; end

Returns:

  • (Boolean)


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

def require_related_part?
  false
end

- (Boolean) smart_phone?

smartphone かどうか

Returns:

  • (Boolean)


49
50
51
# File 'lib/jpmobile/mobile/abstract_mobile.rb', line 49

def smart_phone?
  false
end

- (Boolean) supports_cookie?

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

Returns:

  • (Boolean)


44
45
46
# File 'lib/jpmobile/mobile/abstract_mobile.rb', line 44

def supports_cookie?
  return false
end

- (Boolean) tablet?

tablet かどうか

Returns:

  • (Boolean)


54
55
56
# File 'lib/jpmobile/mobile/abstract_mobile.rb', line 54

def tablet?
  false
end

- (Object) to_external(str, content_type, charset)



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

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

- (Object) to_internal(str)

エンコーディング変換用



69
70
71
# File 'lib/jpmobile/mobile/abstract_mobile.rb', line 69

def to_internal(str)
  str
end

- (Object) to_mail_body(str)



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

def to_mail_body(str)
  to_mail_encoding(str)
end

- (Boolean) to_mail_body_encoded?(str)

Returns:

  • (Boolean)


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

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

- (Object) to_mail_encoding(str)



139
140
141
142
143
# File 'lib/jpmobile/mobile/abstract_mobile.rb', line 139

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

- (Object) to_mail_internal(str, charset)



154
155
156
# File 'lib/jpmobile/mobile/abstract_mobile.rb', line 154

def to_mail_internal(str, charset)
  str
end

- (Object) to_mail_subject(str)

メール送信用



111
112
113
114
115
# File 'lib/jpmobile/mobile/abstract_mobile.rb', line 111

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

- (Boolean) to_mail_subject_encoded?(str)

Returns:

  • (Boolean)


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

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

- (Object) utf8_to_mail_encode(str)



144
145
146
147
148
149
150
151
152
153
# File 'lib/jpmobile/mobile/abstract_mobile.rb', line 144

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

- (Boolean) valid_ip?

Returns:

  • (Boolean)


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

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

- (Object) variants

for view selector



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

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