Coder Social home page Coder Social logo

jpmobile's Introduction

jpmobile: A Rails plugin for Japanese mobile-phones

jpmobileとは

携帯電話特有の機能を Rails で利用するためのプラグイン。以下の機能を備える。

  • 携帯電話のキャリア判別

  • 端末位置情報の取得

  • 端末製造番号、契約者番号等の取得

  • IPアドレスの検証(キャリアが公開しているIPアドレス帯域からのアクセスか判定)

  • セッションIDをフォーム/リンクに付与(Trans SID)

  • 携帯電話ビューへの自動振分け

  • ディスプレイ情報(画面サイズ、ブラウザ画面サイズ、カラー・白黒、色数)の取得

  • GeoKit(geokit.rubyforge.org)との連携

  • 文字コード変換機能/絵文字のキャリア間相互変換

インストール

Rails 2.3.2

pluginとしてインストールする場合

開発版:

% cd rails_app_root
% git clone git://github.com/darashi/jpmobile.git vendor/plugins/jpmobile
% cd vendor/plugins/jpmobile
% git pull origin rails-2.3.2
% rm -rf .git

gemでインストールする場合

未実装

Rails 2.2.x 以前

リリース版:

% ./script/plugin install git://github.com/darashi/jpmobile.git -r 'tag 0.0.5'

使用例

携帯電話の識別

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)

class MyController
  trans_sid
end

PCにも付与する

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 %>

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

リンク

作者

Copyright 2006 © Yohji Shidara, under MIT License.

Yohji Shidara <[email protected]>

d.hatena.ne.jp/darashi

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.