768x90 Getting Online Shouldn't be Tough- $7.49 .com Domains at Go Daddy

Create and Update Google Contact from Ruby without Batch

There's less information about ruby on rails method in google contact. I would like to share you about how to export new google contact / create new google contact from our table contacts through Ruby.

in my contacts_controller.rb, i wrote this script


class ContactsController < ApplicationController
require 'yaml'
require 'cgi'
before_filter :prepare_google_contact, :expect => [:create, :update]
CONTACTS_SCOPE = 'http://www.google.com/m8/feeds/'

def create
@contact = Contact.new(params[:contact])

respond_to do |format|
if @contact.save
feed = @google_contact.post( CONTACTS_SCOPE + 'contacts/default/base', google_contact(@contact)).to_xml
@contact.update_attribute(:google_contact_id, feed.elements['id'].text)
flash[:notice] = 'Contact was successfully created.'
format.html { redirect_to(@contact) }
format.xml { render :xml => @contact, :status => :created, :location => @contact }
format.fxml { render :fxml => @contact }
else
format.html { render :action => "new" }
format.xml { render :xml => @contact.errors, :status => :unprocessable_entity }
format.fxml { render :fxml => @contact.errors }
end
end
end

# PUT /contacts/1
# PUT /contacts/1.xml
# PUT /contacts/1.fxml
def update
@contact = Contact.find(params[:id])
@saved = @contact.update_attributes(params[:contact])

respond_to do |format|
if @saved
@google_contact.headers = {'If-Match'=>'*'}
feed = @google_contact.put(@contact.google_contact_id,
create_google_contact(@contact.reload())).to_xml
flash[:notice] = 'Contact was successfully updated.'
format.html { redirect_to(@contact) }
format.xml { head :ok }
format.fxml { render :fxml => @contact }
else
format.html { render :action => "edit" }
format.xml { render :xml => @contact.errors, :status => :unprocessable_entity }
format.fxml { render :fxml => @contact.errors }
end
end
end

private

def prepare_google_contact
contact_setup = YAML.load_file("#{RAILS_ROOT}/config/contact.yml")
contact_setup["google"].each { |key, value| instance_variable_set("@#{key}", value) }
@google_contact = GData::Client::Contacts.new({
:authsub_scope => 'http://www.google.com/m8/feeds/',
:source => 'google-DocListManager-v1.1',
:version => '3.0'})
session[:token_contact] = @google_contact.clientlogin(@email_contact, @password_contact)
@contact_entry = <<-EOF
<entry xmlns="http://www.w3.org/2005/Atom"
xmlns:contact="http://schemas.google.com/contact/2008"
xmlns:gd="http://schemas.google.com/g/2005">

<category term='http://schemas.google.com/contact/2008#contact'
scheme='http://schemas.google.com/g/2005#kind'/>
EOF

session[:token_contact] = @google_contact.clientlogin(@email_contact, @password_contact)
end

def google_contact(contact)
data = <<-EOF
#{@contact_entry}
<title>#{contact.first_name}</title>
<content>#{contact.note}</content>
<gd:name>
<gd:fullName>#{contact.first_name}</gd:fullName>
</gd:name>
<gContact:birthday when='#{contact.birth_day}'/>
<gd:email primary='true' rel='http://schemas.google.com/g/2005#home' address='#{contact.email}'/>
<gd:phoneNumber rel='http://schemas.google.com/g/2005#mobile'>#{contact.mobile}</gd:phoneNumber>
<gd:phoneNumber rel='http://schemas.google.com/g/2005#work'>#{contact.phone}</gd:phoneNumber>
<gd:structuredPostalAddress rel='http://schemas.google.com/g/2005#home'>
<gd:formattedAddress>#{contact.street}
#{contact.city}
#{contact.province}, #{contact.zipcode}
#{contact.country}
</gd:formattedAddress>
</gd:structuredPostalAddress>
</entry>
EOF
return data
end

end


in this study case I put my login google information in config/contact.yml file like this :


google:
email_contact: yacobus.r@kiranatama.com
password_contact: dec.07.1982


and here is my contact migration script lines :

class CreateContacts < ActiveRecord::Migration
def self.up
create_table :contacts do |t|
t.string :first_name
t.string :middle_name
t.string :last_name
t.string :phone
t.string :mobile
t.string :email
t.string :street
t.string :province
t.string :zipcode
t.string :city
t.string :country
t.string :note
t.string :type
t.string :google_contact_id
t.timestamps
end
end

def self.down
drop_table :contacts
end
end


and of course you have to install gem install gdata

Do not confuse about "format.fxml" this is respond data type for Restfulx (Flex/Air Framework).

Simple right !! but when this written's published, google still not release how to create and update trough ruby on rails. And many developer stucked because google provided create and update contact trough Batch method.

Basic Authentication in Flex or Air

After discussing about AuthSub and ClientLogin in Google in Flex or Air, now I would like to step forward about Basic authentication in Flex or Air. To know more about Basic Authentication please browse to this link https://mail.google.com/mail/feed/atom/all , you will be asked by pop up window to enter your login information right? now let we see how to handle it :



<mx:VBox
xmlns:mx = "http://www.adobe.com/2006/mxml"
width = "100%"
height = "100%"
verticalAlign = "middle"
horizontalAlign = "center"
backgroundColor = "#364a59"
creationComplete= "{creationHandler()}"
>

<mx:Script>
<![CDATA[

import mx.rpc.events.FaultEvent;
import mx.rpc.events.ResultEvent;
import mx.rpc.http.mxml.HTTPService;
import mx.utils.*;

private function creationHandler():void{
gmailService.headers = {Authorization:"Basic " + basicAuth()};
gmailService.url = "https://mail.google.com/mail/feed/atom/all"
gmailService.contentType = "application/x-www-form-urlencoded"
gmailService.resultFormat = "e4x"
gmailService.addEventListener(ResultEvent.RESULT, feedSuccess);
gmailService.addEventListener(FaultEvent.FAULT, faultHandler);
gmailService.send(); //Update Here
}

private function basicAuth():String{
var encoder:Base64Encoder = new Base64Encoder();
encoder.insertNewLines = false;
encoder.encode("yacobus.r@kiranatama.com:Dec.07.1082"));
return String(encoder);
}

private function feedSuccess(event:ResultEvent):void{
//Do any logic for feed result here
trace(XML(event.result))
}


private function faultHandler(event:FaultEvent):void{

}

]]>
</mx:Script>

<mx:HTTPService id="gmailService" showBusyCursor="true"/>

</mx:VBox>


Perfect right!!

AuthSub Authentication on Flex or Air

There are 3 types of authentication on when you are accessing website nowdays, they are :

  1. AuthSub / ClientLogin authentication, this authentication uses credential login information and give back to you authentication token that will be used on header as security login token to make you available login in 24 hours.
  2. OAuth authentication, this authentication is using secret key and customer key, more secure than AuthSub, this kind authentication will use your token and secret token in header Authentication information to identify you are the valid user when accessing secure data or page.
  3. Basic Authentication, if you see pop up alert that ask you to enter username or password, that is Basic Authentication.
Now i would like to share you about how to make those authentication become autologin without you face on their login page. First let discuss AuthSub or ClientLogin :

Now, try access this page : http://docs.google.com/feeds/documents/private/full

You will see error like this Authorization required, Error 401. These scripts all you need to pass the authentication easily :

you can use or just a class of HTTPService.


<mx:VBox
xmlns:mx = "http://www.adobe.com/2006/mxml"
width = "100%"
height = "100%"
verticalAlign = "middle"
horizontalAlign = "center"
backgroundColor = "#364a59"
creationComplete= "{creationHandler()}"
>

<mx:Script>
<![CDATA[

import mx.rpc.events.FaultEvent;
import mx.rpc.events.ResultEvent;
import mx.rpc.http.mxml.HTTPService;
import mx.utils.*;

private function creationHandler():void{
var data:Object = new Object;
data.Email = "yacobus.r@kiranatama.com";
data.Passwd = "Dec.07.1082"
data.accountType = "GOOGLE";
data.service = "writely";
//ClientLogin Url : https://www.google.com/accounts/ClientLogin
//AuthSub Url : https://www.google.com/accounts/AuthSubRequest
gmailService.url = "https://www.google.com/accounts/ClientLogin";
gmailService.contentType = "application/x-www-form-urlencoded";
gmailService.method = "POST";
gmailService.request = data;
gmailService.addEventListener(ResultEvent.RESULT, successHandler);
gmailService.addEventListener(FaultEvent.FAULT, faultHandler);
gmailService.send();
}

private function successHandler(event:ResultEvent):void{
gmailService.removeEventListener(ResultEvent.RESULT, new Function);
var txtAuth:String = String(event.result);
var authPosition:int = txtAuth.search("Auth");
txtAuth = txtAuth.substring(authPosition);
txtAuth = txtAuth.replace("Auth=","");
authPosition = txtAuth.length;
authPosition = authPosition - 1;
txtAuth = txtAuth.substring(0,authPosition);

accessProtectedUrl(txtAuth);
}

private function accessProtectedUrl(txtAuth:String):void{

gmailService.headers = {Authorization:"GoogleLogin auth="+txtAuth};
gmailService.url = "http://docs.google.com/feeds/documents/private/full";
gmailService.method = "GET";
gmailService.addEventListener(ResultEvent.RESULT, feedSuccess);
gmailService.addEventListener(FaultEvent.FAULT, faultHandler);
}

private function feedSuccess(event:ResultEvent):void{
//Do any logic for feed result here
trace(XML(event.result))
}


private function faultHandler(event:FaulttEvent):void{

}


]]>
</mx:Script>

<mx:HTTPService id="gmailService" showBusyCursor="true"/>

</mx:VBox>


List of Google Apps Service : http://code.google.com/apis/base/faq_gdata.html#clientlogin

How to make Air Application become iPhone SDK

Adobe air is great and smooth desktop application, many developers of Air dream their application can be installed into iPhone SDK. Now that dream come true. Thanks to my friend auraAnar for this great information :

First thing you do is downloading editor Eclips (like Flex Builder) from openplug.com, once you have successfully download it, create new project, in this show case i would to create sample parking searcher based on google Map for iPhone SDK.


Preview builder application

  1. Create WindowedApplication example project name is parkingApp.mxml
  2. Design indexView for iPhone application, let's called it as indexView.mxml (click the link)
  3. For component you can use mx:xxx like Flex or Air, but for control you should use mob:xxx from openPlug. Take a look my textinput Here
  4. Here is the screenshot of iPhone Emulator :


Preview Emulator


Once you success compiling your script without error, you need XCode of Apple to compile your script to iPhone SDK, i am using this way to compile my script in XCode. Happy iPhoning.

PS: I've tried commiting my script to google codes but it seems the google SVN is under maintenance, once it works i will upload the sample codes. Here is the address:

svn checkout http://airiphone.googlecode.com/svn/trunk/

AutoLogin to google account in Air without Oauth

i believe a part number of you there want their application allow the user to enter google account credential information to access the page, right? It's like i were faced off in my project. So how we will do it ?

If you use webservice, here's the information you should send to air application :
Google Account Credential Information and CallBack Url (the next url you want go after login)
I believe to access protected google page it will asked thridparty login, right? Note: OAuth, AuthSub and ClientLogin will not allow you to remote interface specially page after login google. In this case i create a component which is called GoogleBrowser.mxml and here the codes:

 <?xml version="1.0" encoding="utf-8"?>
  

     <mx:Window xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" 
                   width="600" height="600" backgroundColor="#242424"
                   creationComplete="init()"  >

     <mx:Script>
      <![CDATA[
         private var googleUserName:String = "yacobus.r@kiranatama.com";
         private var googlePassword:String = "i_love_u";
         private var nextUrl:String = "http://docs.google.com/a/kiranatama.com/Doc?docid=0AWbobJ8BSfPlZGcyaGtiYmJfMjlkajlwc3hncw&hl=en";  

         private function init():void{
           browserStack.selectedIndex = 1;
           gdocHtml.addEventListener(Event.COMPLETE, onCompleteHandler);
           gdocHtml.location = nextUrl;
         }

   
         private function onCompleteHandler(event:Event):void{
           gdocHtml.removeEventListener(Event.COMPLETE, removeListenerHandler);
           var additionalScript:String = '<script type="text/javascript">\n'+
                                   "gaia_setFocus();\n" + 
                                   "document.gaia_loginform.submit();\n"+
                                   "</script>";
           var username:String = googleUserName.split("@")[0];
           var body:String = String(event.target.htmlLoader.window.document.body.innerHTML);
           body = body.replace('id="Passwd"','id="Passwd" value="'+googlePassword+'" ');
           body = body.replace('value="" class','value="'+username+'" class"');
           body = body + addScript;
           gdocHtml.htmlLoader.window.document.body.innerHTML = body;
           browserStack.selectedIndex = 0;
          }

           private function removeListenerHandler(event:Event):void{
              event.stopPropagation();
           }

        ]]>
      </mx:Script>

      <mx:ViewStack id="browserStack" width="100%" height="100%">
        <mx:Canvas width="100%" height="100%">
          <mx:HTML width="100%" height="100%" id="gdocHtml" />    
        </mx:Canvas>
        <mx:Text id="loadingText" text="Loading ..." />
      </mx:ViewStack>  
</mx:Window>

 
powered by Blogger