Every repository with this icon (
Every repository with this icon (
| name | age | message | |
|---|---|---|---|
| |
.gitignore | Thu Oct 09 07:34:24 -0700 2008 | |
| |
CHANGELOG | Fri Sep 26 00:02:48 -0700 2008 | |
| |
MIT-LICENSE | Sun Oct 29 12:45:18 -0800 2006 | |
| |
README.rdoc | Thu Apr 23 21:12:56 -0700 2009 | |
| |
Rakefile | Fri May 01 20:03:01 -0700 2009 | |
| |
init.rb | Sun Oct 29 12:45:18 -0800 2006 | |
| |
install.rb | Sun Oct 29 12:45:18 -0800 2006 | |
| |
lib/ | Mon May 11 14:21:36 -0700 2009 | |
| |
spec/ | Fri May 01 20:03:01 -0700 2009 | |
| |
tasks/ | Fri May 01 20:03:01 -0700 2009 | |
| |
test/ | ||
| |
tools/ | Mon May 11 14:21:36 -0700 2009 |
jpmobile: A Rails plugin for Japanese mobile-phones
jpmobileとは
携帯電話特有の機能を Rails で利用するためのプラグイン。 以下の機能を備える。
- 携帯電話のキャリア判別
- 端末位置情報の取得
- 端末製造番号、契約者番号等の取得
- IPアドレスの検証(キャリアが公開しているIPアドレス帯域からのアクセスか判定)
- セッションIDをフォーム/リンクに付与(Trans SID)
- 携帯電話ビューへの自動振分け
- ディスプレイ情報(画面サイズ、ブラウザ画面サイズ、カラー・白黒、色数)の取得
- GeoKit(geokit.rubyforge.org)との連携
- 文字コード変換機能/絵文字のキャリア間相互変換
インストール
pluginとしてインストールする場合
リリース版:
% ./script/plugin install git://github.com/darashi/jpmobile.git -r 'tag x.x.x' (x.x.xはバージョン)
開発版:
% ./script/plugin install git://github.com/darashi/jpmobile.git
gemでインストールする場合
# gem install jpmobile
としてgemをインストールした後
RAILS_ROOT/config/environment.rb の Rails::Initializer.run do |config| 〜 end 内に
config.gem "jpmobile"
の行を追加する。
使用例
携帯電話の識別
Viewの中で一部を切替える例
<% if request.mobile? %>
携帯電話からのアクセスです。
<% else %>
携帯電話からのアクセスではありません。
<% end %>
別に用意した携帯電話用コントローラへリダイレクトする例
class PcController < ApplicationController
before_filter :redirect_if_mobile
def index
end
private
def redirect_if_mobile
if request.mobile?
pa = params.dup
pa[:controller] = "/mobile"
redirect_to pa
end
end
end
class MobileController < ApplicationController
mobile_filter
end
携帯電話viewの自動振分け
DoCoMo携帯電話からアクセスすると、
- index_mobile_docomo.html.erb
- index_mobile.html.erb
- index.html.erb
の順でテンプレートを検索し、最初に見付かったテンプレートが利用される。 Auの場合は、index_mobile_au.html.erb、Softbankの場合はindex_mobile_softbank.html.erbが最初に検索される。
BUG: 現状、上記の例では index.html.erb が存在しない場合に振り分けが行われない(ダミーファイルを置くことで回避可能)。
キャリアの識別
case request.mobile
when Jpmobile::Mobile::Docomo
# for DoCoMo
when Jpmobile::Mobile::Au
# for au
when Jpmobile::Mobile::Softbank
# for SoftBank
when Jpmobile::Mobile::Willcom
# for Willcom
when Jpmobile::Mobile::Emobile
# for EMOBILE
else
# for PC
end
あるいは
if request.mobile.is_a?(Jpmobile::Mobile::Docomo)
# for DoCoMo
end
位置情報の取得
取得用リンクの生成
以下のようなコードで、端末に位置情報を要求するリンクを出力する。
<%= get_position_link_to(:action=>:gps) %>
位置情報の取得
class MobileController < ApplicationController
def gps
if request.mobile && pos = request.mobile.position
@latitude = pos.lat
@longuitude = pos.lon
end
end
end
端末情報の取得
端末側から通知されている場合、request.mobile.ident で 契約に固有の識別子もしくは端末の製造番号を取得できる。 両方存在する場合は契約に固有のIDが優先される。
- 契約に固有のID (request.mobile.ident_subscriber)
- au: EZ番号(サブスクライバ番号)
- DoCoMo: FOMAカード製造番号
- EMOBILE: EMnet対応端末から通知されるユニークなユーザID
- 端末製造番号 (request.mobile.ident_device)
- DoCoMo: 端末製造番号(FOMA, MOVA)
- SoftBank: 製造番号
IPの検証
キャリアが公開しているIPアドレス帯域からのアクセスか判定する。
request.mobile.valid_ip?
セッションIDの付与(Trans SID)
Cookie非対応携帯だけに付与する
class MyController
trans_sid
end
PCにも付与する
class MyController
trans_sid :always
end
trans_sid 機能を使う場合には cookie session store を使用することができません。 trans_sid を使用する際には、例えば environment.rb で
config.action_controller.session_store = :active_record_store
として active_record_store を使用します。 このとき ApplicationController において protect_from_forgery の :secret を指定するか、 あるいは protect_from_forgery を解除する必要があるでしょう。
端末の画面サイズ
request.mobile.display で Jpmobile::Display クラスのインスタンスが返る。
画面幅 <%= request.mobile.display.width %> 画面高さ <%= request.mobile.display.height %>
GeoKit(geokit.rubyforge.org)との連携
vandor/plugins/geokit以下にGeoKitがインストールされていると、Jpmobile::PositionにGeoKit::Mappableがincludeされる。したがって、
request.mobile.position.distance_to('札幌駅')
とすることで、端末と「札幌駅」との距離を求めることができる。詳細は geokit.rubyforge.org/api/index.html 参照。
文字コード変換機能/絵文字のキャリア間相互変換
JpmobileではControllerにmobile_filterを指定することで DoCoMo、Au、SoftBankの絵文字を透過的に扱うことができる。
class MyController mobile_filter end
また、半角・全角の自動変換を用いる場合は
class MyController mobile_filter :hankaku=>true end
と指定する。
Jpmobile内では、各キャリアの絵文字はUnicode私的領域上にマッピングされ、管理される。 このとき、DoCoMo、Auは公式サイト記載のマッピングが使用される。 ただしSoftBankはAuとの重複を避けるため、公式のマッピングに0x1000加算しU+F001以降に割り当てる。 テンプレート内ではUTF-8でエンコードするか、数値文字参照の&#xHHHH;形式で指定する。
絵文字は送出時に内蔵の変換表に基づいて変換され、携帯電話のエンコーディングにあわせて送出される。 携帯電話から受信した絵文字は上記マッピングに基づいてUTF-8でparamsに渡される。
mobile_filterを有効にすると以下の処理が自動で行われる。
- DoCoMo、Auとの通信時にはShift_JIS、SoftBankとの通信時にはUTF-8が使用される。
- :hankaku=>true指定時は、カタカナは半角カナに変換されて送出される。携帯電話から送られた半角カナは全角カナに変換される。
- 絵文字はキャリアにあわせて変換されて送出される。
- 携帯電話からの絵文字はUnicode私的領域にマップされ、UTF-8でparamsに格納される。
テストに必要なgemパッケージ
テストを実行するためには以下のgemパッケージが必要です。
- rails
- rack
- hpricot
- rspec
- rspec-rails
- spec-fixtures
リンク
- Project Website: jpmobile-rails.org
- RDoc Documentation: jpmobile.rubyforge.org/rdoc
- GitHub: github.com/darashi/jpmobile/
- RubyForge Project Page: rubyforge.org/projects/jpmobile
- Mailing List: groups.google.com/group/jpmobile
- IRC Channel #jpmobile@freenode.net
作者
Copyright 2006 © Yohji Shidara, under MIT License.
Yohji Shidara <dara@shidara.net>









