Path: | README.rdoc |
Last Update: | Wed Sep 23 01:42:09 +0000 2009 |
携帯電話特有の機能を Rails で利用するためのプラグイン。 以下の機能を備える。
Rails の Version に応じて選択してください。
リリース版:
% ./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 install jpmobile
としてgemをインストールした後
RAILS_ROOT/config/environment.rb の Rails::Initializer.run do |config| 〜 end 内に
config.gem "jpmobile"
の行を追加する。
リリース版:
% ./script/plugin install git://github.com/darashi/jpmobile.git -r 'tag 0.0.5'
<% 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
DoCoMo携帯電話からアクセスすると、
の順でテンプレートを検索し、最初に見付かったテンプレートが利用される。 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が優先される。
キャリアが公開しているIPアドレス帯域からのアクセスか判定する。
request.mobile.valid_ip?
class MyController trans_sid end
class MyController trans_sid :always end
trans_sid 機能を使う場合には cookie session store を使用することができません。 trans_sid を使用する際には、例えば config/initializers/session_store.rb で
ActionController::Base.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 %>
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を有効にすると以下の処理が自動で行われる。
テストを実行するためには以下のgemパッケージが必要です。
Copyright 2006 (c) Yohji Shidara, under MIT License.
Yohji Shidara <dara@shidara.net>