Tuesday, December 30, 2008

Using line - wrapping after specified limit.

Following method is useful to display line with wrapping after specified limit. So that if you give a limit to 50, then it will insert '
' after 50 characters. Also, it will not break line from middle of any word. So after using this method, you can provide justification method to display text block properly.

Just pass a paragraph to display as 'par' and also provide a limit as 'length'.

def line_wrapping_after_limit(par, length)
    b = []
    res = []
    if par.length > length
      a = par.split(' ')
      len = 0
      0.upto(a.length-1) do |i|
        temp = a[i]
        while(temp.length > length)
          res << (temp[0, length-1] + "-").to_s
          temp = temp[length-1, a[i].length]
        end
        b << (temp + " ")
        len += temp.length + 1
        tot_len = i.eql?(a.length-1) ? len : len + a[i+1].length
        if(tot_len > length)
          b.join('')
          res << (b.to_s)
          len = 0
          b = []
        end
      end
      b.join('')
      res << (b.to_s)
      res.join("<\br/>

")

    else
      par
    end
  end

Thursday, December 25, 2008

Fetching photos from facebook in rails application

If you want to get all photos from each album of any particular facebook user, then try the following code. I have used it and it works in my application. Hope that u can also enjoy it......

## Just initialize two arrays ## 

@albums = []
@fbphotos = []

## Get the facebook user information who is logged in ## 

@fbuser = fbsession.users_getInfo(:uids => fbsession.session_user_id, :fields =>     ["first_name", "last_name", "pic_big"])

## Then collect all album information providing his/her facebook user_id ##

@fbalbums = fbsession.photos_getAlbums(:uid => @fbuser.uid)
        
doc = Hpricot::XML(@fbalbums.to_s)

(doc/:album).each do |a|
          aid = a.at('aid').innerHTML
          @albums <<>
end
@albums.each do |a|
          @fbphotos << uid =""> @fbuser.uid, :aid => a)
end

## Thus, finally you will get all the photos stored in @fbphotos from each album... If u want photos for any particular album, then not need to write entire code, just  use fbsession.photos_get method with passing parameter :aid i.e album_id...

Integrating ruby on rails appliation with facebook

I have created one application which directly integrates with facebook and uses facebook data in ror application.

To implement it, you have to just log in to facebook and from 'Developer' application, you have to add one application. Then facebook will provide you an api_key and secret_key.

You should also have rfacebook gem installed before creating ror application. After installing that gem, you will find 'facebook.yml' file in application's /config folder. In that file, you can insert api_key and secret_key provided from facebook.

Then you can integrate your ror application with facebook. For more help on functions, visit 


Wednesday, December 24, 2008

To resolve problem of rounding-off numbers

Following code is used to rounding-off any floating numbers with specified decimal places. There is also helper method provided by rails to do the same which is "number_with_precision". But if we want to rounding-off the floating number in controller, then following one-liner code may be helpful for that case.

# Specify your number to be round-off instead of FLOATING_NUMBER and how many decimal places you want instead of i.

(FLOATING_NUMBER * 10**i).round.to_f / 10**i