40歳からのキャリアチェンジ

20代はエンジニア・PM、30代はWeb系エンジニア向けのキャリアアドバイザー。40代の今はフリーランスで開発含めて色々やってます。技術ネタとしてはRuby/RailsとJavaScript関連あたり

Google Reader用のTitanium モジュールをJavaScriptで作り始めました

タイトルそのままですが、CommonJSなスタイルになれる勉強も兼ねて作り始めました。

前回のエントリ

assetsフォルダ配下に、com.twitter.h5y1m141.googleaccount.js作ってコードを書く。

ということを書いていましたが、上記のcom.twitter.h5y1m141.googleaccount.jsのソースはひとまずこんな感じ

//モジュール側
exports.login = function(email,password,func){
  var _authToken = null;
  var auth = null;
  var xhr = Ti.Network.createHTTPClient();
  var config = {
    email          : email,
    password       : password,
    login_param :{
	accountType: 'HOSTED_OR_GOOGLE',
	Email : email,
	Passwd : password,
	'Content-Type': "application/x-www-form-urlencoded",
	service        : "reader"
    }
  };
  xhr.open('POST','https://www.google.com/accounts/ClientLogin');
  xhr.onload = function(){
      //AuthKey取得した後にその値をHeaderにセットして認証処理を行う
      var body = xhr.responseText;
      var matches = body.match(/Auth=(.*)/);
      var results = matches[0].split('Auth=');
      var auth_token = results[1];
      if(this.status===200){
	var auth = "GoogleLogin auth=" + auth_token;
	func(auth);
      }else{
	//認証通らない場合の処理
      }
  };
  xhr.send(config.login_param);
};

exports.retreive = function(auth,param,func){
  var googleReaderAPI ={
    unread:'http://www.google.com/reader/api/0/unread-count',
    starred:'http://www.google.com/reader/atom/user/-/state/com.google/starred'
  };
  var xhr = Ti.Network.createHTTPClient();
  xhr.setRequestHeader("Authorization",auth);

  xhr.open('GET',googleReaderAPI[param]);
  xhr.onload = function(){
    var xml = this.responseXML;    
    func(xml);
  };
  xhr.send();
};

モジュールの動作確認

google
iOS Module Development GuideのTesting your Moduleの項目を見て知ったのですが、example/app.jsを編集すれば、比較的簡単にモジュールの動作が出来るみたいですね

ひとまず、こんな感じに修正しました

// TODO: write your module tests here
var googleaccount = require('com.twitter.h5y1m141.googleaccount');
Ti.API.info("module is => " + googleaccount);

googleaccount.login('YOUR-GMAIL-ACCOUNT','PASSWORD',
  function(auth){
    googleaccount.retreive(auth,'starred',
    function(xml){
      Ti.API.info('inside xml is:' + xml);
      return xml;
    });
  }
);

今後

他の言語での実装(例えばRubyGoogleReaderAPI)を参考にしながら、気が向いたらもう少し実装を進めてみようかと思います