jQuery.opensocial_simple Pluginを作成しました。

昨日OpenSocial Hackathonに参加させていただきました。agoです。

Hackathonは終わってしまいましたが、jQueryからOpenSocialを簡単に扱うためのPluginを作成したので、jQuery.opensocial_simpleとして公開したいと思います。

opensocialgadgetsのすべての機能を実装したわけではないですが、owner、viewerの情報取得、外部サイトへの接続はかなり簡単に実装できるようになったと思います。

使い方はjQuery読み込み後、jquery.opensocial_simple.jsを読み込み、$.opensocial_simpleからmethodを呼び出すだけ。

MySpaceアプリの場合以下の形で使用できます。

<script type=" text/javascript"http://api.msappspace.com/OpenSocial/JSExtensions/jquery/jquery-1.2.3.min.js"></script>
// jquery.opensocial_simple.min.jsの中身を展開

// http://example.com/の中身をtextで取得
$.opensocial_simple.get('http://example.com/', function (data) { console.log(data) });

// http://example.com/の中身をDOMで取得
$.opensocial_simple.getDOM('http://example.com/', function (data) { console.log(data) });

// http://example.com/rss.xmlの中身をFEEDで取得
$.opensocial_simple.getFEED('http://example.com/rss.xml', function (data) { console.log(data) });

// http://example.com/json.jsの中身をJSONで取得
$.opensocial_simple.getJSON('http://example.com/json.js', function (data) { console.log(data) });

// http://example.com/apiへkey=valの値をPOST
$.opensocial_simple.post('http://example.com/api', {'key':'val'}, function (data) { console.log(data) });

// OWNERとVIEWERの情報を取得
$.opensocial_simple.getPerson(function (result) { console.log(result) });

// OWNERとVIEWERの詳細情報を項目指定で取得
$.opensocial_simple.getPerson(['PROFILE_URL', 'ADDRESSES'...], function (result) { console.log(result) });

// OWNERとVIEWERの詳細情報をすべて取得
$.opensocial_simple.getPerson('all_field_set', function (result) { console.log(result) });

// OWNERとVIEWERの詳細情報でmixiがサポートしているものを取得
$.opensocial_simple.getPerson('mixi_field_set', function (result) { console.log(result) });

// opensocial-simpleで保存したOWNERのデータを取得
$.opensocial_simple.getOwnerData(function (data) { console.log(data) });

// opensocial-simpleで保存したVIEWERのデータを取得
$.opensocial_simple.getViewerData(function (data) { console.log(data) });

// 現在のVIEWERに情報を保持
$.opensocial_simple.postViewerData('data'/* or {'key' : 'data'} */, function () { console.log(arguments) } /* optional */);

// Activityの追加
$.opensocial_simple.postActivity('title strings'/* or {'TITLE' : 'title strings', 'BODY' : 'title strings'} */, 'HIGH' /* optional */, function () { console.log(arguments) } /* optional */);

// メッセージの取得
// 以下のtagを{ 'key' : 'value' }に変換したものを返します(gadget.xmlに含めてください)
    <script type="text/javascript" class="jQuery_opensocial_simple_locale">
    /*
        <Locale>
            <msg name="key">value</msg>
        </Locale>
    */
    </script>
$.opensocial_simple.getLocale('target selector'/* optional default '.jQuery_opensocial_simple_locale' */);

// opensocial-simpleで保存した全Friendsのデータを取得
$.opensocial_simple.getFriendsData(function () { console.log(arguments) });

// Ownerの全Friendsの情報を取得
$.opensocial_simple.getOwnerFriends(function () { console.log(arguments) });

// Viewerの全Friendsの情報を取得
$.opensocial_simple.getViewerFriends(function () { console.log(arguments) });

// gadgets.views.requestNavigateToで渡されたparamの取得
$.opensocial_simple.getParams();

// IDで指定された複数のユーザの情報を取得
$.opensocial_simple.getPersons(['domain:nnn', ...], function () { console.log(arguments) });

// 指定ユーザにmessageを送信
$.opensocial_simple.postMessage('target user id'/* or {'TARGET' : 'target user id', 'BODY' : 'body strings', 'TYPE' : 'EMAIL' or 'NOTIFICATION' or 'PRIVATE_MESSAGE' or 'PUBLIC_MESSAGE'} */, 'title' /* optional */, 'body' /* optional */, function () { console.log(arguments) } /* optional */);

Plugin自体はMIT、GPLのDual license、公開場所はCodeReposを使用させていただいているので、興味のある方はご自由に組み込み、修正を行ってください。

例によって以下のリンクからお試しいただくことが可能です。

MySpace内のtestアプリへ飛ぶ(MySpace内からhttp://www.google.co.jp/を読み込んでいます)

カヤックではOpenSocialに詳しい方や、MySpaceアプリ、Mixiアプリの開発経験のある技術者を募集しています!

(2008/12/22 「jQueryPluginはCamelCaseの方がいいのでは?」との指摘をいただいたので、$.opensocialSimpleでもアクセスできるよう修正しました。$.opensocial_simpleでも引き続きご利用いただけます)

(2009/01/27 getOwnerData、getViewerData、postViewerDataを追加いたしました)

(2009/02/10 postActivityを追加いたしました。また、このバージョンからOpenSocial Ver 0.8専用となります。Ver 0.7で使用する場合、旧バージョンをご利用ください)

(2009/04/27 getLocale、getFriendsData、getOwnerFriends、getViewerFriends、getParams、getPersons、postMessageを追加いたしました。また、mixiの開発が中心になっているためその他のコンテナでは動かないかもしれません)

(2009/08/04 getLocale、getPersonを拡張いたしました。第一引数に必要なfield名を指定するとcallback functionの引数に{fieldValue:{'PROFILE_URL':'http://...'}}といった形で変数に展開して渡します)