携帯アプリで固体識別番号を聞く

何がしたいのか

携帯向けのアプリケーションを使っていると、認証用などに、固体識別番号を取得したい場合がある。その際の処理。

今回は、Railsで携帯アプリを作る際に便利なプラグインとして「JpMobile」を使うことにする。

固体識別番号を取得するフォーム


固体識別番号は、auSoftBankは、利用者が「通知する」に設定していれば通信時に送信されてくるので、ヘッダからその部分を取り出せばよい。しかし、DoCoMoの場合は、一回一回「固体識別番号を送信してよいか」というダイアログが利用者側にでることになっている。
そのダイアログボックスを出すフォームは次のように書く。

<% form_tag({:action =>"send_mail", :user_id =>@user},{"utn"=>"utn"}) do -%>
  <%= text_field(:user,:name) %>
<% end -%>

これで3キャリアの固体識別番号を受け取る準備ができた。

エンジン側

固体識別番号を受け取るエンジン側のコードは例えば次のようになる。

def login
  send_ident?(request) do |ident|
    if @user = User.find_by_ident(ident)
      session[:user_id] = @user.id
      redirect_to :action => "mypage"
    else
      flash[:notice] = "登録されていません"
    end
  end
end

private

def send_ident?(request,error_action = "error",options = {})
  begin
    if ident = request.mobile.ident
      yield(ident)
    else
     raise <<-MSG
固体識別番号を確認できませんでした。
              MSG
    end
  rescue RuntimeError => err
    case error_action
    when "redirect"
      flash[:notice] = err.message
      redirect_to({:action => "index"}.merge(options))
    else
      error_handler(err.message == "Intentional Error" ? nil : err.message)
    end
  end
end