Image Uploading to facebook and tagging friends

For uploading and tagging the friends on Facebook in Ruby on Rails , we need a gem named “fb-graph”. Which will provide the api call for this functionality.

# @Params : params
# @Return : None
# @Purpose : To tag user on the image with comment and upload it to facebook
def create

# find the image from the image_id
@image = Image.find_by_id(params[:image_id])

# fetch the user details from the facebook
me = FbGraph::User.me(current_user.auth_token)

#create the array of friends uid

friends = params[:friends].split(",")

tags = []

# looping through friends array

friends.each do |friend|

#creating a new tag object and add it to tag array

tags << FbGraph::Tag.new(
:id => friend,
:name => "with Facebook profile link",
: x => 2*Random.rand(9),
:y => 9*Random.rand(2)
)

end # end of friends block

# uploading the image the friends tag
FbGraph::User.me(current_user.auth_token).photo!(
:url => @image.picture.url ,
:message => params[:message],
:tags => tags
)

flash[:success] = "Successfully uploaded and tagged the your friends."

redirect_to image_path(@image)

end # end of create action

Also you can pass the source file to upload the image instead of image url using
FbGraph::User.me(ACCESS_TOKEN).photo!(
:source => File.new(File.join(File.dirname(__FILE__), 'fb_graph.png'), 'rb'),
:message => params[:message],
:tags => tags
)


For more information visit the site https://github.com/nov/fb_graph/

Also you can checkout my running application at http://warm-badlands-9447.herokuapp.com
Thanks for reading the post……. 🙂

Advertisements

One thought on “Image Uploading to facebook and tagging friends

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s