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
Related
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.
I have embedded the youtube and vimeo links in my site and trying to show the thumbnail of the video as a link to play while on click.
i tried the gem "has_vimeo_video" but it only accepts the vimeo videos.So i want to show the thumbnails of both the videos i.e youtube and vimeo.
Please need solution.
Thanks
the video_info gem is great for fetching thumbnails:
https://github.com/thibaudgg/video_info
I found out yesterday that there was carrierwave-video-thumbnailer which is pretty cool. It basically does what you want
A thumbnailer plugin for Carrierwave. It mixes into your uploader setup and makes easy thumbnailing of your uploaded videos. This software is quite an alpha right now so any kind of OpenSource collaboration is welcome.
But you will need to of course include carrierwave into your application. Hopefully this helps
Carreierwave is an uploader that enables users to upload content in their app, which I think
Update
Alternatively you can use embedly which allows you to add embedded media into your application. The API provides a lot response options as can be seen here. In doing so you could do something like:
$.embedly('http://www.youtube.com/watch?v=_FE194VN6c4',
{maxWidth: 600,
elems: $('#element'),
success: function(oembed, dict){
alert(oembed.title);
});
The elems property is where you want the video to be embedded, so that when the link is pasted in it should embed the video. You need to pass the video url and get it using the jQuery selector from wherever you put your url in your html.
Your other alternative is to have a look at the following auto_html gem
Add a new file 'thumbnail_helper.rb' in '/app/helper'. Copy and paste the following code in that file:
module ThumbnailHelper
# Regex to find YouTube's and Vimeo's video ID
YOUTUBE_REGEX = %r(^(http[s]*:\/\/)?(www.)?(youtube.com|youtu.be)\/(watch\?v=){0,1}([a-zA-Z0-9_-]{11}))
VIMEO_REGEX = %r(^https?:\/\/(?:.*?)\.?(vimeo)\.com\/(\d+).*$)
# Finds YouTube's video ID from given URL or [nil] if URL is invalid
# The video ID matches the RegEx \[a-zA-Z0-9_-]{11}\
def find_youtube_id url
url = sanitize url
matches = YOUTUBE_REGEX.match url.to_str
if matches
matches[6] || matches[5]
end
end
# Finds youtube video thumbnail
def get_youtube_thumbnail url
youtube_id = find_youtube_id url
result = "http://img.youtube.com/vi/#{youtube_id}/0.jpg"
end
# Finds Vimeo's video ID from given URL or [nil] if URL is invalid
def find_vimeo_id url
url = sanitize url
matches = VIMEO_REGEX.match url.to_str
matches[2] if matches
end
# Finds vimeo video thumbnail
def get_vimeo_thumbnail url
vimeo_id = find_vimeo_id url
result = URI.open("http://vimeo.com/api/v2/video/#{vimeo_id}.json").read
begin
JSON.parse(result).first['thumbnail_large']
rescue StandardError
nil
end
end
# Main function
# Return a video thumbnail
# If the url provided is not a valid YouTube or Vimeo url it returns [nil]
def get_video_thumbnail(url)
if find_vimeo_id(url)
get_vimeo_thumbnail(url)
elsif find_youtube_id(url)
get_youtube_thumbnail(url)
end
end
end
Now call the get_video_thumbnail('video_url') from your view. It will return you the thumbnail of the given video.
You don't need any gems for such a simple task
url = 'https://vimeo.com/126100721'
id = url.partition('vimeo.com/').last
result = URI.open("http://vimeo.com/api/v2/video/#{id}.json").read
begin
JSON.parse(result).first['thumbnail_large']
rescue StandardError
nil
end
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.
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))
I'm currently using mdeering's gravatar_image_tag plugin to get gravatar images for users but my dilemma is to try to detect in the code if the user has a gravatar:
If he does then display the gravatar image.
If not, then display a local default image file on my server.
I'm open to using other plugins if they offer this functionality.
Please provide code examples. They help me learn the best.
Thanks!
You don't need gems/plugins. This screencast explains what you need step-by-step. It comes down to using the following helper method:
def avatar_url(user)
default_url = "#{root_url}images/guest.png"
gravatar_id = Digest::MD5.hexdigest(user.email.downcase)
"http://gravatar.com/avatar/#{gravatar_id}.png?s=48&d=#{CGI.escape(default_url)}"
end
Here is a helper method to check if a user has already a gravatar image :
The trick is to get gravatar image with a false default image and then check header response.
It's achieved with the Net::HTTP ruby library.
def gravatar?(user)
gravatar_check = "http://gravatar.com/avatar/#{Digest::MD5.hexdigest(user.gravatar_email.downcase)}.png?d=404"
uri = URI.parse(gravatar_check)
http = Net::HTTP.new(uri.host, uri.port)
request = Net::HTTP::Get.new(uri.request_uri)
response = http.request(request)
if (response.code.to_i == 404)
return false
else
return true
end
end
Gravtastic gem should come to your rescue. Its fairly straightforward - you may peruse through its README. The Gem's github link
Looks like I'm extremely late to the party but the gravatar_image_tag gems does have the options that you need to accomplish this very easily.
You can configure your default image globally in your application like so:
# config/initializers/gravatar_image_tag.rb
GravatarImageTag.configure do |config|
# Set this to use your own default gravatar image rather then serving up Gravatar's default image [ 'http://example.com/images/default_gravitar.jpg', :identicon, :monsterid, :wavatar, 404 ].
config.default_image = nil
end
Or on a one off basis like so:
gravatar_image_tag('junk', alt: 'Github Default Gravatar', gravatar: { default: 'https://assets.github.com/images/gravatars/gravatar-140.png' })