Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Uploading a file to Facebook using Koala on Ruby on Rails

I followed the following blogpost to figure out how to create Facebook events remotely using my app. I've been having problems loading the images from my app, however, because I do not have images stored locally on my app, they are stored in AWS.

    @graph = Koala::Facebook::GraphAPI.new(@token)  
    picture = Koala::UploadableIO.new(@event.photo.url(:small))
    params = {
        :picture => picture,
        :name => 'Event name',
        :description => 'Event descriptio
        :start_time => datetime,
       }

is the following code I am currently using to send pictures to Facebook when Facebook events are created on my app. The problem is, however, that Rails is throwing the error: No such file or directory - http://s3.amazonaws.com/ColumbiaEventsApp/photos/21/small.jpeg?1312521889.

Does anybody who's more experienced with Rails development know if there is a way for me to treat a URL like a path to a file? The UploadableIO class expects a path to a file, and I'm struggling to figure out if there's a way in Ruby to treat URL's like filepaths. The way that photos stored on the app can be loaded to Facebook is as follows:

picture = Koala::UploadableIO.new(File.open("PATH TO YOUR EVENT IMAGE"))

if that helps.

I appreciate any new insights into this issue.

like image 543
Sid Avatar asked Dec 07 '22 20:12

Sid


1 Answers

Ok so I played around and figured out how to post pictures.

Basically what I did was use the 'open-uri' library to convert the image links into file objects, which can then be passed to UploadableIO and sent to Facebook. This is the code that worked:

require 'open-uri'
OpenURI::Buffer.send :remove_const, 'StringMax' if OpenURI::Buffer.const_defined?('StringMax')
OpenURI::Buffer.const_set 'StringMax', 0
picture = Koala::UploadableIO.new(open(@event.photo.url(:small)).path, 'image')
params = {
  picture:     picture,
  name:        @event.name,
  description: @event.description,
  location:    @event.location,
  start_time:  datetime
}
@graph.put_object('me', 'events', params )

The OpenURI constant StringMax needed to be changed because the image files I was using were small enough that the files were being processed as Strings rather than File Objects.

Hope this helps anyone trying to fix this!

like image 189
Sid Avatar answered Mar 08 '23 09:03

Sid