Error with open-uri on Heroku - ruby-on-rails

I'm using the code below to open and upload a Facebook profile image to S3 using Paperclip. The code works fine on my localhost (Rails 3.1, ruby 1.9.2 p290) but it breaks on heroku (bamboo-mri-1.9.2)
facebook_image_url = access_token['user_info']['image'].gsub("square", "large")
url = URI.parse(facebook_image_url)
#Follow the redirect of Facebook profile picture url
res = Net::HTTP.start(url.host, url.port) { |http|
http.get(facebook_image_url)
}
signed_in_resource.avatar = open(res['location'])
signed_in_resource.save
signed_in_resource
Does anyone have any suggestion?

I have hade similar problems solved the issue by using an updated version of URLTempfile. Also using S3 as image storage.
https://github.com/chris/paperclip_url_support, put in lib folder of your installation.
product = Product.first(:conditions => {:_id => self.product_id})
raise "Unable to get product with id #{self.product_id}" unless product
product.picture = URLTempfile.new(self.image_url)
product.save!
Using this in production on heroku with same config as you. (Heroku 1.9.3, Rails 3.1).
Hope this helps you.

Related

Docusign gem block request

I'm using the DocuSign ruby client (https://github.com/docusign/docusign-ruby-client) on ruby on rails to send a document via email to some clients, but when I deploy the project after 15 minutes the request between the application and DocuSign gets "paused". For some reason the gem creates the request but doesn't send it as you can see in the next image where I enable the debug in the gem:
In that point the log doesn't print any more after 15 minutes.
The code that send the message in my app is:
access_token = "xxxxxxx"
account_id = "xxxxxxxxx"
base_path = "xxxxxxxxxx"
envelope_args = {
signer_email: contact.email,
signer_name: contact.name,
template_id: document.docusign_id
}
#args = {
account_id: account_id,
base_path: base_path,
access_token: access_token,
envelope_args: envelope_args
}
envelope_args = #args[:envelope_args]
# 1. Create the envelope request object
envelope_definition = make_envelope(envelope_args)
# 2. Call Envelopes::create API method
# Exceptions will be caught by the calling function
envelope_api = create_envelope_api(#args)
envelope_api.create_envelope #args[:account_id], envelope_definition
I don't know what can I do.
Thank you
Your screenshot shows everything happening at 18:29:05 -- I don't understand the issue.
Also, have you tried install/using the RoR code example?
See if it has the same problem.
We looked for the issue in our code, but we saw that in the step which the request is send in the gem here always freezes. So we debugged there and we saw that curl was being used for ruby. At that point we saw that curl was trying to reconnect with Docusign but it wasn't success, so we found this issue in the version of curl that we had (https://github.com/curl/curl/issues/4499 )
To fix it we updated the version to the latest, and it fixed the issue.
Thanks for your answers.

Rails 5.0.1 - ActionMailer with MailGun, setting 'noname' for filename when directly attaching base64 string

I have read the entire official guide here and about 5 questions on SO that describe this problem for Rails 3 and Rails 4.
This question is for Rails 5, but he is using an actual file, not an encoded string.
This is my mailer code (and yes I have both views setup). The email comes through perfectly fine, with the attached PDF.
def pdf_quote(proposal)
#proposal = proposal
email_with_name = %("#{#proposal.first_name} #{#proposal.last_name}" <#{#proposal.email}>)
filename = "QD-#{#proposal.qd_number}_#{#proposal.last_name}.pdf"
attachments[filename] = {
mime_type: 'application/pdf',
encoding: 'Base64',
content: #proposal.pdf_base64
}
mail(
to: email_with_name,
from: 'Floorbook UK <email address>',
subject: 'Your Personal Flooring Quote is attached',
sent_on: Time.now
)
end
In gmail the attachment is called 'noname' and in Postbox it is called 'pb_mime_attachment.pdf'
How can I get ActionMailer to use the filename I provide?
Note that I am using MailGun (mailgun-ruby gem 1.1.4) to send the email, so it could be the gem at fault here.
It turned out to be a bug with 3rd party mailgun-ruby gem.
I raised a bug with them, here are the details:
https://github.com/mailgun/mailgun-ruby/issues/82
It is fixed (I tested it) in version 1.1.5.

Why do I get the error 'HTTP Response: 403 Forbidden' when I try to call the amazon api in my Rails app?

I'm creating a Ruby on Rails application and am following the instructions step for step based on the gem required to call the amazon API (https://github.com/jugend/amazon-ecs) when searching for an item.
This is my code in the Ruby app:
Amazon::Ecs.options = {
:associate_tag => '[*****]',
:AWS_access_key_id => '[*****]',
:AWS_secret_key => '[*****]'
}
res = Amazon::Ecs.item_search('soccer books')
puts res.total_items
I've tried regenerating my keys, still doesn't work and am not sure what to do.
It might be possible issue in gem as they already solved if for 1.9 . You can check changelog here https://github.com/jugend/amazon-ecs/blob/master/CHANGELOG . This confirm that issue resolved for 1.9 . But might be come up again due to new changes they done. So my opinion is to create an issue in gem as well.
please check s3-bucket timestamp and you local machine timestamp. If its greate the 10hr or more s3 considers as invalid request and refuse it.

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

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.
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))

Resources