Wednesday, July 08, 2009

authenticity_token not available in Rails 2.X version for non forms

This problem occur  if view is not having any form ...
When a form is being generated rails automatically adds something like
this:
< input name="authenticity_token" type="hidden" value="11ff3908e6cd4be7b4041a93b783829ce6b12349" >  
If view does not contain form ("authenticity_token")  InvalidAuthenticityToken is being raised.
 
So You can do something like this in your view to make your authenticity 
token available to your javascript in your views.

<%= javascript_tag "window._token = '#{form_authenticity_token}'" %>

That will make your authenticity token available to your custom 
javascript Ajax requests.  If you're using prototype.js and you want to 
do a custom PUT, you do something like this.

  new Ajax.Request ('/products/1', {
    method: 'put',
    parameters: 'product[name]=chair&authenticity_token=' + 
window._token});
 
If you use Jquery :=
$.ajax({
              url: "people/1,
              global: false,
              type: "PUT",
              data: ({'person[name]' : 'hey',authenticity_token : window._token}),
              dataType: "html",
              success: function(msg){
                 //ur msg;
              }});

Thursday, June 11, 2009

MS Word processing or parsing using ruby and Apache POI

I am looking for MS word document processing to get text information using ruby. But could not find right gems in ruby. Some win32 ole libraries are there but will not work in linux. So thought about using Poi java api to process word document.

I hv downloded poi jar files from here.
http://www.apache.org/dyn/closer.cgi/poi/release/

changed the name of the directory to poi folder .

Check the following ruby script to get the parsed text .. it uses java interface code in WordSampleReader.java   this file should be compiled and should be available under poi folder where all jar files are there.

CONFIG = {}
class WordReader
  #include Config
  CONFIG['host'] = 'mswin32'
  def self.generate_text(filename)
   
    interface_classpath=Dir.getwd+"/poi"
  
    case CONFIG['host']
      when /mswin32/
        Dir.foreach("poi") do |file|
          interface_classpath << ";#{Dir.getwd}/poi/"+file if (file != '.' and file != '..' and file.match(/.jar/))
        end
        path = "java -cp \"#{interface_classpath}\" WordSampleReader "+filename
      else
        Dir.foreach(Dir.getwd+"/poi/") do |file|
          interface_classpath << ":#{Dir.getwd}/poi/"+file if (file != '.' and file != '..' and file.match(/.jar/)) 
        end
        path = "java -cp \"#{interface_classpath}\" WordSampleReader "+filename
      end
    result = ""
    IO.popen(path, "w+b" ) { |x| result= x.read }
    result
  end
end

puts reader = WordReader.generate_text('poi/test.doc')


Though i dont know java used sample code found in google..  The file called WordSampleReader.java.

The source code file is ...

//package com.informit.poi;

// Import POI classes
import org.apache.poi.poifs.filesystem.*;
import org.apache.poi.hwpf.*;
import org.apache.poi.hwpf.extractor.*;

// Import Java classes
import java.io.*;
import java.util.*;

public class WordSampleReader
{
  public static void main( String[] args )
  {
   if( args.length == 0 )
   {
     System.out.println( "Usage: WordSampleReader " );
     System.exit( 0 );
   }

   String filename = args[ 0 ];
   try
   {
     // Create a POI File System object; this is the main class for the POIFS file system
     // and it manages the entire lifecycle of the file system
     POIFSFileSystem fs = new POIFSFileSystem( new FileInputStream( filename ) );

     // Create a document for this file
     HWPFDocument doc = new HWPFDocument( fs );

     // Create a WordExtractor to read the text of the word document
     WordExtractor we = new WordExtractor( doc );
    
     // Extract all paragraphs in the document as strings
     String[] paragraphs = we.getParagraphText();

     // Output the document
     //System.out.println( "Word Document has " + paragraphs.length + " paragraphs" );
    // for( int i=0; i
     //{
      //System.out.println( paragraphs[ i ] );
     //}
    //  output text
      System.out.println( we.getText() );

   }
   catch( Exception e )
   {
     e.printStackTrace();
   }
  }
}

Monday, May 11, 2009

QuickBase autheticate ruby method bug in quickbase client library

For subsequent calls with the help of ruby quickbase api , it should consider the old token number.

Existing method :
def authenticate( username, password, hours = nil )

      @username, @password, @hours = username, password, hours

      if username and password

         @ticket = nil
         xmlRequestData = toXML( :hours, @hours ) if @hours
         sendRequest( :authenticate )
         @userid = getResponseValue( :userid )
         return self if @chainAPIcalls
         return @ticket, @userid

      elsif username or password
         raise "authenticate: missing username or password"
      elsif @ticket
         raise "authenticate: #{username} is already authenticated"
      end
   end

The above one missing token variable and which is very important for next or immediate calls.
so add this line in the above method.
@ticket = getResponseValue( :ticket )

Saturday, April 11, 2009

Import Gmail Contacts - Ruby on Rails

Tried with my own ruby script to get contact information from google but requires some more extra work to make request in secure way.

First, you need to register your domain with Google. This can be as easy as uploading a temporary file to your domain’s root directory to verify control of the domain. The steps for doing this are clearly outlined at Registration for Web-Based Applications.  

I find good  article to import google contact information. Use the same code but check the code for error free.. you should have same patience to make it work :-)

http://blog.guitarati.com/2008/08/google-accounts-authentication-using.html