Uploading a file to Facebook using Koala on Ruby on Rails - 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.

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!

With Koala 1.2.1 it's a very elegant solution. Here is sample code for creating an album and uploading to it from a remote, AWS link (btw this took about 30 lines in PHP w/ the PHP SDK!
#foo = Foo.find(params[:foo_id])
albuminfo = #graph.put_object('me','albums', :name=>#foo.title)
album_id = albuminfo["id"]
#graph.put_picture(#foo.remote_image_path,{}, album_id)

Facebook recently released an update that lets you post pictures using publicly accessible URLs (http://developers.facebook.com/blog/post/526/). The Koala library you're using supports that (https://github.com/arsduo/koala/blob/master/lib/koala/graph_api.rb#L102), so you should be able to post the pictures you're hosting on S3 without having to use OpenURI::Buffer.

For Facebook Ad Images, you unfortunately currently cannot do it by URL, thus:
require 'open-uri'
img_data = open(my_post.image.url :medium).read
img = graph.put_connections('act_X', 'adimages', bytes: Base64.encode64(img_data))

Related

Ruby on Rails ActionMailer Images Issue

I'm trying to use embedded images in my E-Mail, but if I try to load a file from my image folder as a header, it's not displaying in the email:
The image is an inline attachment:
def export_bill_email(user)
#user = user
attachments.inline["logo.jpg"] = File.read("#{Rails.root}/app/assets/images/bdh_caption_logo.jpg")
make_bootstrap_mail(
to: 'xxx',
from: 'xxx',
subject: 'xxx'
)
end
Then in my view, I try to call it the following way:
<%= image_tag(attachments["logo.jpg"].url, width: '100', height: '100') %>
Is it not displaying cause of development mode? I also tried forwarding with ngrok, but still, it's just displaying a broken image sign in my received email.
The strange thing is: If I use a direct hyperlink to an image, it is displayed so it has to do something with the image URL I guess, but I can't figure it out.
Thanks for any good advice!
Okay I got it working in production, I think the problem is that the image can't bei downloaded from localhost cause it's not publically accessible by the mail client. If I run it on my Webserver it's working fine.

Direct link (no redirect) to files in ActiveStorage

Using url_for() on a file stored in active storage returns a url that leads to the application and then redirects to the actual location. Because of a bug in firefox with CORS, the redirect breaks my application.
Is there any way to get the direct link to the file with ActiveStorage?
You can do this
record.active_storage_object.blob.service_url
Found here https://github.com/rails/rails/blob/master/activestorage/app/controllers/active_storage/blobs_controller.rb
I had to dig through the rails source to create this so I have no idea how recommended it is but this works for disk storage at least.
ActiveStorage::Current.host = "yourhostname"
attachment_blob = ActiveStorage::Attachment.find_by(record_type: "YourModel", record_id: record.id).blob
direct_url = ActiveStorage::Blob.service.url(
attachment_blob.key,
expires_in: 20000,
disposition: "attachment",
filename: attachment_blob.filename,
content_type: attachment_blob.content_type
)
For me, rails_blob_url(#blog.pdf) (if you're trying to get the file stored as #blog.pdf) worked best.

404 error when writing to a Shopify asset

I've been trying to upload an asset using the shopify_api gem. I made sure I have the appropriate OAuth2 scope (write_themes), and I have no problem reading and even destroying them. The problem is that I get a 404 error when attempting to create or update an asset.
Here's the request the gem is creating:
PUT: /admin/themes/3650318/assets.json [{"Content-Type"=>"application/json", "User-Agent"=>"ShopifyAPI/3.0.3 ActiveResource/4.0.0.beta1 Ruby/2.0.0", "X-Shopify-Access-Token"=>"ommitted"}] ({"key":"templates/index.liquid","attachment":"base64 attachment omitted"})
For reference, here is the code I've used to make the request (wrapped in a ShopifyAPI::Session, of course):
ShopifyAPI::Asset.create(key: 'snippets/test.liquid', attachment: some_base64_data, theme_id: 3650318)
Or:
asset = ShopifyAPI::Asset.new(key: 'snippets/test.liquid', attachment: baset64_data, theme_id: 3650318)
asset.save
Any ideas?
This works for me...
To upload to the published theme (no theme id is given)
a = ShopifyAPI::Asset.new
a.key = "assets/google.png"
a.src = "https://www.google.co.uk/images/srpr/logo11w.png"
a.save
or
ShopifyAPI::Asset.create(key: 'assets/google.png', src: "https://www.google.co.uk/images/srpr/logo11w.png")
To upload to a specific theme
a = ShopifyAPI::Asset.new
a.key = "assets/google.png"
a.src = "https://www.google.co.uk/images/srpr/logo11w.png"
a.prefix_options[:theme_id] = "6731537"
a.save
or
ShopifyAPI::Asset.create(key: 'assets/google.png', src: "https://www.google.co.uk/images/srpr/logo11w.png", theme_id: 6731537)
It seems quite late for the reply but i am answering this so that it will help other developers facing similar issue.
If you have setup shopify_app gem then you can access the asset API on rails by
#This will access the asset of live theme
#assets = ShopifyAPI::Asset.find(:all)
#or if you want to access the asset of particular theme.
#assets = ShopifyAPI::Asset.find(:all, params: {"theme_id": themeid})
You can find the detail explanation here

Using Koala, how can I attach an image when I publish to a user's stream?

I've started to play with the Koala gem for a RoR app. I've already got permission from the user to publish to their stream
After this line
graph = Koala::Facebook::GraphAPI.new(#facebook_cookies["access_token"])
to post to the stream, I can do a
graph.put_object("me", "feed", "I am writing to my wall")
The above works, but how do I include an image like http://example.com/foo.jpg as part of the update? I tried reading up the Stream Attachments but without a lot of luck. Does anyone have some sample code?
You can use something like:
options = {
:message => "I am writing to my wall",
:picture => "http://example.com/foo.jpg"
}
graph.put_object(facebook_uid, "feed", options)

Using MVC to talk directly to Amazon S3

I am writing an MVC 3 application that needs to allow the user to directly upload a file to S3. I also need to show a progress bar. All of the examples I have seen are PHP or Ruby-on-Rails related. Has anyone managed to upload a file to S3 directly (from client browser) using MVC?
So, after a morning of smashing my head into my keyboard, the following snippet of code works (with the obvious credentials removed):
using (AmazonS3 client = Amazon.AWSClientFactory.CreateAmazonS3Client("Access_Key",
"Secret_Key"))
{
PutObjectRequest request = new PutObjectRequest();
request.WithBucketName("BUCKET-NAME")
.WithCannedACL(S3CannedACL.PublicRead)
.WithKey("myDirectory/" +
HttpContext.Current.Server.UrlEncode(fileBase.FileName))
.InputStream = fileBase.InputStream;
S3Response response = client.PutObject(request);
}

Resources